dropdownAutoCompleteMenu.spec.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import DropdownAutoCompleteMenu from 'sentry/components/dropdownAutoComplete/menu';
  3. describe('DropdownAutoCompleteMenu', function () {
  4. const items = [
  5. {
  6. value: 'apple',
  7. label: <div>Apple</div>,
  8. },
  9. {
  10. value: 'bacon',
  11. label: <div>Bacon</div>,
  12. },
  13. {
  14. value: 'corn',
  15. label: <div>Corn</div>,
  16. },
  17. ];
  18. it('renders without a group', function () {
  19. const {container} = render(
  20. <DropdownAutoCompleteMenu isOpen items={items}>
  21. {() => 'Click Me!'}
  22. </DropdownAutoCompleteMenu>
  23. );
  24. expect(container).toSnapshot();
  25. });
  26. it('renders with a group', function () {
  27. const {container} = render(
  28. <DropdownAutoCompleteMenu
  29. isOpen
  30. items={[
  31. {
  32. id: 'countries',
  33. value: 'countries',
  34. label: 'countries',
  35. items: [
  36. {
  37. value: 'new zealand',
  38. label: <div>New Zealand</div>,
  39. },
  40. {
  41. value: 'australia',
  42. label: <div>Australia</div>,
  43. },
  44. ],
  45. },
  46. ]}
  47. >
  48. {() => 'Click Me!'}
  49. </DropdownAutoCompleteMenu>
  50. );
  51. expect(container).toSnapshot();
  52. });
  53. it('can select an item by clicking', function () {
  54. const mock = jest.fn();
  55. const countries = [
  56. {
  57. value: 'new zealand',
  58. label: <div>New Zealand</div>,
  59. },
  60. {
  61. value: 'australia',
  62. label: <div>Australia</div>,
  63. },
  64. ];
  65. render(
  66. <DropdownAutoCompleteMenu
  67. isOpen
  68. items={[
  69. {
  70. id: 'countries',
  71. value: 'countries',
  72. label: 'countries',
  73. items: countries,
  74. },
  75. ]}
  76. onSelect={mock}
  77. >
  78. {({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
  79. </DropdownAutoCompleteMenu>
  80. );
  81. userEvent.click(screen.getByRole('option', {name: 'Australia'}));
  82. expect(mock).toHaveBeenCalledTimes(1);
  83. expect(mock).toHaveBeenCalledWith(
  84. {index: 1, ...countries[1]},
  85. {highlightedIndex: 1, inputValue: '', isOpen: true, selectedItem: undefined},
  86. expect.anything()
  87. );
  88. });
  89. it('shows empty message when there are no items', function () {
  90. render(
  91. <DropdownAutoCompleteMenu
  92. items={[]}
  93. emptyMessage="No items!"
  94. emptyHidesInput
  95. isOpen
  96. >
  97. {({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
  98. </DropdownAutoCompleteMenu>
  99. );
  100. expect(screen.getByText('No items!')).toBeInTheDocument();
  101. // No input because there are no items
  102. expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
  103. });
  104. it('shows default empty results message when there are no items found in search', function () {
  105. render(
  106. <DropdownAutoCompleteMenu isOpen items={items} emptyMessage="No items!">
  107. {({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
  108. </DropdownAutoCompleteMenu>
  109. );
  110. userEvent.type(screen.getByRole('textbox'), 'U-S-A');
  111. expect(screen.getByText('No items! found')).toBeInTheDocument();
  112. });
  113. it('overrides default empty results message', function () {
  114. render(
  115. <DropdownAutoCompleteMenu
  116. isOpen
  117. items={items}
  118. emptyMessage="No items!"
  119. noResultsMessage="No search results"
  120. >
  121. {({selectedItem}) => (selectedItem ? selectedItem.label : 'Click me!')}
  122. </DropdownAutoCompleteMenu>
  123. );
  124. userEvent.type(screen.getByRole('textbox'), 'U-S-A');
  125. expect(screen.getByText('No search results')).toBeInTheDocument();
  126. expect(screen.queryByRole('option')).not.toBeInTheDocument();
  127. });
  128. it('hides filter with `hideInput` prop', function () {
  129. render(
  130. <DropdownAutoCompleteMenu isOpen items={items} hideInput>
  131. {() => 'Click Me!'}
  132. </DropdownAutoCompleteMenu>
  133. );
  134. expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
  135. });
  136. it('filters using a value from prop instead of input', function () {
  137. render(
  138. <DropdownAutoCompleteMenu isOpen items={items} filterValue="Apple">
  139. {() => 'Click Me!'}
  140. </DropdownAutoCompleteMenu>
  141. );
  142. userEvent.type(screen.getByRole('textbox'), 'U-S-A');
  143. expect(screen.getByRole('option', {name: 'Apple'})).toBeInTheDocument();
  144. expect(screen.queryByRole('option', {name: 'Corn'})).not.toBeInTheDocument();
  145. expect(screen.queryByRole('option', {name: 'Bacon'})).not.toBeInTheDocument();
  146. });
  147. });