|
| 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 | + }, |
| 54 | + { |
| 55 | + id: "experience", |
| 56 | + label: "Erfaring", |
| 57 | + content: ( |
| 58 | + <div className="text-gray-300"> |
| 59 | + {mockCVData.experience.map((exp) => ( |
| 60 | + <div key={exp.description} className="mb-6"> |
| 61 | + <h3 className="font-semibold text-white"> |
| 62 | + {exp.period} - {exp.company} |
| 63 | + </h3> |
| 64 | + {exp.role && <p className="italic">{exp.role}</p>} |
| 65 | + <p>{exp.description}</p> |
| 66 | + </div> |
| 67 | + ))} |
| 68 | + </div> |
| 69 | + ), |
| 70 | + }, |
| 71 | + { |
| 72 | + id: "education", |
| 73 | + label: "Utdanning", |
| 74 | + content: ( |
| 75 | + <div className="text-gray-300"> |
| 76 | + {mockCVData.education.map((edu) => ( |
| 77 | + <div key={edu.description} className="mb-6"> |
| 78 | + <h3 className="font-semibold text-white"> |
| 79 | + {edu.period} - {edu.institution} |
| 80 | + </h3> |
| 81 | + {edu.degree && <p className="italic">{edu.degree}</p>} |
| 82 | + <p>{edu.description}</p> |
| 83 | + </div> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + ), |
| 87 | + }, |
| 88 | +]; |
| 89 | + |
| 90 | +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 | + }); |
| 99 | + |
| 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 | + |
| 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(); |
| 115 | + }); |
| 116 | + |
| 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(); |
| 130 | + }); |
| 131 | + |
| 132 | + it("applies correct ARIA attributes to CV tabs", () => { |
| 133 | + render(<Tabs tabs={mockTabs} />); |
| 134 | + const qualificationsTab = screen.getByRole("tab", { |
| 135 | + name: "Nøkkelkvalifikasjoner", |
| 136 | + }); |
| 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 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it("renders in vertical orientation by default", () => { |
| 152 | + render(<Tabs tabs={mockTabs} />); |
| 153 | + const tabList = screen.getByRole("tablist"); |
| 154 | + expect(tabList).toHaveClass("sm:flex-col"); |
| 155 | + }); |
| 156 | +}); |
0 commit comments