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()
expect(screen.getByRole('combobox', { name: 'Country' })).toBeInTheDocument()
})
it('filters as you type and shows the empty state', async () => {
render()
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()
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()
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()
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('ca')
return (
<>
{String(value)}
>
)
}
render()
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()
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(
,
)
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(
,
)
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()
expect(screen.getByRole('combobox')).toHaveAccessibleDescription('Where you pay tax.')
rerender()
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()
const input = screen.getByRole('combobox', { name: /Country/ })
expect(input).toBeDisabled()
})
it('has no axe violations, closed and open', async () => {
const { container } = render(
,
)
await expectNoA11yViolations(container)
await userEvent.click(screen.getByRole('combobox', { name: 'Countries' }))
await screen.findByRole('listbox')
await expectNoA11yViolations(document.body)
})
})