import { render, screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { describe, expect, it, vi } from 'vitest' import { expectNoA11yViolations } from '../test/a11y' import { Button } from './Button' import { DropdownMenu } from './DropdownMenu' import type { DropdownMenuItemData } from './dropdown-menu.tokens' function items(overrides: Partial void>> = {}): DropdownMenuItemData[] { return [ { label: 'Edit', shortcut: '⌘E', onSelect: overrides.edit }, { label: 'Duplicate', onSelect: overrides.duplicate }, { type: 'separator' }, { type: 'checkbox', label: 'Show archived', defaultChecked: false }, { type: 'radio', label: 'Sort by', defaultValue: 'name', items: [{ value: 'name', label: 'Name' }, { value: 'date', label: 'Date' }] }, { type: 'separator' }, { label: 'Delete', color: 'danger', onSelect: overrides.remove }, ] } describe('DropdownMenu', () => { it('opens from the trigger as a menu', async () => { render(Actions} items={items()} />) const trigger = screen.getByRole('button', { name: 'Actions' }) expect(trigger).toHaveAttribute('aria-haspopup', 'menu') await userEvent.click(trigger) const menu = await screen.findByRole('menu') expect(menu).toHaveAccessibleName('Actions') expect(screen.getByRole('menuitem', { name: /Edit/ })).toHaveTextContent('⌘E') expect(screen.getByRole('menuitem', { name: /Edit/ }).querySelector('kbd')).toHaveTextContent('⌘E') }) it('moves with ArrowDown and runs onSelect on Enter', async () => { const duplicate = vi.fn() render(Actions} items={items({ duplicate })} />) screen.getByRole('button', { name: 'Actions' }).focus() await userEvent.keyboard('{Enter}') await screen.findByRole('menu') await waitFor(() => expect(screen.getByRole('menuitem', { name: /Edit/ })).toHaveFocus()) await userEvent.keyboard('{ArrowDown}') expect(screen.getByRole('menuitem', { name: 'Duplicate' })).toHaveFocus() await userEvent.keyboard('{Enter}') expect(duplicate).toHaveBeenCalledOnce() await waitFor(() => expect(screen.queryByRole('menu')).not.toBeInTheDocument()) }) it('runs onSelect on click', async () => { const remove = vi.fn() render(Actions} items={items({ remove })} />) await userEvent.click(screen.getByRole('button', { name: 'Actions' })) await userEvent.click(await screen.findByRole('menuitem', { name: 'Delete' })) expect(remove).toHaveBeenCalledOnce() }) it('toggles checkbox items and selects radio items', async () => { render(Actions} items={items()} />) await userEvent.click(screen.getByRole('button', { name: 'Actions' })) const checkbox = await screen.findByRole('menuitemcheckbox', { name: 'Show archived' }) expect(checkbox).toHaveAttribute('aria-checked', 'false') await userEvent.click(checkbox) expect(checkbox).toHaveAttribute('aria-checked', 'true') expect(screen.getByRole('menu')).toBeInTheDocument() const date = screen.getByRole('menuitemradio', { name: 'Date' }) await userEvent.click(date) expect(date).toHaveAttribute('aria-checked', 'true') expect(screen.getByRole('menuitemradio', { name: 'Name' })).toHaveAttribute('aria-checked', 'false') }) it('closes on Escape and returns focus to the trigger', async () => { render(Actions} items={items()} />) const trigger = screen.getByRole('button', { name: 'Actions' }) await userEvent.click(trigger) await screen.findByRole('menu') await userEvent.keyboard('{Escape}') await waitFor(() => expect(screen.queryByRole('menu')).not.toBeInTheDocument()) await waitFor(() => expect(trigger).toHaveFocus()) }) it('opens a submenu with ArrowRight', async () => { const onSelect = vi.fn() render( Actions} items={[{ type: 'submenu', label: 'Move to', items: [{ label: 'Design team', onSelect }] }]} />, ) await userEvent.click(screen.getByRole('button', { name: 'Actions' })) const sub = await screen.findByRole('menuitem', { name: 'Move to' }) expect(sub).toHaveAttribute('aria-haspopup', 'menu') sub.focus() await userEvent.keyboard('{ArrowRight}') await userEvent.click(await screen.findByRole('menuitem', { name: 'Design team' })) expect(onSelect).toHaveBeenCalledOnce() }) it('builds from compound parts', async () => { const onSelect = vi.fn() render( Options} /> Account Profile Notifications , ) expect(await screen.findByRole('group', { name: 'Account' })).toBeInTheDocument() expect(screen.getByRole('menuitemcheckbox', { name: 'Notifications' })).toHaveAttribute('aria-checked', 'true') await userEvent.click(screen.getByRole('menuitem', { name: 'Profile' })) expect(onSelect).toHaveBeenCalledOnce() }) it('has no axe violations when open', async () => { render( Actions} items={[{ type: 'label', label: 'Project' }, ...items()]} />, ) await screen.findByRole('menu') await expectNoA11yViolations(document.body) }) })