messageFormatter.spec.tsx 3.3 KB

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