|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { render, screen, within } from '@testing-library/svelte'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { NavDropdown as Subject } from '$lib'; |
| 5 | + |
| 6 | +const versionOptions = [ |
| 7 | + { |
| 8 | + label: 'Version 1', |
| 9 | + detail: '1 day ago', |
| 10 | + description: 'stable', |
| 11 | + href: '/v1', |
| 12 | + }, |
| 13 | + { |
| 14 | + label: 'Version 2', |
| 15 | + detail: '5 hours ago', |
| 16 | + description: 'latest', |
| 17 | + href: '/v2', |
| 18 | + }, |
| 19 | +]; |
| 20 | + |
| 21 | +describe('NavDropdown', () => { |
| 22 | + it('renders a button that controls a menu', () => { |
| 23 | + render(Subject, { |
| 24 | + props: { options: versionOptions, selectedHref: '/v1' }, |
| 25 | + }); |
| 26 | + |
| 27 | + const button = screen.getByRole('button'); |
| 28 | + |
| 29 | + expect(button).toHaveAttribute('aria-haspopup', 'menu'); |
| 30 | + expect(button).toHaveAttribute('aria-expanded', 'false'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('expands the menu on click', async () => { |
| 34 | + const user = userEvent.setup(); |
| 35 | + render(Subject, { |
| 36 | + props: { options: versionOptions, selectedHref: '/v1' }, |
| 37 | + }); |
| 38 | + |
| 39 | + const button = screen.getByRole('button'); |
| 40 | + await user.click(button); |
| 41 | + |
| 42 | + const menu = screen.getByRole('menu'); |
| 43 | + |
| 44 | + expect(menu).toBeInTheDocument(); |
| 45 | + expect(button).toHaveAttribute('aria-expanded', 'true'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('displays option details', async () => { |
| 49 | + const user = userEvent.setup(); |
| 50 | + render(Subject, { |
| 51 | + props: { options: versionOptions, selectedHref: '/v1' }, |
| 52 | + }); |
| 53 | + |
| 54 | + const button = screen.getByRole('button'); |
| 55 | + await user.click(button); |
| 56 | + |
| 57 | + const menuitem = screen.getByRole('menuitem', { name: /Version 1/u }); |
| 58 | + expect(within(menuitem).getByText(/1 day ago/u)).toBeInTheDocument(); |
| 59 | + expect(within(menuitem).getByText('stable')).toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('opens menu with Space when button is focused', async () => { |
| 63 | + const user = userEvent.setup(); |
| 64 | + render(Subject, { |
| 65 | + props: { options: versionOptions, selectedHref: '/v1' }, |
| 66 | + }); |
| 67 | + |
| 68 | + const button = screen.getByRole('button'); |
| 69 | + button.focus(); |
| 70 | + await user.keyboard(' '); |
| 71 | + |
| 72 | + expect(screen.getByRole('menu')).toBeInTheDocument(); |
| 73 | + expect(button).toHaveAttribute('aria-expanded', 'true'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('closes menu with Escape', async () => { |
| 77 | + const user = userEvent.setup(); |
| 78 | + render(Subject, { |
| 79 | + props: { options: versionOptions, selectedHref: '/v1' }, |
| 80 | + }); |
| 81 | + |
| 82 | + const button = screen.getByRole('button'); |
| 83 | + await user.click(button); |
| 84 | + expect(screen.getByRole('menu')).toBeInTheDocument(); |
| 85 | + |
| 86 | + await user.keyboard('{Escape}'); |
| 87 | + |
| 88 | + expect(screen.queryByRole('menu')).not.toBeInTheDocument(); |
| 89 | + expect(button).toHaveAttribute('aria-expanded', 'false'); |
| 90 | + }); |
| 91 | +}); |
0 commit comments