Package Tansy UI as @openkhalix/tansy-ui in a monorepo (v0.4.0)
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.
This commit is contained in:
1 parent
23e67006d3
commit
9255b255a6
214 files changed
+1521
-389
No files matched your search
@@ -0,0 +1,161 @@
|
||||
import { render, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useState } from 'react'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { expectNoA11yViolations } from '../test/a11y'
|
||||
import { Combobox } from './Combobox'
|
||||
import type { SelectOption } from './select.tokens'
|
||||
|
||||
const countries: SelectOption[] = [
|
||||
{ value: 'ca', label: 'Canada' },
|
||||
{ value: 'de', label: 'Germany' },
|
||||
{ value: 'fr', label: 'France' },
|
||||
{ value: 'gb', label: 'United Kingdom' },
|
||||
{ value: 'us', label: 'United States' },
|
||||
]
|
||||
|
||||
describe('Combobox', () => {
|
||||
it('links the label to the input', () => {
|
||||
render(<Combobox label="Country" options={countries} />)
|
||||
expect(screen.getByRole('combobox', { name: 'Country' })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('filters as you type and shows the empty state', async () => {
|
||||
render(<Combobox label="Country" options={countries} emptyText="No countries found" />)
|
||||
const input = screen.getByRole('combobox', { name: 'Country' })
|
||||
await userEvent.type(input, 'united')
|
||||
const listbox = await screen.findByRole('listbox')
|
||||
expect(within(listbox).getAllByRole('option').map((o) => o.textContent)).toEqual([
|
||||
'United Kingdom',
|
||||
'United States',
|
||||
])
|
||||
await userEvent.type(input, 'zzz')
|
||||
expect(await screen.findByText('No countries found')).toBeInTheDocument()
|
||||
expect(screen.queryAllByRole('option')).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('chooses with the keyboard and points aria-activedescendant at the highlighted option', async () => {
|
||||
const onValueChange = vi.fn()
|
||||
render(<Combobox label="Country" options={countries} onValueChange={onValueChange} />)
|
||||
const input = screen.getByRole('combobox', { name: 'Country' })
|
||||
input.focus()
|
||||
await userEvent.keyboard('{ArrowDown}')
|
||||
await screen.findByRole('listbox')
|
||||
await userEvent.keyboard('{ArrowDown}')
|
||||
const active = input.getAttribute('aria-activedescendant')
|
||||
expect(active).toBeTruthy()
|
||||
expect(document.getElementById(active!)).toHaveAttribute('role', 'option')
|
||||
await userEvent.keyboard('{Enter}')
|
||||
expect(onValueChange).toHaveBeenCalledTimes(1)
|
||||
expect(typeof onValueChange.mock.calls[0][0]).toBe('string')
|
||||
expect(input).toHaveFocus()
|
||||
})
|
||||
|
||||
it('closes on Escape and keeps focus in the input', async () => {
|
||||
render(<Combobox label="Country" options={countries} />)
|
||||
const input = screen.getByRole('combobox', { name: 'Country' })
|
||||
await userEvent.click(input)
|
||||
await screen.findByRole('listbox')
|
||||
await userEvent.keyboard('{Escape}')
|
||||
await waitFor(() => expect(input).toHaveAttribute('aria-expanded', 'false'))
|
||||
expect(input).toHaveFocus()
|
||||
})
|
||||
|
||||
it('works uncontrolled and shows the chosen label', async () => {
|
||||
render(<Combobox label="Country" options={countries} defaultValue="de" />)
|
||||
const input = screen.getByRole('combobox', { name: 'Country' })
|
||||
expect(input).toHaveValue('Germany')
|
||||
await userEvent.clear(input)
|
||||
await userEvent.type(input, 'Fra')
|
||||
await userEvent.click(await screen.findByRole('option', { name: 'France' }))
|
||||
expect(input).toHaveValue('France')
|
||||
})
|
||||
|
||||
it('works controlled', async () => {
|
||||
function Controlled() {
|
||||
const [value, setValue] = useState<string | null>('ca')
|
||||
return (
|
||||
<>
|
||||
<Combobox label="Country" options={countries} value={value} onValueChange={setValue} />
|
||||
<p data-testid="value">{String(value)}</p>
|
||||
</>
|
||||
)
|
||||
}
|
||||
render(<Controlled />)
|
||||
const input = screen.getByRole('combobox', { name: 'Country' })
|
||||
expect(input).toHaveValue('Canada')
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Country' }))
|
||||
await userEvent.click(await screen.findByRole('option', { name: 'United States' }))
|
||||
expect(screen.getByTestId('value')).toHaveTextContent('us')
|
||||
})
|
||||
|
||||
it('clears with a labelled button', async () => {
|
||||
const onValueChange = vi.fn()
|
||||
render(<Combobox label="Country" clearable options={countries} defaultValue="gb" onValueChange={onValueChange} />)
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Clear selection' }))
|
||||
expect(onValueChange).toHaveBeenLastCalledWith(null)
|
||||
expect(screen.getByRole('combobox', { name: 'Country' })).toHaveValue('')
|
||||
})
|
||||
|
||||
it('shows multiple values as chips; Backspace removes the last one', async () => {
|
||||
const onValueChange = vi.fn()
|
||||
render(
|
||||
<Combobox
|
||||
label="Countries"
|
||||
multiple
|
||||
options={countries}
|
||||
defaultValue={['ca', 'de']}
|
||||
onValueChange={onValueChange}
|
||||
/>,
|
||||
)
|
||||
expect(screen.getByRole('button', { name: 'Remove Canada' })).toBeInTheDocument()
|
||||
expect(screen.getByRole('button', { name: 'Remove Germany' })).toBeInTheDocument()
|
||||
const input = screen.getByRole('combobox', { name: 'Countries' })
|
||||
await userEvent.click(input)
|
||||
await userEvent.click(await screen.findByRole('option', { name: 'France' }))
|
||||
expect(onValueChange).toHaveBeenLastCalledWith(['ca', 'de', 'fr'])
|
||||
input.focus()
|
||||
await userEvent.keyboard('{Backspace}')
|
||||
expect(onValueChange).toHaveBeenLastCalledWith(['ca', 'de'])
|
||||
await userEvent.keyboard('{Escape}')
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Remove Canada' }))
|
||||
expect(onValueChange).toHaveBeenLastCalledWith(['de'])
|
||||
})
|
||||
|
||||
it('reports typing through onInputValueChange and shows loading', async () => {
|
||||
const onInputValueChange = vi.fn()
|
||||
render(
|
||||
<Combobox label="Employee" options={[]} loading filterOptions={false} onInputValueChange={onInputValueChange} />,
|
||||
)
|
||||
const input = screen.getByRole('combobox', { name: 'Employee' })
|
||||
await userEvent.type(input, 'Ja')
|
||||
expect(onInputValueChange).toHaveBeenLastCalledWith('Ja')
|
||||
expect(await screen.findByText('Loading…')).toBeInTheDocument()
|
||||
expect(screen.queryByText('No results')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('announces the description, and the error when invalid', () => {
|
||||
const { rerender } = render(<Combobox label="Country" description="Where you pay tax." options={countries} />)
|
||||
expect(screen.getByRole('combobox')).toHaveAccessibleDescription('Where you pay tax.')
|
||||
rerender(<Combobox label="Country" description="Where you pay tax." error="Choose a country." options={countries} />)
|
||||
const input = screen.getByRole('combobox')
|
||||
expect(input).toHaveAttribute('aria-invalid', 'true')
|
||||
expect(input).toHaveAccessibleDescription('Choose a country.')
|
||||
})
|
||||
|
||||
it('passes required and disabled to the input', () => {
|
||||
render(<Combobox label="Country" required disabled options={countries} />)
|
||||
const input = screen.getByRole('combobox', { name: /Country/ })
|
||||
expect(input).toBeDisabled()
|
||||
})
|
||||
|
||||
it('has no axe violations, closed and open', async () => {
|
||||
const { container } = render(
|
||||
<Combobox label="Countries" multiple clearable description="Pick any." defaultValue={['ca']} options={countries} />,
|
||||
)
|
||||
await expectNoA11yViolations(container)
|
||||
await userEvent.click(screen.getByRole('combobox', { name: 'Countries' }))
|
||||
await screen.findByRole('listbox')
|
||||
await expectNoA11yViolations(document.body)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user