import { describe, expect, it } from 'vitest'; import type { ComponentProps } from 'svelte'; import { render, screen, waitFor, within } from '@testing-library/svelte'; import userEvent from '@testing-library/user-event'; import Subject from '../nav-dropdown.svelte'; const renderSubject = (props?: Partial>) => { return render(Subject, { options: [ { label: '2', detail: 'Jan 2', description: 'latest', href: '/second-version', selectedText: "Look at me I'm the latest version", }, { label: '1', detail: 'Jan 1', description: 'stable', href: '/first-version', }, ], selectedHref: undefined, ...props, }); }; const expectFocus = async (element: HTMLElement | undefined) => { await waitFor(() => expect(element).toHaveFocus(), { timeout: 200 }); }; describe('NavDropdown', () => { it('renders a button that controls a menu', () => { renderSubject({ selectedHref: '/first-version' }); const button = screen.getByRole('button'); expect(button).toHaveAccessibleName(/^1 \(Jan 1\)$/iu); expect(button).toHaveAttribute('aria-haspopup', 'menu'); expect(button).toHaveAttribute('aria-expanded', 'false'); }); it('renders selected text of an item in the button, if specified', () => { renderSubject({ selectedHref: '/second-version' }); const button = screen.getByRole('button'); expect(button).toHaveAccessibleName(/look at me i'm the latest version/iu); }); it('expands the menu on click', async () => { const user = userEvent.setup(); renderSubject(); const button = screen.getByRole('button'); await user.click(button); const menu = screen.getByRole('menu'); expect(menu).toHaveAttribute( 'id', expect.stringMatching(/^nav-dropdown/iu) ); expect(button).toHaveAttribute('aria-expanded', 'true'); expect(button).toHaveAttribute('aria-controls', menu.id); }); it('closes the menu on escape', async () => { const user = userEvent.setup(); renderSubject(); const button = screen.getByRole('button'); await user.click(button); const menu = screen.getByRole('menu'); await user.keyboard('{Escape}'); expect(menu).not.toBeInTheDocument(); }); it('displays option details', async () => { const user = userEvent.setup(); renderSubject(); const button = screen.getByRole('button'); await user.click(button); const menu = screen.getByRole('menu'); const items = within(menu).getAllByRole('menuitem'); expect(items).toHaveLength(2); expect(items[0]).toHaveTextContent(/2 \(Jan 2\) latest/iu); expect(items[0]).toHaveAttribute('href', '/second-version'); expect(items[1]).toHaveTextContent(/1 \(Jan 1\) stable/iu); expect(items[1]).toHaveAttribute('href', '/first-version'); }); it('marks current page', async () => { const user = userEvent.setup(); renderSubject({ selectedHref: '/first-version' }); const button = screen.getByRole('button'); await user.click(button); const item = screen.getByRole('menuitem', { name: /1/iu }); expect(item).toHaveAttribute('aria-current', 'page'); }); it('focuses the selected link on open and the button on close', async () => { const user = userEvent.setup(); renderSubject({ selectedHref: '/first-version' }); const button = screen.getByRole('button'); await user.click(button); const firstVersion = screen.getByRole('menuitem', { name: /1/iu }); await expectFocus(firstVersion); await user.keyboard('{Escape}'); expect(button).toHaveFocus(); }); it('uses arrow keys to navigate through options', async () => { const user = userEvent.setup(); renderSubject({ selectedHref: '/second-version' }); const button = screen.getByRole('button'); await user.click(button); const items = screen.getAllByRole('menuitem'); await expectFocus(items[0]); await user.keyboard('{ArrowDown}'); await expectFocus(items[1]); await user.keyboard('{ArrowDown}'); await expectFocus(items[0]); await user.keyboard('{ArrowUp}'); await expectFocus(items[1]); await user.keyboard('{ArrowUp}'); await expectFocus(items[0]); }); it('closes the menu and returns focus to the button on Tab', async () => { const user = userEvent.setup(); renderSubject({ selectedHref: '/first-version' }); const button = screen.getByRole('button'); await user.click(button); const menu = screen.getByRole('menu'); await user.keyboard('{Tab}'); expect(menu).not.toBeInTheDocument(); expect(button).toHaveFocus(); }); });