Files
co-lite/solvway-admin-portal/app/components/app/Header.test.tsx
T
ngqileandClaude Opus 5.5 1071ddaa2a feat(admin-portal): cut babyboo portal down to Co-Lite login, users and roles
Removes every other screen, API route, component, test and the dependencies only
they used. Cookie colite_admin, Co-Lite branding, BO_USERS/BO_ROLES modules only.
The user form no longer loads branches, events or country scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 21:24:30 +08:00

152 lines
5.6 KiB
TypeScript

import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi, beforeEach } from "vitest";
const nav = vi.hoisted(() => ({ replace: vi.fn(), refresh: vi.fn() }));
vi.mock("next/navigation", () => ({
useRouter: () => ({ replace: nav.replace, refresh: nav.refresh }),
usePathname: () => "/orders/web",
}));
// The mobile drawer (fix-admin-shell-foundation) renders the same aclMenu-derived tree as the sidebar.
vi.mock("@/app/lib/useAclSidebar", () => ({
useAclSidebar: () => [
{ name: "Dashboard", url: "/", children: [] },
{ name: "Sales & Orders", url: "", children: [{ name: "Sales Listing", url: "/orders/web", children: [] }] },
],
}));
const auth = vi.hoisted(() => ({ name: "Admin User" as string | null }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({ name: auth.name, hasModule: () => false }),
}));
vi.mock("@/app/lib/theme", () => ({
useTheme: () => ({ theme: "light", toggleTheme: vi.fn() }),
}));
import Header from "./Header";
import Sidebar from "./Sidebar";
beforeEach(() => {
nav.replace.mockReset();
nav.refresh.mockReset();
auth.name = "Admin User";
global.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({ ok: true }), { status: 200 }));
});
describe("Header", () => {
it("shows the signed-in user's name from the auth context", () => {
render(<Header />);
expect(screen.getByText("Admin User")).toBeInTheDocument();
});
// Global constraint: logout calls the same-origin /api/auth/logout route
// (never the mobile-api directly), then routes to /login.
it("logout calls /api/auth/logout then navigates to /login", async () => {
const user = userEvent.setup();
render(<Header />);
await user.click(screen.getByRole("button", { name: /admin user/i }));
await user.click(await screen.findByText(/logout/i));
await waitFor(() =>
expect(global.fetch).toHaveBeenCalledWith("/api/auth/logout", expect.objectContaining({ method: "POST" })),
);
await waitFor(() => expect(nav.replace).toHaveBeenCalledWith("/login"));
});
it("still navigates to /login even if the logout request fails", async () => {
global.fetch = vi.fn().mockRejectedValue(new Error("network down"));
const user = userEvent.setup();
render(<Header />);
await user.click(screen.getByRole("button", { name: /admin user/i }));
await user.click(await screen.findByText(/logout/i));
await waitFor(() => expect(nav.replace).toHaveBeenCalledWith("/login"));
});
// Spec: "The Portal Provides A Locale Switcher" -- the switcher lives in
// the header alongside the user menu and theme toggle.
it("includes the locale switcher", async () => {
const user = userEvent.setup();
render(<Header />);
await user.click(screen.getByRole("button", { name: /change language/i }));
expect(screen.getByText("简体中文")).toBeInTheDocument();
});
});
// fix-admin-shell-foundation: below lg the sidebar is hidden, so the header carries the only way
// to navigate.
describe("Header mobile navigation", () => {
it("shows a menu button only below lg", () => {
render(<Header />);
const button = screen.getByRole("button", { name: "Open menu" });
expect(button.className).toContain("lg:hidden");
});
it("opens the full navigation tree in a drawer", async () => {
const user = userEvent.setup();
render(<Header />);
await user.click(screen.getByRole("button", { name: "Open menu" }));
const drawer = await screen.findByRole("dialog");
expect(drawer).toHaveTextContent("Dashboard");
expect(drawer).toHaveTextContent("Sales Listing");
});
// the drawer always shows the full, labelled tree, even with the desktop sidebar collapsed.
it("shows the labelled tree in the drawer while the desktop sidebar is collapsed", async () => {
localStorage.setItem("bb_sidebar_collapsed", "1");
try {
const user = userEvent.setup();
render(
<>
<Sidebar />
<Header />
</>,
);
await waitFor(() =>
expect(screen.getByRole("complementary")).toHaveAttribute("data-collapsed", "true"),
);
await user.click(screen.getByRole("button", { name: "Open menu" }));
const drawer = await screen.findByRole("dialog");
expect(within(drawer).getByRole("link", { name: "Dashboard" })).toHaveTextContent("Dashboard");
expect(within(drawer).getByRole("button", { name: "Sales & Orders" })).toHaveTextContent("Sales & Orders");
expect(drawer).toHaveTextContent("Sales Listing");
} finally {
localStorage.clear();
}
});
it("closes the drawer when the admin follows a link", async () => {
const user = userEvent.setup();
render(<Header />);
await user.click(screen.getByRole("button", { name: "Open menu" }));
const drawer = await screen.findByRole("dialog");
const link = [...drawer.querySelectorAll("a")].find((a) => a.textContent === "Dashboard")!;
await user.click(link);
await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());
});
it("closes the drawer on Escape", async () => {
const user = userEvent.setup();
render(<Header />);
await user.click(screen.getByRole("button", { name: "Open menu" }));
await screen.findByRole("dialog");
await user.keyboard("{Escape}");
await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());
});
it("shows breadcrumbs for the current page", () => {
render(<Header />);
expect(screen.getByRole("navigation", { name: "breadcrumb" })).toHaveTextContent("Sales Listing");
});
});