|
1 | | -import { render, screen } from "@testing-library/react"; |
| 1 | +import { render, screen, fireEvent } from "@testing-library/react"; |
2 | 2 | import { FeaturedProjects } from "../FeaturedProjects"; |
3 | 3 |
|
4 | 4 | jest.mock("@/components/micro"); |
@@ -60,6 +60,126 @@ describe("FeaturedProjects", () => { |
60 | 60 | expect(projectCards).toHaveLength(0); |
61 | 61 | }); |
62 | 62 |
|
| 63 | + describe("More Projects Functionality", () => { |
| 64 | + const manyProjects = [ |
| 65 | + { |
| 66 | + title: "Test Project 1", |
| 67 | + description: "Test description 1", |
| 68 | + technologies: ["React", "Node.js"] |
| 69 | + }, |
| 70 | + { |
| 71 | + title: "Test Project 2", |
| 72 | + description: "Test description 2", |
| 73 | + technologies: ["Next.js", "PostgreSQL"] |
| 74 | + }, |
| 75 | + { |
| 76 | + title: "Test Project 3", |
| 77 | + description: "Test description 3", |
| 78 | + technologies: ["Vue", "MongoDB"] |
| 79 | + }, |
| 80 | + { |
| 81 | + title: "Test Project 4", |
| 82 | + description: "Test description 4", |
| 83 | + technologies: ["Angular", "MySQL"] |
| 84 | + }, |
| 85 | + { |
| 86 | + title: "Test Project 5", |
| 87 | + description: "Test description 5", |
| 88 | + technologies: ["Svelte", "Redis"] |
| 89 | + } |
| 90 | + ]; |
| 91 | + |
| 92 | + it("shows only first 3 projects initially when more than 3 projects exist", () => { |
| 93 | + render(<FeaturedProjects projects={manyProjects} />); |
| 94 | + |
| 95 | + const projectCards = screen.getAllByTestId("project-card"); |
| 96 | + expect(projectCards).toHaveLength(3); |
| 97 | + |
| 98 | + // Check that only first 3 projects are visible |
| 99 | + expect(screen.getByText("Test Project 1")).toBeInTheDocument(); |
| 100 | + expect(screen.getByText("Test Project 2")).toBeInTheDocument(); |
| 101 | + expect(screen.getByText("Test Project 3")).toBeInTheDocument(); |
| 102 | + expect(screen.queryByText("Test Project 4")).not.toBeInTheDocument(); |
| 103 | + expect(screen.queryByText("Test Project 5")).not.toBeInTheDocument(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("shows 'more...' button when more than 3 projects exist", () => { |
| 107 | + render(<FeaturedProjects projects={manyProjects} />); |
| 108 | + |
| 109 | + const moreButton = screen.getByRole("button", { name: /more/i }); |
| 110 | + expect(moreButton).toBeInTheDocument(); |
| 111 | + expect(moreButton).toHaveTextContent("more..."); |
| 112 | + }); |
| 113 | + |
| 114 | + it("does not show 'more...' button when 3 or fewer projects exist", () => { |
| 115 | + const threeProjects = manyProjects.slice(0, 3); |
| 116 | + render(<FeaturedProjects projects={threeProjects} />); |
| 117 | + |
| 118 | + const moreButton = screen.queryByRole("button", { name: /more/i }); |
| 119 | + expect(moreButton).not.toBeInTheDocument(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("shows all projects when 'more...' button is clicked", () => { |
| 123 | + render(<FeaturedProjects projects={manyProjects} />); |
| 124 | + |
| 125 | + const moreButton = screen.getByRole("button", { name: /more/i }); |
| 126 | + fireEvent.click(moreButton); |
| 127 | + |
| 128 | + const projectCards = screen.getAllByTestId("project-card"); |
| 129 | + expect(projectCards).toHaveLength(5); |
| 130 | + |
| 131 | + // Check that all projects are now visible |
| 132 | + expect(screen.getByText("Test Project 1")).toBeInTheDocument(); |
| 133 | + expect(screen.getByText("Test Project 2")).toBeInTheDocument(); |
| 134 | + expect(screen.getByText("Test Project 3")).toBeInTheDocument(); |
| 135 | + expect(screen.getByText("Test Project 4")).toBeInTheDocument(); |
| 136 | + expect(screen.getByText("Test Project 5")).toBeInTheDocument(); |
| 137 | + }); |
| 138 | + |
| 139 | + it("hides 'more...' button after it is clicked", () => { |
| 140 | + render(<FeaturedProjects projects={manyProjects} />); |
| 141 | + |
| 142 | + const moreButton = screen.getByRole("button", { name: /more/i }); |
| 143 | + fireEvent.click(moreButton); |
| 144 | + |
| 145 | + expect(screen.queryByRole("button", { name: /more/i })).not.toBeInTheDocument(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("shows additional projects with animations when 'more...' button is clicked", () => { |
| 149 | + render(<FeaturedProjects projects={manyProjects} />); |
| 150 | + |
| 151 | + const moreButton = screen.getByRole("button", { name: /more/i }); |
| 152 | + fireEvent.click(moreButton); |
| 153 | + |
| 154 | + // Additional projects should be rendered with motion.div wrapper |
| 155 | + const projectCards = screen.getAllByTestId("project-card"); |
| 156 | + expect(projectCards).toHaveLength(5); |
| 157 | + |
| 158 | + // The additional projects (4th and 5th) should be wrapped in motion.div |
| 159 | + const motionDivs = document.querySelectorAll('[data-testid="project-card"]'); |
| 160 | + expect(motionDivs.length).toBe(5); |
| 161 | + }); |
| 162 | + |
| 163 | + it("positions 'more...' button in bottom right corner", () => { |
| 164 | + render(<FeaturedProjects projects={manyProjects} />); |
| 165 | + |
| 166 | + const moreButton = screen.getByRole("button", { name: /more/i }); |
| 167 | + const buttonContainer = moreButton.closest('[data-testid="more-button-container"]'); |
| 168 | + |
| 169 | + expect(buttonContainer).toBeInTheDocument(); |
| 170 | + expect(buttonContainer).toHaveClass("flex", "justify-end", "mt-8"); |
| 171 | + }); |
| 172 | + |
| 173 | + it("uses link variant for 'more...' button", () => { |
| 174 | + render(<FeaturedProjects projects={manyProjects} />); |
| 175 | + |
| 176 | + const moreButton = screen.getByRole("button", { name: /more/i }); |
| 177 | + expect(moreButton).toHaveAttribute("data-testid", "more-projects-button"); |
| 178 | + // Since Button is mocked, we can't test the actual classes |
| 179 | + // The variant="link" prop is passed to the Button component |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
63 | 183 | describe("Dynamic Grid Layout", () => { |
64 | 184 | it("uses 2 columns layout for less than 3 projects", () => { |
65 | 185 | const oneProject = [mockProjects[0]]; |
|
0 commit comments