|
| 1 | +import React from "react"; |
| 2 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 3 | +import "@testing-library/jest-dom"; |
| 4 | +import CfpSection2023 from "./CfpSection2023"; |
| 5 | +import { useWindowSize } from "react-use"; |
| 6 | +import conferenceData from "../../data/2023.json"; |
| 7 | +import { data } from "./CfpData"; |
| 8 | + |
| 9 | +// Mock useWindowSize to control the window size in tests |
| 10 | +jest.mock("react-use", () => ({ |
| 11 | + ...jest.requireActual("react-use"), |
| 12 | + useWindowSize: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +describe("CfpSection2023", () => { |
| 16 | + beforeEach(() => { |
| 17 | + // Reset the mock before each test |
| 18 | + (useWindowSize as jest.Mock).mockReset(); |
| 19 | + (useWindowSize as jest.Mock).mockReturnValue({ width: 1024 }); // Default width |
| 20 | + }); |
| 21 | + |
| 22 | + it("should render without crashing", () => { |
| 23 | + render(<CfpSection2023 />); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should render the title and subtitle", () => { |
| 27 | + render(<CfpSection2023 />); |
| 28 | + expect( |
| 29 | + screen.getByText("CFP Committee", { exact: false }), |
| 30 | + ).toBeInTheDocument(); |
| 31 | + expect( |
| 32 | + screen.getByText( |
| 33 | + "We're excited to announce the members of the Call for Papers committee for the next DevBcn conference! These experienced professionals will be reviewing and selecting the best talks and workshops for the upcoming event.", |
| 34 | + ), |
| 35 | + ).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should render the tracks and members", () => { |
| 39 | + render(<CfpSection2023 />); |
| 40 | + data.forEach((track) => { |
| 41 | + expect(screen.getAllByText(track.name, { exact: false })).not.toBeNull(); |
| 42 | + track.members |
| 43 | + .filter((member) => member.photo !== "") |
| 44 | + .forEach((member) => { |
| 45 | + expect( |
| 46 | + screen.getAllByText(member.name, { exact: false }), |
| 47 | + ).not.toBeNull(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should render member photos", () => { |
| 53 | + render(<CfpSection2023 />); |
| 54 | + data.forEach((track) => { |
| 55 | + track.members |
| 56 | + .filter((member) => member.photo !== "") |
| 57 | + .forEach((member) => { |
| 58 | + const image = screen.getAllByAltText(member.name); |
| 59 | + expect(image).not.toBeNull(); |
| 60 | + expect(image.at(0)).toHaveAttribute("src", member.photo); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should render twitter links", () => { |
| 66 | + render(<CfpSection2023 />); |
| 67 | + data.forEach((track) => { |
| 68 | + track.members |
| 69 | + .filter((member) => member.twitter !== "") |
| 70 | + .forEach((member) => { |
| 71 | + const twitterLinks = screen.getAllByRole("link"); |
| 72 | + const twitterLink = twitterLinks.find( |
| 73 | + (link) => link.getAttribute("href") === member.twitter, |
| 74 | + ); |
| 75 | + expect(twitterLink).toBeInTheDocument(); |
| 76 | + expect(twitterLink).toHaveAttribute("href", member.twitter); |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should render linkedIn links", () => { |
| 82 | + render(<CfpSection2023 />); |
| 83 | + data.forEach((track) => { |
| 84 | + track.members |
| 85 | + .filter((member) => member.linkedIn !== "") |
| 86 | + .forEach((member) => { |
| 87 | + const linkedInLinks = screen.getAllByRole("link"); |
| 88 | + const linkedInLink = linkedInLinks.find( |
| 89 | + (link) => link.getAttribute("href") === member.linkedIn, |
| 90 | + ); |
| 91 | + expect(linkedInLink).toBeInTheDocument(); |
| 92 | + expect(linkedInLink).toHaveAttribute("href", member.linkedIn); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should update the document title", async () => { |
| 98 | + render(<CfpSection2023 />); |
| 99 | + await waitFor(() => { |
| 100 | + expect(document.title).toBe( |
| 101 | + `CFP Committee - DevBcn - ${conferenceData.edition}`, |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should not render the icons when the width is smaller than the breakpoint", () => { |
| 107 | + (useWindowSize as jest.Mock).mockReturnValue({ width: 767 }); |
| 108 | + render(<CfpSection2023 />); |
| 109 | + const lessIcon = screen.queryByAltText("more than - icon"); |
| 110 | + const moreIcon = screen.queryByAltText("Less than - icon"); |
| 111 | + expect(lessIcon).not.toBeInTheDocument(); |
| 112 | + expect(moreIcon).not.toBeInTheDocument(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments