|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { Header } from "./header"; |
| 5 | + |
| 6 | +// Mock next/link |
| 7 | +vi.mock("next/link", () => ({ |
| 8 | + default: ({ |
| 9 | + children, |
| 10 | + href, |
| 11 | + ...props |
| 12 | + }: { |
| 13 | + children: React.ReactNode; |
| 14 | + href: string; |
| 15 | + }) => ( |
| 16 | + <a href={href} {...props}> |
| 17 | + {children} |
| 18 | + </a> |
| 19 | + ), |
| 20 | +})); |
| 21 | + |
| 22 | +// Mock next-themes |
| 23 | +vi.mock("next-themes", () => ({ |
| 24 | + useTheme: () => ({ |
| 25 | + theme: "light", |
| 26 | + setTheme: vi.fn(), |
| 27 | + }), |
| 28 | +})); |
| 29 | + |
| 30 | +describe("Header", () => { |
| 31 | + it("renders with logo and navigation", () => { |
| 32 | + render(<Header />); |
| 33 | + |
| 34 | + // Check logo |
| 35 | + expect(screen.getByText("GhostPaste")).toBeInTheDocument(); |
| 36 | + |
| 37 | + // Check desktop navigation links |
| 38 | + expect(screen.getByRole("link", { name: "Create" })).toBeInTheDocument(); |
| 39 | + expect(screen.getByRole("link", { name: "About" })).toBeInTheDocument(); |
| 40 | + expect(screen.getByText("GitHub")).toBeInTheDocument(); |
| 41 | + |
| 42 | + // Check theme toggles (both desktop and mobile) |
| 43 | + const themeToggles = screen.getAllByRole("button", { |
| 44 | + name: "Toggle theme", |
| 45 | + }); |
| 46 | + expect(themeToggles).toHaveLength(2); // One for desktop, one for mobile |
| 47 | + }); |
| 48 | + |
| 49 | + it("includes skip to main content link", () => { |
| 50 | + render(<Header />); |
| 51 | + |
| 52 | + const skipLink = screen.getByText("Skip to main content"); |
| 53 | + expect(skipLink).toBeInTheDocument(); |
| 54 | + expect(skipLink).toHaveClass("sr-only"); |
| 55 | + expect(skipLink).toHaveAttribute("href", "#main-content"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("shows mobile menu button on small screens", () => { |
| 59 | + render(<Header />); |
| 60 | + |
| 61 | + const mobileMenuButton = screen.getByRole("button", { |
| 62 | + name: "Open navigation menu", |
| 63 | + }); |
| 64 | + expect(mobileMenuButton).toBeInTheDocument(); |
| 65 | + expect(mobileMenuButton).toHaveClass("md:hidden"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("opens and closes mobile menu", async () => { |
| 69 | + const user = userEvent.setup(); |
| 70 | + render(<Header />); |
| 71 | + |
| 72 | + const mobileMenuButton = screen.getByRole("button", { |
| 73 | + name: "Open navigation menu", |
| 74 | + }); |
| 75 | + |
| 76 | + // Open menu |
| 77 | + await user.click(mobileMenuButton); |
| 78 | + |
| 79 | + // Check if sheet content is visible |
| 80 | + expect(screen.getByRole("dialog")).toBeInTheDocument(); |
| 81 | + |
| 82 | + // Check mobile navigation links |
| 83 | + const mobileNav = screen.getByRole("dialog"); |
| 84 | + expect(mobileNav).toContainElement(screen.getAllByText("Create")[1]); |
| 85 | + expect(mobileNav).toContainElement(screen.getAllByText("About")[1]); |
| 86 | + expect(mobileNav).toContainElement(screen.getAllByText("GitHub")[1]); |
| 87 | + |
| 88 | + // Close menu by clicking a link |
| 89 | + const createLink = screen.getAllByText("Create")[1]; |
| 90 | + await user.click(createLink); |
| 91 | + |
| 92 | + // Menu should be closed (dialog removed from DOM) |
| 93 | + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("has correct link hrefs", () => { |
| 97 | + render(<Header />); |
| 98 | + |
| 99 | + // Desktop links |
| 100 | + const desktopCreateLink = screen.getByRole("link", { name: "Create" }); |
| 101 | + expect(desktopCreateLink).toHaveAttribute("href", "/create"); |
| 102 | + |
| 103 | + const desktopAboutLink = screen.getByRole("link", { name: "About" }); |
| 104 | + expect(desktopAboutLink).toHaveAttribute("href", "/about"); |
| 105 | + |
| 106 | + // GitHub link (external) |
| 107 | + const githubLinks = screen.getAllByRole("link", { name: /GitHub/i }); |
| 108 | + githubLinks.forEach((link) => { |
| 109 | + expect(link).toHaveAttribute( |
| 110 | + "href", |
| 111 | + "https://github.com/nullcoder/ghostpaste" |
| 112 | + ); |
| 113 | + expect(link).toHaveAttribute("target", "_blank"); |
| 114 | + expect(link).toHaveAttribute("rel", "noopener noreferrer"); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it("logo links to home page", () => { |
| 119 | + render(<Header />); |
| 120 | + |
| 121 | + const logoLink = screen.getByRole("link", { name: /GhostPaste/i }); |
| 122 | + expect(logoLink).toHaveAttribute("href", "/"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("has sticky positioning", () => { |
| 126 | + render(<Header />); |
| 127 | + |
| 128 | + const header = screen.getByRole("banner"); |
| 129 | + expect(header).toHaveClass("sticky", "top-0", "z-50"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("applies proper backdrop blur", () => { |
| 133 | + render(<Header />); |
| 134 | + |
| 135 | + const header = screen.getByRole("banner"); |
| 136 | + expect(header).toHaveClass( |
| 137 | + "bg-background/95", |
| 138 | + "backdrop-blur", |
| 139 | + "supports-[backdrop-filter]:bg-background/60" |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it("mobile menu closes on escape key", async () => { |
| 144 | + const user = userEvent.setup(); |
| 145 | + render(<Header />); |
| 146 | + |
| 147 | + const mobileMenuButton = screen.getByRole("button", { |
| 148 | + name: "Open navigation menu", |
| 149 | + }); |
| 150 | + |
| 151 | + // Open menu |
| 152 | + await user.click(mobileMenuButton); |
| 153 | + expect(screen.getByRole("dialog")).toBeInTheDocument(); |
| 154 | + |
| 155 | + // Press escape |
| 156 | + await user.keyboard("{Escape}"); |
| 157 | + |
| 158 | + // Menu should be closed |
| 159 | + expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); |
| 160 | + }); |
| 161 | + |
| 162 | + it("mobile menu has proper aria labels", async () => { |
| 163 | + const user = userEvent.setup(); |
| 164 | + render(<Header />); |
| 165 | + |
| 166 | + const mobileMenuButton = screen.getByRole("button", { |
| 167 | + name: "Open navigation menu", |
| 168 | + }); |
| 169 | + |
| 170 | + await user.click(mobileMenuButton); |
| 171 | + |
| 172 | + const dialog = screen.getByRole("dialog"); |
| 173 | + expect(dialog).toBeInTheDocument(); |
| 174 | + |
| 175 | + // Check close button |
| 176 | + const closeButton = screen.getByRole("button", { name: "Close" }); |
| 177 | + expect(closeButton).toBeInTheDocument(); |
| 178 | + }); |
| 179 | +}); |
0 commit comments