import type {ComponentProps} from 'react'; import { render, screen, userEvent, waitForElementToBeRemoved, } from 'sentry-test/reactTestingLibrary'; import DeprecatedDropdownMenu from 'sentry/components/deprecatedDropdownMenu'; jest.useFakeTimers(); describe('dropdownMenuDeprecated', function () { const DeprecatedDropdownImplementation = ( props: Partial> = {} ) => { return ( {({getRootProps, getActorProps, getMenuProps, isOpen}) => ( {isOpen && (
  • Dropdown Menu Item 1
)}
)}
); }; it('renders', function () { const {container} = render(); expect(container).toSnapshot(); }); it('can toggle dropdown menu with actor', function () { render(); userEvent.click(screen.getByRole('button')); expect(screen.getByRole('listbox')).toBeInTheDocument(); userEvent.click(screen.getByRole('button')); expect(screen.queryByRole('listbox')).not.toBeInTheDocument(); }); it('closes dropdown when clicking on anything in menu', function () { render(); userEvent.click(screen.getByRole('button')); userEvent.click(screen.getByRole('listitem')); expect(screen.queryByRole('listbox')).not.toBeInTheDocument(); }); it('closes dropdown when clicking outside of menu', async function () { render(
); userEvent.click(screen.getByRole('button')); userEvent.click(screen.getByTestId('outside-element')); await waitForElementToBeRemoved(() => screen.queryByRole('listbox')); }); it('closes dropdown when pressing escape', function () { render(); userEvent.click(screen.getByRole('button')); userEvent.keyboard('{Escape}'); expect(screen.queryByRole('listbox')).not.toBeInTheDocument(); }); it('ignores "Escape" key if `closeOnEscape` is false', function () { render(); userEvent.click(screen.getByRole('button')); userEvent.keyboard('{Escape}'); expect(screen.getByRole('listbox')).toBeInTheDocument(); }); it('keeps dropdown open when clicking on anything in menu with `keepMenuOpen` prop', function () { render(); userEvent.click(screen.getByRole('button')); userEvent.click(screen.getByRole('listitem')); expect(screen.getByRole('listbox')).toBeInTheDocument(); }); it('render prop getters all extend props and call original onClick handlers', function () { const rootClick = jest.fn(); const actorClick = jest.fn(); const menuClick = jest.fn(); render( {({getRootProps, getActorProps, getMenuProps, isOpen}) => ( {isOpen && (
  • Dropdown Menu Item 1
)}
)}
); expect(screen.queryByRole('listbox')).not.toBeInTheDocument(); userEvent.click(screen.getByTestId('root')); expect(rootClick).toHaveBeenCalled(); userEvent.click(screen.getByTestId('actor')); expect(actorClick).toHaveBeenCalled(); userEvent.click(screen.getByTestId('menu')); expect(menuClick).toHaveBeenCalled(); expect(screen.queryByRole('listbox')).toBeInTheDocument(); }); it('always rendered menus should attach document event listeners only when opened', function () { const addSpy = jest.spyOn(document, 'addEventListener'); const removeSpy = jest.spyOn(document, 'removeEventListener'); render( {({getRootProps, getActorProps, getMenuProps}) => (
  • Dropdown Menu Item 1
)}
); // Make sure this is only called when menu is open expect(addSpy).not.toHaveBeenCalled(); userEvent.click(screen.getByRole('button')); expect(addSpy).toHaveBeenCalled(); expect(removeSpy).not.toHaveBeenCalled(); userEvent.click(screen.getByRole('button')); expect(removeSpy).toHaveBeenCalled(); addSpy.mockRestore(); removeSpy.mockRestore(); }); it('does not close nested dropdown on actor clicks', function () { render( {({getRootProps, getActorProps, getMenuProps}) => ( {
  • Dropdown Menu Item 1
}
)}
); userEvent.hover(screen.getByRole('button')); expect(screen.getByTestId('menu-item')).toBeInTheDocument(); userEvent.click(screen.getByRole('button')); // Should still be visible. expect(screen.getByTestId('menu-item')).toBeInTheDocument(); }); });