breadcrumbs.spec.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {fireEvent, mountWithTheme, screen} from 'sentry-test/reactTestingLibrary';
  3. import {getAllByTextContent} from 'sentry-test/utils';
  4. import Breadcrumbs from 'app/components/events/interfaces/breadcrumbs';
  5. import {BreadcrumbLevelType, BreadcrumbType} from 'app/types/breadcrumbs';
  6. import {EntryType} from 'app/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. mountWithTheme(<Breadcrumbs {...props} />);
  68. const searchInput = screen.getByPlaceholderText('Search breadcrumbs');
  69. fireEvent.change(searchInput, {target: {value: 'hi'}});
  70. expect(
  71. await screen.findByText('Sorry, no breadcrumbs match your search query')
  72. ).toBeInTheDocument();
  73. fireEvent.change(searchInput, {target: {value: 'up'}});
  74. expect(
  75. screen.queryByText('Sorry, no breadcrumbs match your search query')
  76. ).not.toBeInTheDocument();
  77. expect(getAllByTextContent('sup')).toHaveLength(3);
  78. });
  79. it('should filter crumbs based on crumb level', function () {
  80. mountWithTheme(<Breadcrumbs {...props} />);
  81. const searchInput = screen.getByPlaceholderText('Search breadcrumbs');
  82. fireEvent.change(searchInput, {target: {value: 'war'}});
  83. // breadcrumbs + filter item
  84. // TODO(Priscila): Filter should not render in the dom if not open
  85. expect(getAllByTextContent('Warning')).toHaveLength(6);
  86. });
  87. it('should filter crumbs based on crumb category', function () {
  88. mountWithTheme(<Breadcrumbs {...props} />);
  89. const searchInput = screen.getByPlaceholderText('Search breadcrumbs');
  90. fireEvent.change(searchInput, {target: {value: 'error'}});
  91. expect(getAllByTextContent('error')).toHaveLength(2);
  92. });
  93. });
  94. describe('render', function () {
  95. it('should display the correct number of crumbs with no filter', function () {
  96. props.data.values = props.data.values.slice(0, 4);
  97. mountWithTheme(<Breadcrumbs {...props} />);
  98. // data.values + virtual crumb
  99. expect(screen.getAllByTestId('crumb')).toHaveLength(4);
  100. expect(screen.getByTestId('last-crumb')).toBeInTheDocument();
  101. });
  102. it('should display the correct number of crumbs with a filter', function () {
  103. props.data.values = props.data.values.slice(0, 4);
  104. mountWithTheme(<Breadcrumbs {...props} />);
  105. const searchInput = screen.getByPlaceholderText('Search breadcrumbs');
  106. fireEvent.change(searchInput, {target: {value: 'sup'}});
  107. expect(screen.queryByTestId('crumb')).not.toBeInTheDocument();
  108. expect(screen.getByTestId('last-crumb')).toBeInTheDocument();
  109. });
  110. it('should not crash if data contains a toString attribute', function () {
  111. // Regression test: A "toString" property in data should not falsely be
  112. // used to coerce breadcrumb data to string. This would cause a TypeError.
  113. const data = {nested: {toString: 'hello'}};
  114. props.data.values = [
  115. {
  116. message: 'sup',
  117. category: 'default',
  118. level: BreadcrumbLevelType.INFO,
  119. type: BreadcrumbType.INFO,
  120. data,
  121. },
  122. ];
  123. mountWithTheme(<Breadcrumbs {...props} />);
  124. // data.values + virtual crumb
  125. expect(screen.getByTestId('crumb')).toBeInTheDocument();
  126. expect(screen.getByTestId('last-crumb')).toBeInTheDocument();
  127. });
  128. });
  129. });