messageFormatter.spec.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {
  4. BreadcrumbLevelType,
  5. BreadcrumbType,
  6. BreadcrumbTypeDefault,
  7. Crumb,
  8. } from 'sentry/types/breadcrumbs';
  9. import {OrganizationContext} from 'sentry/views/organizationContext';
  10. import {RouteContext} from 'sentry/views/routeContext';
  11. import MessageFormatter from './messageFormatter';
  12. const breadcrumbs: Extract<Crumb, BreadcrumbTypeDefault>[] = [
  13. {
  14. type: BreadcrumbType.DEBUG,
  15. category: 'console',
  16. data: {
  17. arguments: ['This is a %s', 'test'],
  18. logger: 'console',
  19. },
  20. level: BreadcrumbLevelType.LOG,
  21. message: 'This is a %s test',
  22. timestamp: '2022-06-22T20:00:39.959Z',
  23. id: 1,
  24. color: 'purple300',
  25. description: 'Debug',
  26. },
  27. {
  28. type: BreadcrumbType.DEBUG,
  29. category: 'console',
  30. data: {
  31. arguments: ['test', 1, false, {}],
  32. logger: 'console',
  33. },
  34. level: BreadcrumbLevelType.LOG,
  35. message: 'test 1 false [object Object]',
  36. timestamp: '2022-06-22T16:49:11.198Z',
  37. id: 2,
  38. color: 'purple300',
  39. description: 'Debug',
  40. },
  41. {
  42. type: BreadcrumbType.DEBUG,
  43. category: 'console',
  44. data: {
  45. arguments: [{}],
  46. logger: 'console',
  47. },
  48. level: BreadcrumbLevelType.ERROR,
  49. message: 'Error: this is my error message',
  50. timestamp: '2022-06-22T20:00:39.958Z',
  51. id: 2,
  52. color: 'purple300',
  53. description: 'Debug',
  54. },
  55. {
  56. type: BreadcrumbType.DEBUG,
  57. category: 'console',
  58. data: {
  59. arguments: [{}],
  60. logger: 'console',
  61. },
  62. level: BreadcrumbLevelType.ERROR,
  63. timestamp: '2022-06-22T20:00:39.958Z',
  64. id: 3,
  65. color: 'red300',
  66. description: 'Error',
  67. },
  68. {
  69. type: BreadcrumbType.DEBUG,
  70. category: 'console',
  71. data: {
  72. arguments: [
  73. '%c prev state',
  74. 'color: #9E9E9E; font-weight: bold',
  75. {
  76. cart: [],
  77. },
  78. ],
  79. logger: 'console',
  80. },
  81. level: BreadcrumbLevelType.LOG,
  82. message: '%c prev state color: #9E9E9E; font-weight: bold [object Object]',
  83. timestamp: '2022-06-09T00:50:25.273Z',
  84. id: 4,
  85. color: 'purple300',
  86. description: 'Debug',
  87. },
  88. {
  89. type: BreadcrumbType.DEBUG,
  90. category: 'console',
  91. data: {
  92. arguments: ['test', ['foo', 'bar']],
  93. logger: 'console',
  94. },
  95. level: BreadcrumbLevelType.LOG,
  96. message: 'test foo,bar',
  97. timestamp: '2022-06-23T17:09:31.158Z',
  98. id: 5,
  99. color: 'purple300',
  100. description: 'Debug',
  101. },
  102. ];
  103. function TestComponent({children}: {children: React.ReactNode}) {
  104. const {organization, router} = initializeOrg();
  105. return (
  106. <OrganizationContext.Provider value={organization}>
  107. <RouteContext.Provider
  108. value={{
  109. router,
  110. location: router.location,
  111. params: {},
  112. routes: [],
  113. }}
  114. >
  115. {children}
  116. </RouteContext.Provider>
  117. </OrganizationContext.Provider>
  118. );
  119. }
  120. describe('MessageFormatter', () => {
  121. it('Should print console message with placeholders correctly', () => {
  122. render(
  123. <TestComponent>
  124. <MessageFormatter breadcrumb={breadcrumbs[0]} />
  125. </TestComponent>
  126. );
  127. expect(screen.getByRole('text')).toHaveTextContent('This is a test');
  128. });
  129. it('Should print console message with objects correctly', () => {
  130. render(
  131. <TestComponent>
  132. <MessageFormatter breadcrumb={breadcrumbs[1]} />
  133. </TestComponent>
  134. );
  135. expect(screen.getByRole('text')).toHaveTextContent('test 1 false {}');
  136. });
  137. it('Should print console message correctly when it is an Error object', () => {
  138. render(
  139. <TestComponent>
  140. <MessageFormatter breadcrumb={breadcrumbs[2]} />
  141. </TestComponent>
  142. );
  143. expect(screen.getByRole('text')).toHaveTextContent('Error: this is my error message');
  144. });
  145. it('Should print empty object in case there is no message prop', () => {
  146. render(
  147. <TestComponent>
  148. <MessageFormatter breadcrumb={breadcrumbs[3]} />
  149. </TestComponent>
  150. );
  151. expect(screen.getByRole('text')).toHaveTextContent('{}');
  152. });
  153. it('Should ignore the "%c" placheholder and print the console message correctly', () => {
  154. render(
  155. <TestComponent>
  156. <MessageFormatter breadcrumb={breadcrumbs[4]} />
  157. </TestComponent>
  158. );
  159. expect(screen.getByRole('text')).toHaveTextContent('prev state {"cart":[]}');
  160. });
  161. it('Should print arrays correctly', () => {
  162. render(
  163. <TestComponent>
  164. <MessageFormatter breadcrumb={breadcrumbs[5]} />
  165. </TestComponent>
  166. );
  167. expect(screen.getByRole('text')).toHaveTextContent('test ["foo","bar"]');
  168. });
  169. });