mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
feat(tabs): add click handling for tab items in tests and implementation (#3917)
Co-authored-by: WK Wong <wingkwong.code@gmail.com>
This commit is contained in:
parent
6129e4bc79
commit
718dc24ff5
5
.changeset/seven-tips-help.md
Normal file
5
.changeset/seven-tips-help.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@heroui/tabs": patch
|
||||
---
|
||||
|
||||
fixed click handling for tab items
|
||||
@ -400,4 +400,36 @@ describe("Tabs", () => {
|
||||
);
|
||||
expect(ref.current).not.toBeNull();
|
||||
});
|
||||
|
||||
it("Tab click should be handled", async () => {
|
||||
const item1Click = jest.fn();
|
||||
const item2Click = jest.fn();
|
||||
const wrapper = render(
|
||||
<Tabs>
|
||||
<Tab key="item1" data-testid="item1" title="Item 1" onClick={item1Click}>
|
||||
<div>Content 1</div>
|
||||
</Tab>
|
||||
<Tab key="item2" data-testid="item2" title="Item 2" onClick={item2Click}>
|
||||
<div>Content 2</div>
|
||||
</Tab>
|
||||
</Tabs>,
|
||||
);
|
||||
const tab1 = wrapper.getByTestId("item1");
|
||||
const tab2 = wrapper.getByTestId("item2");
|
||||
|
||||
// Test initial state
|
||||
expect(tab1).toHaveAttribute("aria-selected", "true");
|
||||
expect(tab2).toHaveAttribute("aria-selected", "false");
|
||||
|
||||
// Test clicking tab2
|
||||
await user.click(tab2);
|
||||
expect(item2Click).toHaveBeenCalledTimes(1);
|
||||
expect(tab1).toHaveAttribute("aria-selected", "false");
|
||||
expect(tab2).toHaveAttribute("aria-selected", "true");
|
||||
|
||||
// Test clicking tab2 again
|
||||
await user.click(tab2);
|
||||
expect(item2Click).toHaveBeenCalledTimes(2);
|
||||
expect(tab2).toHaveAttribute("aria-selected", "true");
|
||||
});
|
||||
});
|
||||
|
||||
@ -80,7 +80,7 @@ const Tab = forwardRef<"button", TabItemProps>((props, ref) => {
|
||||
});
|
||||
|
||||
const handleClick = () => {
|
||||
chain(onClick, tabProps.onClick);
|
||||
chain(onClick, tabProps.onClick)();
|
||||
|
||||
if (!domRef?.current || !listRef?.current) return;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user