messageFormatter.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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: 0,
  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: 1,
  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. type: BreadcrumbType.DEBUG,
  101. category: 'console',
  102. data: {
  103. arguments: ['This is a literal 100%'],
  104. logger: 'console',
  105. },
  106. level: BreadcrumbLevelType.LOG,
  107. message: 'This is a literal 100%',
  108. timestamp: '2022-06-22T20:00:39.959Z',
  109. id: 6,
  110. color: 'purple300',
  111. description: 'Debug',
  112. },
  113. {
  114. type: BreadcrumbType.DEBUG,
  115. category: 'console',
  116. data: {
  117. arguments: ['Unbound placeholder %s'],
  118. logger: 'console',
  119. },
  120. level: BreadcrumbLevelType.LOG,
  121. message: 'Unbound placeholder %s',
  122. timestamp: '2022-06-22T20:00:39.959Z',
  123. id: 7,
  124. color: 'purple300',
  125. description: 'Debug',
  126. },
  127. {
  128. type: BreadcrumbType.DEBUG,
  129. category: 'console',
  130. data: {
  131. arguments: ['Placeholder %s with 100%', 'myPlaceholder'],
  132. logger: 'console',
  133. },
  134. level: BreadcrumbLevelType.LOG,
  135. message: 'Placeholder %s with 100%',
  136. timestamp: '2022-06-22T20:00:39.959Z',
  137. id: 8,
  138. color: 'purple300',
  139. description: 'Debug',
  140. },
  141. ];
  142. describe('MessageFormatter', () => {
  143. it('Should print console message with placeholders correctly', () => {
  144. render(<MessageFormatter breadcrumb={breadcrumbs[0]} />);
  145. expect(screen.getByText('This is a test')).toBeInTheDocument();
  146. });
  147. it('Should print console message with objects correctly', () => {
  148. render(<MessageFormatter breadcrumb={breadcrumbs[1]} />);
  149. expect(screen.getByText('test 1 false')).toBeInTheDocument();
  150. expect(screen.getByText('{}')).toBeInTheDocument();
  151. });
  152. it('Should print console message correctly when it is an Error object', () => {
  153. render(<MessageFormatter breadcrumb={breadcrumbs[2]} />);
  154. expect(screen.getByText('this is my error message')).toBeInTheDocument();
  155. });
  156. it('Should print empty object in case there is no message prop', () => {
  157. render(<MessageFormatter breadcrumb={breadcrumbs[3]} />);
  158. expect(screen.getByText('{}')).toBeInTheDocument();
  159. });
  160. it('Should ignore the "%c" placheholder and print the console message correctly', () => {
  161. render(<MessageFormatter breadcrumb={breadcrumbs[4]} />);
  162. expect(screen.getByText(/%c prev state/)).toBeInTheDocument();
  163. expect(screen.getByText('cart')).toBeInTheDocument();
  164. expect(screen.getByText('Array(0)')).toBeInTheDocument();
  165. });
  166. it('Should print arrays correctly', () => {
  167. render(<MessageFormatter breadcrumb={breadcrumbs[5]} />);
  168. expect(screen.getByText('test')).toBeInTheDocument();
  169. expect(screen.getByText('(2)')).toBeInTheDocument();
  170. // expect(screen.getByText('[')).toBeInTheDocument();
  171. expect(screen.getByText('"foo"')).toBeInTheDocument();
  172. expect(screen.getByText('"bar"')).toBeInTheDocument();
  173. // expect(screen.getByText(']')).toBeInTheDocument();
  174. });
  175. it('Should print literal %', () => {
  176. render(<MessageFormatter breadcrumb={breadcrumbs[6]} />);
  177. expect(screen.getByText('This is a literal 100%')).toBeInTheDocument();
  178. });
  179. it('Should print unbound %s placeholder', () => {
  180. render(<MessageFormatter breadcrumb={breadcrumbs[7]} />);
  181. expect(screen.getByText('Unbound placeholder %s')).toBeInTheDocument();
  182. });
  183. it('Should print placeholder with literal %', () => {
  184. render(<MessageFormatter breadcrumb={breadcrumbs[8]} />);
  185. expect(screen.getByText('Placeholder myPlaceholder with 100%')).toBeInTheDocument();
  186. });
  187. });