Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,27 @@ describe('Select.Basic', () => {
expect(preventDefaultSpy).not.toHaveBeenCalled();
});

it('should allow typing space when search has value (React state updated via fireEvent)', () => {
const { container } = render(
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
);

const input = container.querySelector('input');
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'hello' } });

const keyDownEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');

input.dispatchEvent(keyDownEvent);

expect(preventDefaultSpy).not.toHaveBeenCalled();
});
Comment on lines +3010 to +3029
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

测试未明确验证下拉框处于打开状态

此测试的核心目的是覆盖 PR 修复的场景(下拉框已打开时允许键入空格),但未调用 expectOpen(container, true) 来显式断言下拉框确实处于打开状态。测试逻辑依赖 fireEvent.change 的副作用来隐式打开下拉框,这使得测试与修复条件(mergedOpen === true)之间的关联不透明,且容易因实现变动而失效。

建议在分发空格键事件前先显式打开下拉框并断言其状态:

✏️ 建议修改
-    it('should allow typing space when search has value (React state updated via fireEvent)', () => {
+    it('should allow typing space when the dropdown is open', () => {
       const { container } = render(
         <Select showSearch options={[{ value: 'test', label: 'test' }]} />,
       );
 
       const input = container.querySelector('input');
       fireEvent.focus(input);
-      fireEvent.change(input, { target: { value: 'hello' } });
+      // 显式打开下拉框并验证其状态,对应 PR 修复的核心条件 mergedOpen === true
+      toggleOpen(container);
+      expectOpen(container, true);
 
       const keyDownEvent = new KeyboardEvent('keydown', {
         key: ' ',
         code: 'Space',
         bubbles: true,
       });
       const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
 
       input.dispatchEvent(keyDownEvent);
 
       expect(preventDefaultSpy).not.toHaveBeenCalled();
     });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('should allow typing space when search has value (React state updated via fireEvent)', () => {
const { container } = render(
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
);
const input = container.querySelector('input');
fireEvent.focus(input);
fireEvent.change(input, { target: { value: 'hello' } });
const keyDownEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
input.dispatchEvent(keyDownEvent);
expect(preventDefaultSpy).not.toHaveBeenCalled();
});
it('should allow typing space when the dropdown is open', () => {
const { container } = render(
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
);
const input = container.querySelector('input');
fireEvent.focus(input);
// 显式打开下拉框并验证其状态,对应 PR 修复的核心条件 mergedOpen === true
toggleOpen(container);
expectOpen(container, true);
const keyDownEvent = new KeyboardEvent('keydown', {
key: ' ',
code: 'Space',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');
input.dispatchEvent(keyDownEvent);
expect(preventDefaultSpy).not.toHaveBeenCalled();
});
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/Select.test.tsx` around lines 3010 - 3029, Test relies on side effects
to open the dropdown but never asserts it, so add an explicit open check before
dispatching the space key: after fireEvent.change(input, { target: { value:
'hello' } }) call expectOpen(container, true) (or perform an explicit open
action such as fireEvent.mouseDown on the trigger) to guarantee mergedOpen ===
true, then dispatch the KeyboardEvent and assert preventDefault; reference
expectOpen, fireEvent.change, input.dispatchEvent, and the mergedOpen condition
when making the change.


it('should call preventDefault on space when showSearch is disabled', () => {
const { container } = render(<Select options={[{ value: 'test', label: 'test' }]} />);

Expand Down Expand Up @@ -3045,4 +3066,46 @@ describe('Select.Basic', () => {
expect(preventDefaultSpy).not.toHaveBeenCalled();
});
});

describe('Enter key behavior (preventDefault)', () => {
it('should call preventDefault on Enter when not combobox to avoid form submit', () => {
const { container } = render(
<Select showSearch options={[{ value: 'test', label: 'test' }]} />,
);

const input = container.querySelector('input');
input.focus();

const keyDownEvent = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');

input.dispatchEvent(keyDownEvent);

expect(preventDefaultSpy).toHaveBeenCalled();
});

it('should not call preventDefault on Enter in combobox mode', () => {
const { container } = render(
<Select mode="combobox" options={[{ value: 'test', label: 'test' }]} />,
);

const input = container.querySelector('input');
input.focus();

const keyDownEvent = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
bubbles: true,
});
const preventDefaultSpy = jest.spyOn(keyDownEvent, 'preventDefault');

input.dispatchEvent(keyDownEvent);

expect(preventDefaultSpy).not.toHaveBeenCalled();
});
});
});
Loading