import { render, screen } 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 { NativeSelect } from './NativeSelect' import type { SelectOption } from './select.tokens' const currencies: SelectOption[] = [ { value: 'usd', label: 'US Dollar', group: 'Americas' }, { value: 'cad', label: 'Canadian Dollar', group: 'Americas' }, { value: 'eur', label: 'Euro', group: 'Europe' }, { value: 'gbp', label: 'Pound Sterling', group: 'Europe', disabled: true }, ] const plain: SelectOption[] = [ { value: 'eng', label: 'Engineering' }, { value: 'fin', label: 'Finance' }, ] describe('NativeSelect', () => { it('links the label to the select', () => { render() expect(screen.getByRole('combobox', { name: 'Department' })).toBeInTheDocument() }) it('renders groups as optgroups and a placeholder option', () => { render() const select = screen.getByRole('combobox', { name: 'Currency' }) expect(select).toHaveValue('') expect(screen.getAllByRole('group').map((g) => g.getAttribute('label'))).toEqual(['Americas', 'Europe']) expect(screen.getByRole('option', { name: 'Pound Sterling' })).toBeDisabled() }) it('works uncontrolled and fires onValueChange and onChange', async () => { const onValueChange = vi.fn() const onChange = vi.fn() render() const select = screen.getByRole('combobox', { name: 'Department' }) expect(select).toHaveValue('eng') await userEvent.selectOptions(select, 'fin') expect(select).toHaveValue('fin') expect(onValueChange).toHaveBeenCalledWith('fin') expect(onChange).toHaveBeenCalled() }) it('works controlled', async () => { function Controlled() { const [value, setValue] = useState('fin') return ( <>

{value}

) } render() const select = screen.getByRole('combobox', { name: 'Department' }) expect(select).toHaveValue('fin') await userEvent.selectOptions(select, 'eng') expect(screen.getByTestId('value')).toHaveTextContent('eng') expect(select).toHaveValue('eng') }) it('announces the description, and the error when invalid', () => { const { rerender } = render() expect(screen.getByRole('combobox')).toHaveAccessibleDescription('Where they report.') rerender() expect(screen.getByRole('combobox')).toHaveAttribute('aria-invalid', 'true') expect(screen.getByRole('combobox')).toHaveAccessibleDescription('Choose one.') }) it('passes required, disabled and name to the select', () => { render() const select = screen.getByRole('combobox', { name: /Department/ }) expect(select).toBeRequired() expect(select).toBeDisabled() expect(select).toHaveAttribute('name', 'department') }) it('is disabled and busy while loading', () => { render() const select = screen.getByRole('combobox', { name: 'Department' }) expect(select).toBeDisabled() expect(select).toHaveAttribute('aria-busy', 'true') }) it('has no axe violations', async () => { const { container } = render( , ) await expectNoA11yViolations(container) }) })