Skip to content

Commit d834dff

Browse files
committed
Reduce duplicated lines
1 parent bbf9c46 commit d834dff

File tree

1 file changed

+47
-51
lines changed

1 file changed

+47
-51
lines changed

__tests__/UI/Tabs.test.tsx

+47-51
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const mockTabs = [
5050
))}
5151
</ul>
5252
),
53+
expectedTexts: mockCVData.keyQualifications,
54+
unexpectedTexts: ["Example Company", "University of Example"],
5355
},
5456
{
5557
id: "experience",
@@ -67,6 +69,12 @@ const mockTabs = [
6769
))}
6870
</div>
6971
),
72+
expectedTexts: [
73+
"2020-2022 - Example Company",
74+
"Software Developer",
75+
"Worked on various projects",
76+
],
77+
unexpectedTexts: ["Qualification 1"],
7078
},
7179
{
7280
id: "education",
@@ -84,72 +92,60 @@ const mockTabs = [
8492
))}
8593
</div>
8694
),
95+
expectedTexts: [
96+
"2016-2020 - University of Example",
97+
"Bachelor in Computer Science",
98+
"Studied various aspects of computer science",
99+
],
100+
unexpectedTexts: ["Qualification 1"],
87101
},
88102
];
89103

90104
describe("Tabs", () => {
91-
it("renders all CV tab labels", () => {
92-
render(<Tabs tabs={mockTabs} />);
93-
expect(
94-
screen.getByRole("tab", { name: "Nøkkelkvalifikasjoner" })
95-
).toBeInTheDocument();
96-
expect(screen.getByRole("tab", { name: "Erfaring" })).toBeInTheDocument();
97-
expect(screen.getByRole("tab", { name: "Utdanning" })).toBeInTheDocument();
98-
});
105+
const renderTabs = () => render(<Tabs tabs={mockTabs} />);
99106

100-
it("renders the first tab content (Nøkkelkvalifikasjoner) by default", () => {
101-
render(<Tabs tabs={mockTabs} />);
102-
expect(screen.getByText("Qualification 1")).toBeInTheDocument();
103-
expect(screen.getByText("Qualification 2")).toBeInTheDocument();
104-
expect(screen.queryByText("Example Company")).not.toBeInTheDocument();
105-
expect(screen.queryByText("University of Example")).not.toBeInTheDocument();
106-
});
107+
const expectTextsToBePresent = (texts: string[]) => {
108+
texts.forEach((text) => {
109+
expect(screen.getByText(text)).toBeInTheDocument();
110+
});
111+
};
107112

108-
it("switches to Erfaring tab when clicked", () => {
109-
render(<Tabs tabs={mockTabs} />);
110-
fireEvent.click(screen.getByRole("tab", { name: "Erfaring" }));
111-
expect(screen.getByText("2020-2022 - Example Company")).toBeInTheDocument();
112-
expect(screen.getByText("Software Developer")).toBeInTheDocument();
113-
expect(screen.getByText("Worked on various projects")).toBeInTheDocument();
114-
expect(screen.queryByText("Qualification 1")).not.toBeInTheDocument();
113+
const expectTextsNotToBePresent = (texts: string[]) => {
114+
texts.forEach((text) => {
115+
expect(screen.queryByText(text)).not.toBeInTheDocument();
116+
});
117+
};
118+
119+
it("renders all CV tab labels", () => {
120+
renderTabs();
121+
mockTabs.forEach((tab) => {
122+
expect(screen.getByRole("tab", { name: tab.label })).toBeInTheDocument();
123+
});
115124
});
116125

117-
it("switches to Utdanning tab when clicked", () => {
118-
render(<Tabs tabs={mockTabs} />);
119-
fireEvent.click(screen.getByRole("tab", { name: "Utdanning" }));
120-
expect(
121-
screen.getByText("2016-2020 - University of Example")
122-
).toBeInTheDocument();
123-
expect(
124-
screen.getByText("Bachelor in Computer Science")
125-
).toBeInTheDocument();
126-
expect(
127-
screen.getByText("Studied various aspects of computer science")
128-
).toBeInTheDocument();
129-
expect(screen.queryByText("Qualification 1")).not.toBeInTheDocument();
126+
it.each(mockTabs)("renders correct content for $label tab", (tab) => {
127+
renderTabs();
128+
if (tab.id !== mockTabs[0].id) {
129+
fireEvent.click(screen.getByRole("tab", { name: tab.label }));
130+
}
131+
expectTextsToBePresent(tab.expectedTexts);
132+
expectTextsNotToBePresent(tab.unexpectedTexts);
130133
});
131134

132135
it("applies correct ARIA attributes to CV tabs", () => {
133-
render(<Tabs tabs={mockTabs} />);
134-
const qualificationsTab = screen.getByRole("tab", {
135-
name: "Nøkkelkvalifikasjoner",
136+
renderTabs();
137+
mockTabs.forEach((tab, index) => {
138+
const tabElement = screen.getByRole("tab", { name: tab.label });
139+
expect(tabElement).toHaveAttribute(
140+
"aria-selected",
141+
index === 0 ? "true" : "false"
142+
);
143+
expect(tabElement).toHaveAttribute("aria-controls", `tabpanel-${tab.id}`);
136144
});
137-
expect(qualificationsTab).toHaveAttribute("aria-selected", "true");
138-
expect(qualificationsTab).toHaveAttribute(
139-
"aria-controls",
140-
"tabpanel-qualifications"
141-
);
142-
143-
const experienceTab = screen.getByRole("tab", { name: "Erfaring" });
144-
expect(experienceTab).toHaveAttribute("aria-selected", "false");
145-
expect(experienceTab).toHaveAttribute(
146-
"aria-controls",
147-
"tabpanel-experience"
148-
);
149145
});
150146

151147
it("renders in vertical orientation by default", () => {
152-
render(<Tabs tabs={mockTabs} />);
148+
renderTabs();
153149
const tabList = screen.getByRole("tablist");
154150
expect(tabList).toHaveClass("sm:flex-col");
155151
});

0 commit comments

Comments
 (0)