Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
83 lines
3.1 KiB
TypeScript
83 lines
3.1 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const nav = vi.hoisted(() => ({ replace: vi.fn() }));
|
|
let mockPathname = "/";
|
|
vi.mock("next/navigation", () => ({
|
|
useRouter: () => ({ replace: nav.replace }),
|
|
usePathname: () => mockPathname,
|
|
}));
|
|
|
|
const auth = vi.hoisted(() => ({ name: null as string | null, loading: true }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => auth,
|
|
}));
|
|
|
|
vi.mock("./Sidebar", () => ({ default: () => <div data-testid="sidebar" /> }));
|
|
vi.mock("./NewCodeNotice", () => ({ NewCodeNotice: () => <div data-testid="notice" /> }));
|
|
vi.mock("./Header", () => ({ default: () => <div data-testid="header" /> }));
|
|
|
|
import AppShell from "./AppShell";
|
|
|
|
beforeEach(() => {
|
|
nav.replace.mockReset();
|
|
mockPathname = "/";
|
|
auth.name = null;
|
|
auth.loading = true;
|
|
});
|
|
|
|
describe("AppShell", () => {
|
|
it("renders nothing (no flash) while session hydration is loading", () => {
|
|
auth.loading = true;
|
|
auth.name = null;
|
|
const { container } = render(<AppShell>content</AppShell>);
|
|
expect(container).toBeEmptyDOMElement();
|
|
});
|
|
|
|
// Ruling R3: session hydration (/api/auth/session via the context) found no
|
|
// session — either a genuinely absent cookie, or the expired-but-present-
|
|
// cookie case the edge middleware's presence-only check cannot catch.
|
|
it("redirects to /login when hydration finishes with no session", async () => {
|
|
auth.loading = false;
|
|
auth.name = null;
|
|
render(<AppShell>content</AppShell>);
|
|
await waitFor(() => expect(nav.replace).toHaveBeenCalledWith("/login"));
|
|
});
|
|
|
|
it("renders the authenticated shell (sidebar, header, children) once signed in", () => {
|
|
auth.loading = false;
|
|
auth.name = "Admin User";
|
|
render(<AppShell>content</AppShell>);
|
|
expect(screen.getByTestId("sidebar")).toBeInTheDocument();
|
|
expect(screen.getByTestId("header")).toBeInTheDocument();
|
|
expect(screen.getByText("content")).toBeInTheDocument();
|
|
expect(nav.replace).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows the new-code notice in the signed-in shell", () => {
|
|
auth.loading = false;
|
|
auth.name = "Admin User";
|
|
render(<AppShell>content</AppShell>);
|
|
expect(screen.getByTestId("notice")).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the Containerverse credit under the content on small screens (the sidebar is hidden there)", () => {
|
|
auth.loading = false;
|
|
auth.name = "Admin User";
|
|
render(<AppShell>content</AppShell>);
|
|
const link = screen.getByRole("link", { name: "Containerverse Sdn. Bhd." });
|
|
expect(link).toHaveAttribute("href", "https://containerverse.dev/");
|
|
expect(link.closest("[data-testid='mobile-credit']")).toHaveClass("lg:hidden");
|
|
});
|
|
|
|
it("does not redirect on a public page even with no session", () => {
|
|
mockPathname = "/login";
|
|
auth.loading = false;
|
|
auth.name = null;
|
|
render(<AppShell>content</AppShell>);
|
|
expect(nav.replace).not.toHaveBeenCalled();
|
|
expect(screen.getByText("content")).toBeInTheDocument();
|
|
expect(screen.queryByTestId("sidebar")).not.toBeInTheDocument();
|
|
});
|
|
});
|