messageFormatter.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import {
  3. BreadcrumbLevelType,
  4. BreadcrumbType,
  5. BreadcrumbTypeDefault,
  6. Crumb,
  7. } from 'sentry/types/breadcrumbs';
  8. import MessageFormatter from './messageFormatter';
  9. const breadcrumbs: Extract<Crumb, BreadcrumbTypeDefault>[] = [
  10. {
  11. type: BreadcrumbType.DEBUG,
  12. category: 'console',
  13. data: {
  14. arguments: ['This is a %s', 'test'],
  15. logger: 'console',
  16. },
  17. level: BreadcrumbLevelType.LOG,
  18. message: 'This is a %s test',
  19. timestamp: '2022-06-22T20:00:39.959Z',
  20. id: 1,
  21. color: 'purple300',
  22. description: 'Debug',
  23. },
  24. {
  25. type: BreadcrumbType.DEBUG,
  26. category: 'console',
  27. data: {
  28. arguments: ['test', 1, false, {}],
  29. logger: 'console',
  30. },
  31. level: BreadcrumbLevelType.LOG,
  32. message: 'test 1 false [object Object]',
  33. timestamp: '2022-06-22T16:49:11.198Z',
  34. id: 2,
  35. color: 'purple300',
  36. description: 'Debug',
  37. },
  38. {
  39. type: BreadcrumbType.DEBUG,
  40. category: 'console',
  41. data: {
  42. arguments: [{}],
  43. logger: 'console',
  44. },
  45. level: BreadcrumbLevelType.ERROR,
  46. message: 'Error: this is my error message',
  47. timestamp: '2022-06-22T20:00:39.958Z',
  48. id: 2,
  49. color: 'purple300',
  50. description: 'Debug',
  51. },
  52. {
  53. type: BreadcrumbType.DEBUG,
  54. category: 'console',
  55. data: {
  56. arguments: [{}],
  57. logger: 'console',
  58. },
  59. level: BreadcrumbLevelType.ERROR,
  60. timestamp: '2022-06-22T20:00:39.958Z',
  61. id: 3,
  62. color: 'red300',
  63. description: 'Error',
  64. },
  65. {
  66. type: BreadcrumbType.DEBUG,
  67. category: 'console',
  68. data: {
  69. arguments: [
  70. '%c prev state',
  71. 'color: #9E9E9E; font-weight: bold',
  72. {
  73. cart: [],
  74. },
  75. ],
  76. logger: 'console',
  77. },
  78. level: BreadcrumbLevelType.LOG,
  79. message: '%c prev state color: #9E9E9E; font-weight: bold [object Object]',
  80. timestamp: '2022-06-09T00:50:25.273Z',
  81. id: 4,
  82. color: 'purple300',
  83. description: 'Debug',
  84. },
  85. {
  86. type: BreadcrumbType.DEBUG,
  87. category: 'console',
  88. data: {
  89. arguments: ['test', ['foo', 'bar']],
  90. logger: 'console',
  91. },
  92. level: BreadcrumbLevelType.LOG,
  93. message: 'test foo,bar',
  94. timestamp: '2022-06-23T17:09:31.158Z',
  95. id: 5,
  96. color: 'purple300',
  97. description: 'Debug',
  98. },
  99. ];
  100. describe('MessageFormatter', () => {
  101. it('Should print console message with placeholders correctly', () => {
  102. render(<MessageFormatter breadcrumb={breadcrumbs[0]} />);
  103. expect(screen.getByText('This is a test')).toBeInTheDocument();
  104. });
  105. it('Should print console message with objects correctly', () => {
  106. render(<MessageFormatter breadcrumb={breadcrumbs[1]} />);
  107. expect(screen.getByText('test 1 false {}')).toBeInTheDocument();
  108. });
  109. it('Should print console message correctly when it is an Error object', () => {
  110. render(<MessageFormatter breadcrumb={breadcrumbs[2]} />);
  111. expect(screen.getByText('Error: this is my error message')).toBeInTheDocument();
  112. });
  113. it('Should print empty object in case there is no message prop', () => {
  114. render(<MessageFormatter breadcrumb={breadcrumbs[3]} />);
  115. expect(screen.getByText('{}')).toBeInTheDocument();
  116. });
  117. it('Should ignore the "%c" placheholder and print the console message correctly', () => {
  118. render(<MessageFormatter breadcrumb={breadcrumbs[4]} />);
  119. expect(screen.getByText('prev state {"cart":[]}')).toBeInTheDocument();
  120. });
  121. it('Should print arrays correctly', () => {
  122. render(<MessageFormatter breadcrumb={breadcrumbs[5]} />);
  123. expect(screen.getByText('test ["foo","bar"]')).toBeInTheDocument();
  124. });
  125. });