Skip to content

Commit 9d6a8f1

Browse files
committed
Test: Add test cases for space key behavior with showSearch
Added test coverage to verify space key behavior in different Select modes: 1. showSearch enabled: Space should NOT call preventDefault, allowing space to be typed 2. showSearch disabled: Space SHOULD call preventDefault, preventing page scroll 3. combobox mode: Space should NOT call preventDefault, allowing space to be typed These tests ensure the fix correctly handles space key input based on whether the input is editable.
1 parent f566f35 commit 9d6a8f1

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/spaceInput.test.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as React from 'react';
2+
import { render } from '@testing-library/react';
3+
import Select from '../src/Select';
4+
5+
describe('Space key behavior with showSearch', () => {
6+
it('should not call preventDefault on space when showSearch is enabled', () => {
7+
const { container } = render(
8+
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
9+
);
10+
11+
const input = container.querySelector('input');
12+
input.focus();
13+
14+
const keyDownEvent = new KeyboardEvent('keydown', {
15+
key: ' ',
16+
code: 'Space',
17+
bubbles: true,
18+
});
19+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
20+
21+
input.dispatchEvent(keyDownEvent);
22+
23+
expect(preventDefaultSpy).not.toHaveBeenCalled();
24+
});
25+
26+
it('should call preventDefault on space when showSearch is disabled', () => {
27+
const { container } = render(<Select options={[{ value: 'test', label: 'test' }]} />);
28+
29+
const input = container.querySelector('input');
30+
input.focus();
31+
32+
const keyDownEvent = new KeyboardEvent('keydown', {
33+
key: ' ',
34+
code: 'Space',
35+
bubbles: true,
36+
});
37+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
38+
39+
input.dispatchEvent(keyDownEvent);
40+
41+
expect(preventDefaultSpy).toHaveBeenCalled();
42+
});
43+
44+
it('should not call preventDefault on space in combobox mode', () => {
45+
const { container } = render(
46+
<Select mode="combobox" options={[{ value: 'test', label: 'test' }]} />,
47+
);
48+
49+
const input = container.querySelector('input');
50+
input.focus();
51+
52+
const keyDownEvent = new KeyboardEvent('keydown', {
53+
key: ' ',
54+
code: 'Space',
55+
bubbles: true,
56+
});
57+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
58+
59+
input.dispatchEvent(keyDownEvent);
60+
61+
expect(preventDefaultSpy).not.toHaveBeenCalled();
62+
});
63+
});

0 commit comments

Comments
 (0)