|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import React from "react"; |
| 6 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 7 | +import Tabs from "../../src/components/UI/Tabs.component"; |
| 8 | + |
| 9 | +// Mock framer-motion to avoid issues with animations in tests |
| 10 | +jest.mock("framer-motion", () => ({ |
| 11 | + motion: { |
| 12 | + div: "div", |
| 13 | + button: "button", |
| 14 | + }, |
| 15 | + AnimatePresence: ({ children }: { children: React.ReactNode }) => ( |
| 16 | + <>{children}</> |
| 17 | + ), |
| 18 | +})); |
| 19 | + |
| 20 | +const mockCVData = { |
| 21 | + keyQualifications: ["Qualification 1", "Qualification 2"], |
| 22 | + experience: [ |
| 23 | + { |
| 24 | + period: "2020-2022", |
| 25 | + company: "Example Company", |
| 26 | + role: "Software Developer", |
| 27 | + description: "Worked on various projects", |
| 28 | + }, |
| 29 | + ], |
| 30 | + education: [ |
| 31 | + { |
| 32 | + period: "2016-2020", |
| 33 | + institution: "University of Example", |
| 34 | + degree: "Bachelor in Computer Science", |
| 35 | + description: "Studied various aspects of computer science", |
| 36 | + }, |
| 37 | + ], |
| 38 | +}; |
| 39 | + |
| 40 | +const mockTabs = [ |
| 41 | + { |
| 42 | + id: "qualifications", |
| 43 | + label: "Nøkkelkvalifikasjoner", |
| 44 | + content: ( |
| 45 | + <ul className="list-disc pl-5 text-gray-300"> |
| 46 | + {mockCVData.keyQualifications.map((qual) => ( |
| 47 | + <li key={qual} className="mb-2"> |
| 48 | + {qual} |
| 49 | + </li> |
| 50 | + ))} |
| 51 | + </ul> |
| 52 | + ), |
| 53 | + expectedTexts: mockCVData.keyQualifications, |
| 54 | + unexpectedTexts: ["Example Company", "University of Example"], |
| 55 | + }, |
| 56 | + { |
| 57 | + id: "experience", |
| 58 | + label: "Erfaring", |
| 59 | + content: ( |
| 60 | + <div className="text-gray-300"> |
| 61 | + {mockCVData.experience.map((exp) => ( |
| 62 | + <div key={exp.description} className="mb-6"> |
| 63 | + <h3 className="font-semibold text-white"> |
| 64 | + {exp.period} - {exp.company} |
| 65 | + </h3> |
| 66 | + {exp.role && <p className="italic">{exp.role}</p>} |
| 67 | + <p>{exp.description}</p> |
| 68 | + </div> |
| 69 | + ))} |
| 70 | + </div> |
| 71 | + ), |
| 72 | + expectedTexts: [ |
| 73 | + "2020-2022 - Example Company", |
| 74 | + "Software Developer", |
| 75 | + "Worked on various projects", |
| 76 | + ], |
| 77 | + unexpectedTexts: ["Qualification 1"], |
| 78 | + }, |
| 79 | + { |
| 80 | + id: "education", |
| 81 | + label: "Utdanning", |
| 82 | + content: ( |
| 83 | + <div className="text-gray-300"> |
| 84 | + {mockCVData.education.map((edu) => ( |
| 85 | + <div key={edu.description} className="mb-6"> |
| 86 | + <h3 className="font-semibold text-white"> |
| 87 | + {edu.period} - {edu.institution} |
| 88 | + </h3> |
| 89 | + {edu.degree && <p className="italic">{edu.degree}</p>} |
| 90 | + <p>{edu.description}</p> |
| 91 | + </div> |
| 92 | + ))} |
| 93 | + </div> |
| 94 | + ), |
| 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"], |
| 101 | + }, |
| 102 | +]; |
| 103 | + |
| 104 | +describe("Tabs", () => { |
| 105 | + const renderTabs = () => render(<Tabs tabs={mockTabs} />); |
| 106 | + |
| 107 | + const expectTextsToBePresent = (texts: string[]) => { |
| 108 | + texts.forEach((text) => { |
| 109 | + expect(screen.getByText(text)).toBeInTheDocument(); |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 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 | + }); |
| 124 | + }); |
| 125 | + |
| 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); |
| 133 | + }); |
| 134 | + |
| 135 | + it("applies correct ARIA attributes to CV tabs", () => { |
| 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}`); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it("renders in vertical orientation by default", () => { |
| 148 | + renderTabs(); |
| 149 | + const tabList = screen.getByRole("tablist"); |
| 150 | + expect(tabList).toHaveClass("sm:flex-col"); |
| 151 | + }); |
| 152 | +}); |
0 commit comments