messageFormatter.spec.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import {BreadcrumbLevelType} from 'sentry/types/breadcrumbs';
  3. import hydrateBreadcrumbs from 'sentry/utils/replays/hydrateBreadcrumbs';
  4. import MessageFormatter from 'sentry/views/replays/detail/console/messageFormatter';
  5. describe('MessageFormatter', () => {
  6. it('Should print console message with placeholders correctly', () => {
  7. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  8. TestStubs.Replay.ConsoleFrame({
  9. data: {
  10. arguments: ['This is a %s', 'test'],
  11. logger: 'console',
  12. },
  13. level: BreadcrumbLevelType.LOG,
  14. message: 'This is a %s test',
  15. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  16. }),
  17. ]);
  18. render(<MessageFormatter frame={frame} />);
  19. expect(screen.getByText('This is a test')).toBeInTheDocument();
  20. });
  21. it('Should print console message without data', () => {
  22. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  23. TestStubs.Replay.ConsoleFrame({
  24. level: BreadcrumbLevelType.LOG,
  25. message: 'This is only a test',
  26. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  27. }),
  28. ]);
  29. // Manually delete `data` from the mock.
  30. // This is reasonable because the type, at this point, `frame` is of type
  31. // `BreadcrumbFrame` and not `ConsoleFrame`.
  32. // When the type is narrowed to `ConsoleFrame` the `data` field is forced to exist.
  33. delete frame.data;
  34. render(<MessageFormatter frame={frame} />);
  35. expect(screen.getByText('This is only a test')).toBeInTheDocument();
  36. });
  37. it('Should print console message with objects correctly', () => {
  38. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  39. TestStubs.Replay.ConsoleFrame({
  40. data: {
  41. arguments: ['test', 1, false, {}],
  42. logger: 'console',
  43. },
  44. level: BreadcrumbLevelType.LOG,
  45. message: 'test 1 false [object Object]',
  46. timestamp: new Date('2022-06-22T16:49:11.198Z'),
  47. }),
  48. ]);
  49. render(<MessageFormatter frame={frame} />);
  50. expect(screen.getByText('test 1 false')).toBeInTheDocument();
  51. expect(screen.getByText('{}')).toBeInTheDocument();
  52. });
  53. it('Should print console message correctly when it is an Error object', () => {
  54. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  55. TestStubs.Replay.ConsoleFrame({
  56. data: {
  57. arguments: [{}],
  58. logger: 'console',
  59. },
  60. level: BreadcrumbLevelType.ERROR,
  61. message: 'Error: this is my error message',
  62. timestamp: new Date('2022-06-22T20:00:39.958Z'),
  63. }),
  64. ]);
  65. render(<MessageFormatter frame={frame} />);
  66. expect(screen.getByText('this is my error message')).toBeInTheDocument();
  67. });
  68. it('Should print empty object in case there is no message prop', () => {
  69. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  70. TestStubs.Replay.ConsoleFrame({
  71. data: {
  72. arguments: [{}],
  73. logger: 'console',
  74. },
  75. level: BreadcrumbLevelType.ERROR,
  76. timestamp: new Date('2022-06-22T20:00:39.958Z'),
  77. }),
  78. ]);
  79. render(<MessageFormatter frame={frame} />);
  80. expect(screen.getByText('{}')).toBeInTheDocument();
  81. });
  82. it('Should ignore the "%c" placheholder and print the console message correctly', () => {
  83. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  84. TestStubs.Replay.ConsoleFrame({
  85. data: {
  86. arguments: [
  87. '%c prev state',
  88. 'color: #9E9E9E; font-weight: bold',
  89. {
  90. cart: [],
  91. },
  92. ],
  93. logger: 'console',
  94. },
  95. level: BreadcrumbLevelType.LOG,
  96. message: '%c prev state color: #9E9E9E; font-weight: bold [object Object]',
  97. timestamp: new Date('2022-06-09T00:50:25.273Z'),
  98. }),
  99. ]);
  100. render(<MessageFormatter frame={frame} />);
  101. expect(screen.getByText(/%c prev state/)).toBeInTheDocument();
  102. expect(screen.getByText('cart')).toBeInTheDocument();
  103. expect(screen.getByText('Array(0)')).toBeInTheDocument();
  104. });
  105. it('Should print arrays correctly', () => {
  106. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  107. TestStubs.Replay.ConsoleFrame({
  108. data: {
  109. arguments: ['test', ['foo', 'bar']],
  110. logger: 'console',
  111. },
  112. level: BreadcrumbLevelType.LOG,
  113. message: 'test foo,bar',
  114. timestamp: new Date('2022-06-23T17:09:31.158Z'),
  115. }),
  116. ]);
  117. render(<MessageFormatter frame={frame} />);
  118. expect(screen.getByText('test')).toBeInTheDocument();
  119. expect(screen.getByText('(2)')).toBeInTheDocument();
  120. // expect(screen.getByText('[')).toBeInTheDocument();
  121. expect(screen.getByText('"foo"')).toBeInTheDocument();
  122. expect(screen.getByText('"bar"')).toBeInTheDocument();
  123. // expect(screen.getByText(']')).toBeInTheDocument();
  124. });
  125. it('Should print literal %', () => {
  126. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  127. TestStubs.Replay.ConsoleFrame({
  128. data: {
  129. arguments: ['This is a literal 100%'],
  130. logger: 'console',
  131. },
  132. level: BreadcrumbLevelType.LOG,
  133. message: 'This is a literal 100%',
  134. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  135. }),
  136. ]);
  137. render(<MessageFormatter frame={frame} />);
  138. expect(screen.getByText('This is a literal 100%')).toBeInTheDocument();
  139. });
  140. it('Should print unbound %s placeholder', () => {
  141. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  142. TestStubs.Replay.ConsoleFrame({
  143. data: {
  144. arguments: ['Unbound placeholder %s'],
  145. logger: 'console',
  146. },
  147. level: BreadcrumbLevelType.LOG,
  148. message: 'Unbound placeholder %s',
  149. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  150. }),
  151. ]);
  152. render(<MessageFormatter frame={frame} />);
  153. expect(screen.getByText('Unbound placeholder %s')).toBeInTheDocument();
  154. });
  155. it('Should print placeholder with literal %', () => {
  156. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  157. TestStubs.Replay.ConsoleFrame({
  158. data: {
  159. arguments: ['Placeholder %s with 100%', 'myPlaceholder'],
  160. logger: 'console',
  161. },
  162. level: BreadcrumbLevelType.LOG,
  163. message: 'Placeholder %s with 100%',
  164. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  165. }),
  166. ]);
  167. render(<MessageFormatter frame={frame} />);
  168. expect(screen.getByText('Placeholder myPlaceholder with 100%')).toBeInTheDocument();
  169. });
  170. });