messageFormatter.spec.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 with objects correctly', () => {
  22. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  23. TestStubs.Replay.ConsoleFrame({
  24. data: {
  25. arguments: ['test', 1, false, {}],
  26. logger: 'console',
  27. },
  28. level: BreadcrumbLevelType.LOG,
  29. message: 'test 1 false [object Object]',
  30. timestamp: new Date('2022-06-22T16:49:11.198Z'),
  31. }),
  32. ]);
  33. render(<MessageFormatter frame={frame} />);
  34. expect(screen.getByText('test 1 false')).toBeInTheDocument();
  35. expect(screen.getByText('{}')).toBeInTheDocument();
  36. });
  37. it('Should print console message correctly when it is an Error object', () => {
  38. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  39. TestStubs.Replay.ConsoleFrame({
  40. data: {
  41. arguments: [{}],
  42. logger: 'console',
  43. },
  44. level: BreadcrumbLevelType.ERROR,
  45. message: 'Error: this is my error message',
  46. timestamp: new Date('2022-06-22T20:00:39.958Z'),
  47. }),
  48. ]);
  49. render(<MessageFormatter frame={frame} />);
  50. expect(screen.getByText('this is my error message')).toBeInTheDocument();
  51. });
  52. it('Should print empty object in case there is no message prop', () => {
  53. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  54. TestStubs.Replay.ConsoleFrame({
  55. data: {
  56. arguments: [{}],
  57. logger: 'console',
  58. },
  59. level: BreadcrumbLevelType.ERROR,
  60. timestamp: new Date('2022-06-22T20:00:39.958Z'),
  61. }),
  62. ]);
  63. render(<MessageFormatter frame={frame} />);
  64. expect(screen.getByText('{}')).toBeInTheDocument();
  65. });
  66. it('Should ignore the "%c" placheholder and print the console message correctly', () => {
  67. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  68. TestStubs.Replay.ConsoleFrame({
  69. data: {
  70. arguments: [
  71. '%c prev state',
  72. 'color: #9E9E9E; font-weight: bold',
  73. {
  74. cart: [],
  75. },
  76. ],
  77. logger: 'console',
  78. },
  79. level: BreadcrumbLevelType.LOG,
  80. message: '%c prev state color: #9E9E9E; font-weight: bold [object Object]',
  81. timestamp: new Date('2022-06-09T00:50:25.273Z'),
  82. }),
  83. ]);
  84. render(<MessageFormatter frame={frame} />);
  85. expect(screen.getByText(/%c prev state/)).toBeInTheDocument();
  86. expect(screen.getByText('cart')).toBeInTheDocument();
  87. expect(screen.getByText('Array(0)')).toBeInTheDocument();
  88. });
  89. it('Should print arrays correctly', () => {
  90. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  91. TestStubs.Replay.ConsoleFrame({
  92. data: {
  93. arguments: ['test', ['foo', 'bar']],
  94. logger: 'console',
  95. },
  96. level: BreadcrumbLevelType.LOG,
  97. message: 'test foo,bar',
  98. timestamp: new Date('2022-06-23T17:09:31.158Z'),
  99. }),
  100. ]);
  101. render(<MessageFormatter frame={frame} />);
  102. expect(screen.getByText('test')).toBeInTheDocument();
  103. expect(screen.getByText('(2)')).toBeInTheDocument();
  104. // expect(screen.getByText('[')).toBeInTheDocument();
  105. expect(screen.getByText('"foo"')).toBeInTheDocument();
  106. expect(screen.getByText('"bar"')).toBeInTheDocument();
  107. // expect(screen.getByText(']')).toBeInTheDocument();
  108. });
  109. it('Should print literal %', () => {
  110. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  111. TestStubs.Replay.ConsoleFrame({
  112. data: {
  113. arguments: ['This is a literal 100%'],
  114. logger: 'console',
  115. },
  116. level: BreadcrumbLevelType.LOG,
  117. message: 'This is a literal 100%',
  118. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  119. }),
  120. ]);
  121. render(<MessageFormatter frame={frame} />);
  122. expect(screen.getByText('This is a literal 100%')).toBeInTheDocument();
  123. });
  124. it('Should print unbound %s placeholder', () => {
  125. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  126. TestStubs.Replay.ConsoleFrame({
  127. data: {
  128. arguments: ['Unbound placeholder %s'],
  129. logger: 'console',
  130. },
  131. level: BreadcrumbLevelType.LOG,
  132. message: 'Unbound placeholder %s',
  133. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  134. }),
  135. ]);
  136. render(<MessageFormatter frame={frame} />);
  137. expect(screen.getByText('Unbound placeholder %s')).toBeInTheDocument();
  138. });
  139. it('Should print placeholder with literal %', () => {
  140. const [frame] = hydrateBreadcrumbs(TestStubs.ReplayRecord(), [
  141. TestStubs.Replay.ConsoleFrame({
  142. data: {
  143. arguments: ['Placeholder %s with 100%', 'myPlaceholder'],
  144. logger: 'console',
  145. },
  146. level: BreadcrumbLevelType.LOG,
  147. message: 'Placeholder %s with 100%',
  148. timestamp: new Date('2022-06-22T20:00:39.959Z'),
  149. }),
  150. ]);
  151. render(<MessageFormatter frame={frame} />);
  152. expect(screen.getByText('Placeholder myPlaceholder with 100%')).toBeInTheDocument();
  153. });
  154. });