|  | 
|  | 1 | +import { render, screen } from '@testing-library/react'; | 
|  | 2 | +import '@testing-library/jest-dom/vitest'; | 
|  | 3 | +import userEvent from '@testing-library/user-event'; | 
|  | 4 | +import { describe, it, expect, vi } from 'vitest'; | 
|  | 5 | +import { Select } from '../components/ui/Select'; | 
|  | 6 | + | 
|  | 7 | +describe('Select keyboard interactions', () => { | 
|  | 8 | +  const options = [ | 
|  | 9 | +    { value: 'a', label: 'Alpha' }, | 
|  | 10 | +    { value: 'b', label: 'Bravo', disabled: true }, | 
|  | 11 | +    { value: 'c', label: 'Charlie' }, | 
|  | 12 | +  ]; | 
|  | 13 | + | 
|  | 14 | +  it('opens with Enter and Space and closes with Escape, focuses trigger after close', async () => { | 
|  | 15 | +    const onChange = vi.fn(); | 
|  | 16 | +    const user = userEvent.setup(); | 
|  | 17 | +    render(<Select options={options} value={undefined} onChange={onChange} />); | 
|  | 18 | + | 
|  | 19 | +    const trigger = screen.getAllByRole('button')[0]; | 
|  | 20 | + | 
|  | 21 | +    await user.click(trigger); | 
|  | 22 | +    expect(trigger).toHaveAttribute('aria-expanded', 'true'); | 
|  | 23 | + | 
|  | 24 | +    await user.keyboard('{Escape}'); | 
|  | 25 | +    expect(trigger).toHaveAttribute('aria-expanded', 'false'); | 
|  | 26 | +    expect(trigger).toHaveFocus(); | 
|  | 27 | + | 
|  | 28 | +    await user.keyboard(' '); | 
|  | 29 | +    expect(trigger).toHaveAttribute('aria-expanded', 'true'); | 
|  | 30 | +  }); | 
|  | 31 | + | 
|  | 32 | +  it('navigates options with ArrowDown/ArrowUp skipping disabled options and commits with Enter', async () => { | 
|  | 33 | +    const onChange = vi.fn(); | 
|  | 34 | +    const user = userEvent.setup(); | 
|  | 35 | +    render(<Select options={options} onChange={onChange} />); | 
|  | 36 | + | 
|  | 37 | +    const trigger = screen.getAllByRole('button')[0]; | 
|  | 38 | + | 
|  | 39 | +    await user.click(trigger); | 
|  | 40 | +    expect(trigger).toHaveAttribute('aria-expanded', 'true'); | 
|  | 41 | + | 
|  | 42 | +    const alpha = screen.getByRole('option', { name: 'Alpha' }); | 
|  | 43 | +    expect(alpha).toHaveFocus(); | 
|  | 44 | + | 
|  | 45 | +    await user.keyboard('{ArrowDown}'); | 
|  | 46 | +    const charlie = screen.getByRole('option', { name: 'Charlie' }); | 
|  | 47 | +    expect(charlie).toHaveFocus(); | 
|  | 48 | + | 
|  | 49 | +    await user.keyboard('{Enter}'); | 
|  | 50 | +    expect(onChange).toHaveBeenCalledWith('c'); | 
|  | 51 | +    expect(trigger).toHaveAttribute('aria-expanded', 'false'); | 
|  | 52 | +    expect(trigger).toHaveFocus(); | 
|  | 53 | +  }); | 
|  | 54 | +}); | 
0 commit comments