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.
88 lines
3.9 KiB
TypeScript
88 lines
3.9 KiB
TypeScript
import { render, screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import { expectNoA11yViolations } from '../test/a11y'
|
|
import { Accordion, type AccordionItem } from './Accordion'
|
|
|
|
const items: AccordionItem[] = [
|
|
{ value: 'a', title: 'Billing', content: 'Billing answer' },
|
|
{ value: 'b', title: 'Cancelling', content: 'Cancel answer' },
|
|
{ value: 'c', title: 'SSO', content: 'SSO answer', disabled: true },
|
|
]
|
|
|
|
describe('Accordion', () => {
|
|
it('toggles a section and keeps aria-controls pointing at an existing panel', async () => {
|
|
const onValueChange = vi.fn()
|
|
render(<Accordion items={items} onValueChange={onValueChange} />)
|
|
const trigger = screen.getByRole('button', { name: 'Billing' })
|
|
expect(trigger).toHaveAttribute('aria-expanded', 'false')
|
|
const controlsExisting = () => {
|
|
const id = trigger.getAttribute('aria-controls')
|
|
// Base UI only sets aria-controls while the panel is open; when set, it must resolve.
|
|
return id == null || document.getElementById(id) != null
|
|
}
|
|
expect(controlsExisting()).toBe(true)
|
|
|
|
await userEvent.click(trigger)
|
|
expect(trigger).toHaveAttribute('aria-expanded', 'true')
|
|
expect(onValueChange).toHaveBeenLastCalledWith(['a'])
|
|
expect(document.getElementById(trigger.getAttribute('aria-controls')!)).toHaveTextContent('Billing answer')
|
|
|
|
await userEvent.click(trigger)
|
|
expect(trigger).toHaveAttribute('aria-expanded', 'false')
|
|
expect(controlsExisting()).toBe(true)
|
|
})
|
|
|
|
it('closes the other section unless multiple', async () => {
|
|
const { unmount } = render(<Accordion items={items} defaultValue={['a']} />)
|
|
await userEvent.click(screen.getByRole('button', { name: 'Cancelling' }))
|
|
expect(screen.getByRole('button', { name: 'Billing' })).toHaveAttribute('aria-expanded', 'false')
|
|
unmount()
|
|
|
|
render(<Accordion items={items} defaultValue={['a']} multiple />)
|
|
await userEvent.click(screen.getByRole('button', { name: 'Cancelling' }))
|
|
expect(screen.getByRole('button', { name: 'Billing' })).toHaveAttribute('aria-expanded', 'true')
|
|
expect(screen.getByRole('button', { name: 'Cancelling' })).toHaveAttribute('aria-expanded', 'true')
|
|
})
|
|
|
|
it('works with the keyboard', async () => {
|
|
render(<Accordion items={items} />)
|
|
await userEvent.tab()
|
|
expect(screen.getByRole('button', { name: 'Billing' })).toHaveFocus()
|
|
await userEvent.keyboard('{Enter}')
|
|
expect(screen.getByRole('button', { name: 'Billing' })).toHaveAttribute('aria-expanded', 'true')
|
|
await userEvent.tab()
|
|
await userEvent.keyboard(' ')
|
|
expect(screen.getByRole('button', { name: 'Cancelling' })).toHaveAttribute('aria-expanded', 'true')
|
|
})
|
|
|
|
it('renders headings at the requested level', () => {
|
|
const { rerender } = render(<Accordion items={items} />)
|
|
expect(screen.getAllByRole('heading', { level: 3 })).toHaveLength(3)
|
|
rerender(<Accordion items={items} headingLevel={5} />)
|
|
expect(screen.getAllByRole('heading', { level: 5 })).toHaveLength(3)
|
|
expect(screen.getByRole('heading', { level: 5, name: 'Billing' })).toContainElement(
|
|
screen.getByRole('button', { name: 'Billing' }),
|
|
)
|
|
})
|
|
|
|
it('disables items', () => {
|
|
render(<Accordion items={items} />)
|
|
const sso = screen.getByRole('button', { name: 'SSO' })
|
|
expect(sso).toHaveAttribute('aria-disabled', 'true')
|
|
})
|
|
|
|
it('works controlled', async () => {
|
|
const onValueChange = vi.fn()
|
|
render(<Accordion items={items} value={['b']} onValueChange={onValueChange} />)
|
|
await userEvent.click(screen.getByRole('button', { name: 'Billing' }))
|
|
expect(onValueChange).toHaveBeenCalledWith(['a'])
|
|
expect(screen.getByRole('button', { name: 'Cancelling' })).toHaveAttribute('aria-expanded', 'true')
|
|
})
|
|
|
|
it('has no axe violations', async () => {
|
|
const { container } = render(<Accordion items={items} defaultValue={['a']} variant="separated" />)
|
|
await expectNoA11yViolations(container)
|
|
})
|
|
})
|