Skip to content

Commit e151270

Browse files
committed
fix(layout): tighten admin path match + real-component PublicChrome tests
1 parent d433038 commit e151270

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

packages/app/src/e2e/admin.e2e.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ test("admin user can access /admin and sees backoffice page", async ({
1313
test("admin routes hide the public footer and help banner", async ({
1414
page,
1515
}) => {
16+
// Runs in the authenticated `chromium` Playwright project (see
17+
// playwright.config.ts): `page.goto("/admin")` reaches the real backoffice
18+
// page, so the assertions below exercise the PublicChrome branch, not a
19+
// login-redirect fallback that would trivially pass.
1620
await page.goto("/admin");
21+
await expect(
22+
page.getByRole("heading", { name: "Backoffice", level: 1 }),
23+
).toBeVisible();
1724
await expect(page.locator("footer#footer")).toHaveCount(0);
1825
await expect(
1926
page.getByRole("region", { name: "Ressources et aide" }),

packages/app/src/modules/layout/PublicChrome.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { ResourceBanner } from "./ResourceBanner";
1212
*/
1313
export function PublicChrome() {
1414
const pathname = usePathname();
15-
if (pathname?.startsWith("/admin")) {
15+
// Match the `/admin` segment boundary so hypothetical sibling routes like
16+
// `/administrator` or `/admin-tools` keep the public chrome.
17+
if (pathname === "/admin" || pathname?.startsWith("/admin/")) {
1618
return null;
1719
}
1820
return (
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import { render, screen } from "@testing-library/react";
22
import { usePathname } from "next/navigation";
3-
import { beforeEach, describe, expect, it, type Mock, vi } from "vitest";
3+
import { beforeEach, describe, expect, it, type Mock } from "vitest";
44

55
import { PublicChrome } from "../PublicChrome";
66

7-
vi.mock("../Footer", () => ({
8-
Footer: () => <footer data-testid="public-footer">footer</footer>,
9-
}));
10-
vi.mock("../ResourceBanner", () => ({
11-
ResourceBanner: () => (
12-
<section data-testid="public-resource-banner">banner</section>
13-
),
14-
}));
7+
// Renders real Footer + ResourceBanner; only their external deps
8+
// (next/link, next/image, next/navigation) are mocked globally in test/setup.ts,
9+
// so a regression inside either landmark would fail this file.
1510

1611
describe("PublicChrome", () => {
1712
beforeEach(() => {
@@ -20,8 +15,10 @@ describe("PublicChrome", () => {
2015

2116
it("renders ResourceBanner + Footer on public routes", () => {
2217
render(<PublicChrome />);
23-
expect(screen.getByTestId("public-resource-banner")).toBeInTheDocument();
24-
expect(screen.getByTestId("public-footer")).toBeInTheDocument();
18+
expect(
19+
screen.getByRole("region", { name: /ressources et aide/i }),
20+
).toBeInTheDocument();
21+
expect(screen.getByRole("contentinfo")).toBeInTheDocument();
2522
});
2623

2724
it("renders nothing on /admin", () => {
@@ -39,6 +36,18 @@ describe("PublicChrome", () => {
3936
it("renders chrome on non-admin nested routes", () => {
4037
(usePathname as Mock).mockReturnValue("/mon-espace/declarations");
4138
render(<PublicChrome />);
42-
expect(screen.getByTestId("public-footer")).toBeInTheDocument();
39+
expect(
40+
screen.getByRole("region", { name: /ressources et aide/i }),
41+
).toBeInTheDocument();
42+
expect(screen.getByRole("contentinfo")).toBeInTheDocument();
43+
});
44+
45+
it("renders chrome on sibling routes whose name merely starts with 'admin'", () => {
46+
(usePathname as Mock).mockReturnValue("/administrator");
47+
render(<PublicChrome />);
48+
expect(
49+
screen.getByRole("region", { name: /ressources et aide/i }),
50+
).toBeInTheDocument();
51+
expect(screen.getByRole("contentinfo")).toBeInTheDocument();
4352
});
4453
});

0 commit comments

Comments
 (0)