Skip to content

Commit 10e08f2

Browse files
committed
Added keyboard navigation tests for Combobox
1 parent 77bedb1 commit 10e08f2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 { Combobox } from '../components/ui/Select';
6+
7+
describe('Combobox keyboard interactions', () => {
8+
const options = [
9+
{ value: '1', label: 'One' },
10+
{ value: '2', label: 'Two' },
11+
{ value: '3', label: 'Three' },
12+
];
13+
14+
it('opens with ArrowDown/Enter and lets you search, commit custom entry with Enter', async () => {
15+
const onChange = vi.fn();
16+
const user = userEvent.setup();
17+
render(<Combobox options={options} value={''} onChange={onChange} />);
18+
19+
const trigger = screen.getByRole('combobox');
20+
21+
await user.click(trigger);
22+
23+
expect(trigger).toHaveAttribute('aria-expanded', 'true');
24+
25+
const input = screen.getByRole('textbox');
26+
await user.type(input, 'CustomName');
27+
28+
await user.keyboard('{Enter}');
29+
expect(onChange).toHaveBeenCalledWith('CustomName');
30+
});
31+
32+
it('navigates filtered options with Arrow keys and selects with Enter', async () => {
33+
const onChange = vi.fn();
34+
const user = userEvent.setup();
35+
render(<Combobox options={options} value={''} onChange={onChange} />);
36+
37+
const trigger = screen.getByRole('combobox');
38+
await user.click(trigger);
39+
40+
const input = screen.getByRole('textbox');
41+
await user.type(input, 'T');
42+
43+
await user.keyboard('{ArrowDown}');
44+
const two = screen.getByRole('option', { name: 'Two' });
45+
expect(two).toHaveFocus();
46+
47+
await user.keyboard('{Enter}');
48+
expect(onChange).toHaveBeenCalledWith('Two');
49+
});
50+
});

0 commit comments

Comments
 (0)