/** @vitest-environment jsdom */ import { cleanup, render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { afterEach, describe, expect, it, vi } from 'vitest'; import Select from './Select'; afterEach(cleanup); const alpha = { value: 1, label: 'Alpha' }; const beta = { value: 2, label: 'Beta' }; const gamma = { value: 3, label: 'Gamma' }; describe('Select', () => { it('keeps the selected label when the option leaves a server-side search result', () => { const props = { value: 1, onChange: vi.fn(), onSearchChange: vi.fn() }; const { rerender } = render(); expect(input).toHaveValue('Alpha'); }); it('drops the remembered label once the value changes', () => { const onChange = vi.fn(); const { rerender } = render(); expect(screen.getByRole('combobox')).toHaveValue(''); }); it('keeps every selected option of a multi-select when they leave the option list', () => { const props = { multiple: true as const, value: [1, 2], onChange: vi.fn() }; const { rerender } = render(); expect(screen.getByText('2 selected')).toBeInTheDocument(); }); it('offers typed text that matches no option to a creatable multi-select', async () => { const user = userEvent.setup(); const props = { multiple: true as const, value: [], onChange: vi.fn(), onCreate: vi.fn() }; render( ); const input = screen.getByRole('combobox'); expect(input).toHaveAttribute('aria-describedby', 'select-help'); expect(input).toHaveAttribute('aria-invalid', 'true'); }); });