Skip to content

Commit b07ca7f

Browse files
test: added new test cases for tab component
1 parent 9fe9474 commit b07ca7f

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/components/Tabs/__test__/Tabs.spec.tsx

+43-2
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ describe("Tabs Component", () => {
1313
);
1414

1515
const tabsComponent = container.querySelector(".derivs-primary-tabs")
16+
const tabTitle = screen.getByText("Tab 1")
1617
const tab1Content = screen.getByText("Tab 1 Content")
18+
expect(tabTitle).toBeInTheDocument();
1719
expect(tabsComponent).toBeInTheDocument();
1820
expect(tab1Content).toBeInTheDocument();
1921
expect(tabsComponent).toHaveClass("derivs-primary-tabs");
2022
});
2123

22-
it("changes active tab when clicked", () => {
24+
it("changes active tab when clicked and checks active tab content showing", () => {
2325
render(
24-
<Tabs>
26+
<Tabs activeTab="Tab 1">
2527
<Tab title="Tab 1">Tab 1 Content</Tab>
2628
<Tab title="Tab 2">Tab 2 Content</Tab>
2729
</Tabs>
2830
);
31+
const tab1 = screen.getByText("Tab 1 Content")
32+
expect(tab1).toBeInTheDocument();
2933
const tab2 = screen.getByText("Tab 2");
3034
fireEvent.click(tab2);
3135
const activeButton = screen.getAllByRole("button")[1];
@@ -34,6 +38,43 @@ describe("Tabs Component", () => {
3438
expect(activeButton).toHaveClass("derivs-primary-tabs__btn--active");
3539
});
3640

41+
it("should render tabs with correct variant", () => {
42+
const { container } = render(
43+
<Tabs variant="secondary">
44+
<Tab title="Tab 1">Tab 1 Content</Tab>
45+
<Tab title="Tab 2">Tab 2 Content</Tab>
46+
</Tabs>
47+
);
48+
const tabsComponent = container.querySelector(".derivs-secondary-tabs")
49+
expect(tabsComponent).toBeInTheDocument();
50+
});
51+
52+
it("should render tabs with correct title font size", () => {
53+
const { container } = render(
54+
<Tabs TitleFontSize="lg">
55+
<Tab title="Tab 1">Tab 1 Content</Tab>
56+
<Tab title="Tab 2">Tab 2 Content</Tab>
57+
</Tabs>
58+
);
59+
60+
const tabsComponent = container.querySelector(".derivs-primary-tabs")
61+
62+
expect(tabsComponent).toHaveStyle("font-size: lg;");
63+
});
64+
65+
it("should call onChange handler when a tab is clicked", () => {
66+
const mockOnChange = jest.fn();
67+
const { getByText } = render(
68+
<Tabs onChange={mockOnChange}>
69+
<Tab title="Tab 1">Tab 1 Content</Tab>
70+
<Tab title="Tab 2">Tab 2 Content</Tab>
71+
</Tabs>
72+
);
73+
const tabTitle = getByText("Tab 1");
74+
fireEvent.click(tabTitle);
75+
expect(mockOnChange).toHaveBeenCalled();
76+
});
77+
3778
it("applies custom class to wrapper", () => {
3879
const { container } = render(
3980
<Tabs wrapperClassName="custom-wrapper">

0 commit comments

Comments
 (0)