dropdownAutoComplete.spec.jsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import DropdownAutoComplete from 'sentry/components/dropdownAutoComplete';
  3. describe('DropdownAutoComplete', 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('has actor wrapper', function () {
  19. render(
  20. <DropdownAutoComplete items={items}>{() => 'Click Me!'}</DropdownAutoComplete>
  21. );
  22. expect(screen.getByRole('button')).toHaveTextContent('Click Me!');
  23. });
  24. it('does not allow the dropdown to be closed without allowActorToggle', function () {
  25. render(
  26. <DropdownAutoComplete items={items}>{() => 'Click Me!'}</DropdownAutoComplete>
  27. );
  28. const actor = screen.getByRole('button');
  29. // Starts closed
  30. expect(actor).toHaveAttribute('aria-expanded', 'false');
  31. expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
  32. // Clicking once opens the menu
  33. userEvent.click(actor);
  34. expect(actor).toHaveAttribute('aria-expanded', 'true');
  35. expect(screen.getByRole('listbox')).toBeInTheDocument();
  36. // Clicking again does not close the menu
  37. userEvent.click(actor);
  38. expect(actor).toHaveAttribute('aria-expanded', 'true');
  39. expect(screen.getByRole('listbox')).toBeInTheDocument();
  40. });
  41. it('toggles dropdown menu when actor is clicked and allowActorToggle=true', function () {
  42. render(
  43. <DropdownAutoComplete allowActorToggle items={items}>
  44. {() => 'Click Me!'}
  45. </DropdownAutoComplete>
  46. );
  47. const actor = screen.getByRole('button');
  48. // Clicking once opens
  49. userEvent.click(actor);
  50. expect(actor).toHaveAttribute('aria-expanded', 'true');
  51. expect(screen.getByRole('listbox')).toBeInTheDocument();
  52. // Clicking again closes
  53. userEvent.click(actor);
  54. expect(actor).toHaveAttribute('aria-expanded', 'false');
  55. expect(screen.queryByRole('listbox')).not.toBeInTheDocument();
  56. });
  57. });