|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { usePathname } from "next/navigation"; |
| 3 | +import { beforeEach, describe, expect, it, type Mock } from "vitest"; |
| 4 | + |
| 5 | +import { AdminNavigation } from "../AdminNavigation"; |
| 6 | + |
| 7 | +describe("AdminNavigation", () => { |
| 8 | + beforeEach(() => { |
| 9 | + (usePathname as Mock).mockReturnValue("/admin"); |
| 10 | + }); |
| 11 | + |
| 12 | + it("renders a sidemenu nav with the admin title", () => { |
| 13 | + render(<AdminNavigation />); |
| 14 | + expect( |
| 15 | + screen.getByRole("navigation", { name: "Administration" }), |
| 16 | + ).toBeInTheDocument(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("renders all admin links", () => { |
| 20 | + render(<AdminNavigation />); |
| 21 | + expect(screen.getByRole("link", { name: "Accueil" })).toBeInTheDocument(); |
| 22 | + expect( |
| 23 | + screen.getByRole("link", { name: "Mimoquer un Siren" }), |
| 24 | + ).toBeInTheDocument(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("marks /admin as active when on /admin", () => { |
| 28 | + (usePathname as Mock).mockReturnValue("/admin"); |
| 29 | + render(<AdminNavigation />); |
| 30 | + const activeLink = screen.getByRole("link", { name: "Accueil" }); |
| 31 | + expect(activeLink).toHaveAttribute("aria-current", "page"); |
| 32 | + expect(activeLink.closest("li")).toHaveClass( |
| 33 | + "fr-sidemenu__item", |
| 34 | + "fr-sidemenu__item--active", |
| 35 | + ); |
| 36 | + expect( |
| 37 | + screen.getByRole("link", { name: "Mimoquer un Siren" }), |
| 38 | + ).not.toHaveAttribute("aria-current"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("marks /admin/impersonate as active when on that page", () => { |
| 42 | + (usePathname as Mock).mockReturnValue("/admin/impersonate"); |
| 43 | + render(<AdminNavigation />); |
| 44 | + expect( |
| 45 | + screen.getByRole("link", { name: "Mimoquer un Siren" }), |
| 46 | + ).toHaveAttribute("aria-current", "page"); |
| 47 | + expect(screen.getByRole("link", { name: "Accueil" })).not.toHaveAttribute( |
| 48 | + "aria-current", |
| 49 | + ); |
| 50 | + }); |
| 51 | +}); |
0 commit comments