Skip to content

Commit b29ce82

Browse files
authored
Merge pull request #151 from yaswanth-deriv/yaswanth/FEQ-1999_Added_test_for_tabs_component
[FEQ]Yaswanth/FEQ-1999_Added Test for tabs component
2 parents 17c2fa9 + b07ca7f commit b29ce82

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
import React from "react";
3+
import { render, fireEvent, screen } from "@testing-library/react";
4+
import { Tab, Tabs } from "..";
5+
6+
describe("Tabs Component", () => {
7+
it("renders Tabs component with primary variant by default", () => {
8+
const { container } = render(
9+
<Tabs>
10+
<Tab title="Tab 1">Tab 1 Content</Tab>
11+
<Tab title="Tab 2">Tab 2 Content</Tab>
12+
</Tabs>
13+
);
14+
15+
const tabsComponent = container.querySelector(".derivs-primary-tabs")
16+
const tabTitle = screen.getByText("Tab 1")
17+
const tab1Content = screen.getByText("Tab 1 Content")
18+
expect(tabTitle).toBeInTheDocument();
19+
expect(tabsComponent).toBeInTheDocument();
20+
expect(tab1Content).toBeInTheDocument();
21+
expect(tabsComponent).toHaveClass("derivs-primary-tabs");
22+
});
23+
24+
it("changes active tab when clicked and checks active tab content showing", () => {
25+
render(
26+
<Tabs activeTab="Tab 1">
27+
<Tab title="Tab 1">Tab 1 Content</Tab>
28+
<Tab title="Tab 2">Tab 2 Content</Tab>
29+
</Tabs>
30+
);
31+
const tab1 = screen.getByText("Tab 1 Content")
32+
expect(tab1).toBeInTheDocument();
33+
const tab2 = screen.getByText("Tab 2");
34+
fireEvent.click(tab2);
35+
const activeButton = screen.getAllByRole("button")[1];
36+
const tabContent = screen.getByText("Tab 2 Content")
37+
expect(tabContent).toBeInTheDocument();
38+
expect(activeButton).toHaveClass("derivs-primary-tabs__btn--active");
39+
});
40+
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+
78+
it("applies custom class to wrapper", () => {
79+
const { container } = render(
80+
<Tabs wrapperClassName="custom-wrapper">
81+
<Tab title="Tab 1">Tab 1 Content</Tab>
82+
<Tab title="Tab 2">Tab 2 Content</Tab>
83+
</Tabs>
84+
);
85+
const tabsWrapper = container.querySelector(".custom-wrapper");
86+
expect(tabsWrapper).toBeInTheDocument();
87+
});
88+
});

0 commit comments

Comments
 (0)