Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions packages/x/components/sender/__tests__/slot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,46 @@ describe('Sender Slot Component', () => {
setupDOMMocks(customSelectionMock, customRangeMock);
fireEvent.keyDown(dom, { key: 'Backspace' });
});
it('should trigger closable.onClose when removing skill via Backspace', () => {
const onClose = jest.fn();
const ref = createRef<SenderRef>();
const { container } = render(
<Sender
ref={ref}
slotConfig={[]}
placeholder="Sender placeholder"
skill={{
value: 'test-skill',
title: 'Test Skill',
closable: { onClose },
}}
/>,
);
const dom = ref.current?.inputElement as HTMLElement;
const skillDom = container.querySelector('.ant-sender-skill') as HTMLElement;
expect(ref.current).toBeDefined();
expect(skillDom).toBeDefined();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using toBeDefined() on ref.current and skillDom is a weak assertion because null is considered defined in Jest. Since querySelector returns null when an element is not found, expect(skillDom).toBeDefined() will pass even if the element does not exist.

We should use not.toBeNull() for the ref and toBeInTheDocument() for the DOM element to ensure they actually exist and are rendered correctly.

Suggested change
expect(ref.current).toBeDefined();
expect(skillDom).toBeDefined();
expect(ref.current).not.toBeNull();
expect(skillDom).toBeInTheDocument();

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 523a199not.toBeNull() for the ref and toBeInTheDocument() for the skill element.

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

const customSelectionMock = {
rangeCount: 1,
focusOffset: 0,
anchorNode: dom,
removeAllRanges: jest.fn(),
addRange: jest.fn(),
};

Object.defineProperty(dom, 'previousSibling', {
value: skillDom,
configurable: true,
});

const customRangeMock = createMockRange();
setupDOMMocks(customSelectionMock, customRangeMock);
fireEvent.keyDown(dom, { key: 'Backspace' });

expect(onClose).toHaveBeenCalledTimes(1);
expect(container.querySelector('.ant-sender-skill')).not.toBeInTheDocument();
});
it('should handle skill removal and addition', () => {
const { rerender, container } = render(
<Sender
Expand Down
4 changes: 4 additions & 0 deletions packages/x/components/sender/components/SlotTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ const SlotTextArea = React.forwardRef<SlotTextAreaRef>((_, ref) => {
if (skillKey) {
e.preventDefault();
removeSkill();
// 与点击关闭行为保持一致,删除后补发 closable.onClose
if (skill?.closable && typeof skill.closable === 'object') {
skill.closable.onClose?.(e as unknown as React.MouseEvent<HTMLDivElement>);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Casting a KeyboardEvent (e) directly to React.MouseEvent is type-unsafe. If the consumer's onClose callback accesses mouse-specific properties (such as clientX, clientY, or button), it will result in undefined values or potential runtime errors.

Since onClose is defined as a MouseEventHandler, we should consider if we can avoid passing the keyboard event directly, or at least document this behavior. If possible, we could also update the onClose type definition in interface.ts to accept React.MouseEvent | React.KeyboardEvent in a future PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in 523a199 — took the wider-type route: closable.onClose is now typed (event: React.SyntheticEvent<HTMLDivElement>) => void (interface, local ClosableConfig, and both docs), and the keyboard path passes the real event with no cast.

}
return true;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Expand Down