breadcrumbs.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import {textWithMarkupMatcher} from 'sentry-test/utils';
  4. import Breadcrumbs from 'sentry/components/events/interfaces/breadcrumbs';
  5. import {BreadcrumbLevelType, BreadcrumbType} from 'sentry/types/breadcrumbs';
  6. import {EntryType} from 'sentry/types/event';
  7. describe('Breadcrumbs', () => {
  8. let props: React.ComponentProps<typeof Breadcrumbs>;
  9. const {router} = initializeOrg();
  10. beforeEach(() => {
  11. props = {
  12. route: {},
  13. router,
  14. organization: TestStubs.Organization(),
  15. event: TestStubs.Event({entries: []}),
  16. type: EntryType.BREADCRUMBS,
  17. data: {
  18. values: [
  19. {
  20. message: 'sup',
  21. category: 'default',
  22. level: BreadcrumbLevelType.WARNING,
  23. type: BreadcrumbType.INFO,
  24. },
  25. {
  26. message: 'hey',
  27. category: 'error',
  28. level: BreadcrumbLevelType.INFO,
  29. type: BreadcrumbType.INFO,
  30. },
  31. {
  32. message: 'hello',
  33. category: 'default',
  34. level: BreadcrumbLevelType.WARNING,
  35. type: BreadcrumbType.INFO,
  36. },
  37. {
  38. message: 'bye',
  39. category: 'default',
  40. level: BreadcrumbLevelType.WARNING,
  41. type: BreadcrumbType.INFO,
  42. },
  43. {
  44. message: 'ok',
  45. category: 'error',
  46. level: BreadcrumbLevelType.WARNING,
  47. type: BreadcrumbType.INFO,
  48. },
  49. {
  50. message: 'sup',
  51. category: 'default',
  52. level: BreadcrumbLevelType.WARNING,
  53. type: BreadcrumbType.INFO,
  54. },
  55. {
  56. message: 'sup',
  57. category: 'default',
  58. level: BreadcrumbLevelType.INFO,
  59. type: BreadcrumbType.INFO,
  60. },
  61. ],
  62. },
  63. };
  64. });
  65. describe('filterCrumbs', function () {
  66. it('should filter crumbs based on crumb message', async function () {
  67. render(<Breadcrumbs {...props} />);
  68. userEvent.type(screen.getByPlaceholderText('Search breadcrumbs'), 'hi');
  69. expect(
  70. await screen.findByText('Sorry, no breadcrumbs match your search query')
  71. ).toBeInTheDocument();
  72. userEvent.click(screen.getByLabelText('Clear'));
  73. userEvent.type(screen.getByPlaceholderText('Search breadcrumbs'), 'up');
  74. expect(
  75. screen.queryByText('Sorry, no breadcrumbs match your search query')
  76. ).not.toBeInTheDocument();
  77. expect(screen.getAllByText(textWithMarkupMatcher('sup'))).toHaveLength(3);
  78. });
  79. it('should filter crumbs based on crumb level', function () {
  80. render(<Breadcrumbs {...props} />);
  81. userEvent.type(screen.getByPlaceholderText('Search breadcrumbs'), 'war');
  82. // breadcrumbs + filter item
  83. // TODO(Priscila): Filter should not render in the dom if not open
  84. expect(screen.getAllByText(textWithMarkupMatcher('Warning'))).toHaveLength(5);
  85. });
  86. it('should filter crumbs based on crumb category', function () {
  87. render(<Breadcrumbs {...props} />);
  88. userEvent.type(screen.getByPlaceholderText('Search breadcrumbs'), 'error');
  89. expect(screen.getAllByText(textWithMarkupMatcher('error'))).toHaveLength(2);
  90. });
  91. });
  92. describe('render', function () {
  93. it('should display the correct number of crumbs with no filter', function () {
  94. props.data.values = props.data.values.slice(0, 4);
  95. render(<Breadcrumbs {...props} />);
  96. // data.values + virtual crumb
  97. expect(screen.getAllByTestId('crumb')).toHaveLength(4);
  98. expect(screen.getByTestId('last-crumb')).toBeInTheDocument();
  99. });
  100. it('should display the correct number of crumbs with a filter', function () {
  101. props.data.values = props.data.values.slice(0, 4);
  102. render(<Breadcrumbs {...props} />);
  103. const searchInput = screen.getByPlaceholderText('Search breadcrumbs');
  104. userEvent.type(searchInput, 'sup');
  105. expect(screen.queryByTestId('crumb')).not.toBeInTheDocument();
  106. expect(screen.getByTestId('last-crumb')).toBeInTheDocument();
  107. });
  108. it('should not crash if data contains a toString attribute', function () {
  109. // Regression test: A "toString" property in data should not falsely be
  110. // used to coerce breadcrumb data to string. This would cause a TypeError.
  111. const data = {nested: {toString: 'hello'}};
  112. props.data.values = [
  113. {
  114. message: 'sup',
  115. category: 'default',
  116. level: BreadcrumbLevelType.INFO,
  117. type: BreadcrumbType.INFO,
  118. data,
  119. },
  120. ];
  121. render(<Breadcrumbs {...props} />);
  122. // data.values + virtual crumb
  123. expect(screen.getByTestId('crumb')).toBeInTheDocument();
  124. expect(screen.getByTestId('last-crumb')).toBeInTheDocument();
  125. });
  126. });
  127. });