Skip to content

Commit cfa1ff5

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 cfa1ff5

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

tests/Select.test.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,4 +2985,64 @@ describe('Select.Basic', () => {
29852985

29862986
expect(selectedItem).not.toHaveAttribute('xxx');
29872987
});
2988+
2989+
describe('Space key behavior with showSearch', () => {
2990+
it('should not call preventDefault on space when showSearch is enabled', () => {
2991+
const { container } = render(
2992+
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
2993+
);
2994+
2995+
const input = container.querySelector('input');
2996+
input.focus();
2997+
2998+
const keyDownEvent = new KeyboardEvent('keydown', {
2999+
key: ' ',
3000+
code: 'Space',
3001+
bubbles: true,
3002+
});
3003+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
3004+
3005+
input.dispatchEvent(keyDownEvent);
3006+
3007+
expect(preventDefaultSpy).not.toHaveBeenCalled();
3008+
});
3009+
3010+
it('should call preventDefault on space when showSearch is disabled', () => {
3011+
const { container } = render(<Select options={[{ value: 'test', label: 'test' }]} />);
3012+
3013+
const input = container.querySelector('input');
3014+
input.focus();
3015+
3016+
const keyDownEvent = new KeyboardEvent('keydown', {
3017+
key: ' ',
3018+
code: 'Space',
3019+
bubbles: true,
3020+
});
3021+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
3022+
3023+
input.dispatchEvent(keyDownEvent);
3024+
3025+
expect(preventDefaultSpy).toHaveBeenCalled();
3026+
});
3027+
3028+
it('should not call preventDefault on space in combobox mode', () => {
3029+
const { container } = render(
3030+
<Select mode="combobox" options={[{ value: 'test', label: 'test' }]} />,
3031+
);
3032+
3033+
const input = container.querySelector('input');
3034+
input.focus();
3035+
3036+
const keyDownEvent = new KeyboardEvent('keydown', {
3037+
key: ' ',
3038+
code: 'Space',
3039+
bubbles: true,
3040+
});
3041+
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
3042+
3043+
input.dispatchEvent(keyDownEvent);
3044+
3045+
expect(preventDefaultSpy).not.toHaveBeenCalled();
3046+
});
3047+
});
29883048
});

0 commit comments

Comments
 (0)