searchBarAction.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import Level from 'sentry/components/events/interfaces/breadcrumbs/breadcrumb/level';
  3. import Type from 'sentry/components/events/interfaces/breadcrumbs/breadcrumb/type';
  4. import SearchBarAction from 'sentry/components/events/interfaces/searchBarAction';
  5. import {BreadcrumbLevelType, BreadcrumbType} from 'sentry/types/breadcrumbs';
  6. const options: NonNullable<
  7. React.ComponentProps<typeof SearchBarAction>['filterOptions']
  8. > = [
  9. {
  10. value: 'types',
  11. label: 'Types',
  12. options: [
  13. {
  14. value: BreadcrumbType.HTTP,
  15. label: 'HTTP request',
  16. leadingItems: <Type color="green300" type={BreadcrumbType.HTTP} />,
  17. },
  18. {
  19. value: BreadcrumbType.TRANSACTION,
  20. label: 'Transaction',
  21. leadingItems: <Type color="pink300" type={BreadcrumbType.TRANSACTION} />,
  22. },
  23. {
  24. value: BreadcrumbType.UI,
  25. label: 'User Action',
  26. leadingItems: <Type color="purple300" type={BreadcrumbType.UI} />,
  27. },
  28. {
  29. value: BreadcrumbType.NAVIGATION,
  30. label: 'Navigation',
  31. leadingItems: <Type color="green300" type={BreadcrumbType.NAVIGATION} />,
  32. },
  33. {
  34. value: BreadcrumbType.DEBUG,
  35. label: 'Debug',
  36. leadingItems: <Type color="purple300" type={BreadcrumbType.DEBUG} />,
  37. },
  38. {
  39. value: BreadcrumbType.ERROR,
  40. label: 'Error',
  41. leadingItems: <Type color="red300" type={BreadcrumbType.ERROR} />,
  42. },
  43. ],
  44. },
  45. {
  46. value: 'levels',
  47. label: 'Levels',
  48. options: [
  49. {
  50. value: BreadcrumbLevelType.INFO,
  51. label: 'info',
  52. leadingItems: <Level level={BreadcrumbLevelType.INFO} />,
  53. },
  54. {
  55. value: BreadcrumbLevelType.ERROR,
  56. label: 'error',
  57. leadingItems: <Level level={BreadcrumbLevelType.ERROR} />,
  58. },
  59. ],
  60. },
  61. ];
  62. describe('SearchBarAction', () => {
  63. let handleFilter;
  64. beforeEach(() => {
  65. handleFilter = jest.fn();
  66. });
  67. it('default render', async () => {
  68. render(
  69. <SearchBarAction
  70. filterOptions={options}
  71. onFilterChange={handleFilter}
  72. onChange={() => null}
  73. placeholder=""
  74. query=""
  75. />
  76. );
  77. const filterDropdownMenu = screen.getByText('Filter By');
  78. await userEvent.click(filterDropdownMenu);
  79. // Types
  80. expect(screen.getByText('Types')).toBeInTheDocument();
  81. expect(screen.getByText('HTTP request')).toBeInTheDocument();
  82. expect(screen.getByText('Transaction')).toBeInTheDocument();
  83. expect(screen.getByText('User Action')).toBeInTheDocument();
  84. expect(screen.getByText('Navigation')).toBeInTheDocument();
  85. expect(screen.getByText('Debug')).toBeInTheDocument();
  86. expect(screen.getAllByText('Error')[0]).toBeInTheDocument();
  87. // Levels
  88. expect(screen.getByText('Levels')).toBeInTheDocument();
  89. expect(screen.getByText('info')).toBeInTheDocument();
  90. expect(screen.getAllByText('Error')[1]).toBeInTheDocument();
  91. });
  92. it('Without Options', async () => {
  93. render(
  94. <SearchBarAction
  95. filterOptions={[]}
  96. onFilterChange={handleFilter}
  97. onChange={() => null}
  98. placeholder=""
  99. query=""
  100. />
  101. );
  102. expect(await screen.findByTestId('input-trailing-items')).toBeInTheDocument();
  103. expect(screen.queryByText('Types')).not.toBeInTheDocument();
  104. expect(screen.queryByText('Levels')).not.toBeInTheDocument();
  105. });
  106. it('With Option Type only', async () => {
  107. const typeOptions = options[0];
  108. render(
  109. <SearchBarAction
  110. filterOptions={[typeOptions]}
  111. onFilterChange={handleFilter}
  112. onChange={() => null}
  113. placeholder=""
  114. query=""
  115. />
  116. );
  117. const filterDropdownMenu = screen.getByText('Filter By');
  118. await userEvent.click(filterDropdownMenu);
  119. // Header
  120. expect(screen.getByText('Types')).toBeInTheDocument();
  121. expect(screen.queryByText('Levels')).not.toBeInTheDocument();
  122. // List Items
  123. expect(screen.getByText('HTTP request')).toBeInTheDocument();
  124. expect(screen.getByText('Transaction')).toBeInTheDocument();
  125. expect(screen.getByText('User Action')).toBeInTheDocument();
  126. expect(screen.getByText('Navigation')).toBeInTheDocument();
  127. expect(screen.getByText('Debug')).toBeInTheDocument();
  128. expect(screen.getAllByText('Error')[0]).toBeInTheDocument();
  129. const httpRequestItem = screen.getByText('HTTP request');
  130. await userEvent.click(httpRequestItem);
  131. const httpRequestOption = ('options' in typeOptions ? typeOptions.options : []).find(
  132. opt => opt.label === 'HTTP request'
  133. );
  134. expect(handleFilter).toHaveBeenCalledWith([httpRequestOption]);
  135. });
  136. it('With Option Level only', async () => {
  137. const levelOptions = options[1];
  138. render(
  139. <SearchBarAction
  140. filterOptions={[levelOptions]}
  141. onFilterChange={handleFilter}
  142. onChange={() => null}
  143. placeholder=""
  144. query=""
  145. />
  146. );
  147. const filterDropdownMenu = screen.getByText('Filter By');
  148. await userEvent.click(filterDropdownMenu);
  149. // Header
  150. expect(screen.getByText('Levels')).toBeInTheDocument();
  151. expect(screen.queryByText('Types')).not.toBeInTheDocument();
  152. // List Items
  153. expect(screen.getByText('info')).toBeInTheDocument();
  154. expect(screen.getByText('error')).toBeInTheDocument();
  155. // Check Item
  156. const infoItem = screen.getByText('info');
  157. await userEvent.click(infoItem);
  158. const infoOption = ('options' in levelOptions ? levelOptions.options : []).find(
  159. opt => opt.label === 'info'
  160. );
  161. expect(handleFilter).toHaveBeenCalledWith([infoOption]);
  162. });
  163. });