Restructure the repo into npm workspaces so the component kit publishes as its own package, like MUI and shadcn/ui: - packages/ui: @openkhalix/tansy-ui (published), with components, stories, tests, tokens and the library build - packages/brand: private logo and catalogue data shared by both apps - apps/site: the landing page - apps/docs: Storybook The package: - MIT licence (copyright OpenKhalix) - per-component ES modules and type declarations - "use client" on component modules so they import into Next.js Server Components; helpers stay server-safe - tokens.css for Tailwind v4 apps, and a prebuilt styles.css for apps without Tailwind - lucide-react 1.x, whose types compile against current React 19 types The apps resolve the package through a "source" export condition, so they hot-reload component edits, while outside users get dist/. scripts/test-package.mjs packs the package and installs the tarball into Vite + Tailwind (strict types), plain React and Next.js apps; each must build and render correctly. Gitea workflows run CI on push and publish to npm when a vX.Y.Z tag matching the package version is pushed.
129 lines
5.8 KiB
TypeScript
129 lines
5.8 KiB
TypeScript
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<Record<string, () => 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(<DropdownMenu trigger={<Button>Actions</Button>} 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(<DropdownMenu trigger={<Button>Actions</Button>} 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(<DropdownMenu trigger={<Button>Actions</Button>} 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(<DropdownMenu trigger={<Button>Actions</Button>} 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(<DropdownMenu trigger={<Button>Actions</Button>} 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(
|
|
<DropdownMenu
|
|
trigger={<Button>Actions</Button>}
|
|
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(
|
|
<DropdownMenu.Root defaultOpen>
|
|
<DropdownMenu.Trigger render={<Button>Options</Button>} />
|
|
<DropdownMenu.Content>
|
|
<DropdownMenu.Group>
|
|
<DropdownMenu.Label>Account</DropdownMenu.Label>
|
|
<DropdownMenu.Item onSelect={onSelect}>Profile</DropdownMenu.Item>
|
|
</DropdownMenu.Group>
|
|
<DropdownMenu.Separator />
|
|
<DropdownMenu.CheckboxItem defaultChecked>Notifications</DropdownMenu.CheckboxItem>
|
|
</DropdownMenu.Content>
|
|
</DropdownMenu.Root>,
|
|
)
|
|
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(
|
|
<DropdownMenu
|
|
defaultOpen
|
|
trigger={<Button>Actions</Button>}
|
|
items={[{ type: 'label', label: 'Project' }, ...items()]}
|
|
/>,
|
|
)
|
|
await screen.findByRole('menu')
|
|
await expectNoA11yViolations(document.body)
|
|
})
|
|
})
|