Files
ngqileandClaude Opus 5.5 75e243f3bb feat: sidebar groups pages into Events, Leads, Access and System
Labelled sections replace the single Administration divider, Login
security gets its own lock icon, and the icon map drops the babyboo
icons Co-Lite does not use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-12-02 00:19:18 +08:00

524 lines
21 KiB
TypeScript

import { render, screen, waitFor, within } from "@testing-library/react";
import { renderToString } from "react-dom/server";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, it, expect, vi } from "vitest";
import type { SidebarLink } from "@/app/lib/aclSidebar";
import { MessagesProvider } from "@/app/lib/i18n/MessagesProvider";
// Covers spec "The Sidebar Renders Lucide Icons From icon_name" at the
// component level: a BO-shaped aclMenu fixture (Dashboard/Members/Reports/
// Network/Messages) renders with its seeded lucide icons, and an
// unknown/absent iconName renders the neutral fallback without throwing.
// Sidebar's own link/structure/gating behaviour (from add-admin-portal-auth)
// is exercised elsewhere (aclSidebar.test.ts, useAclSidebar.test.tsx) and is
// not re-tested here.
const state = vi.hoisted(() => ({ links: [] as SidebarLink[], pathname: "/" }));
vi.mock("next/navigation", () => ({
usePathname: () => state.pathname,
}));
vi.mock("@/app/lib/useAclSidebar", () => ({
useAclSidebar: () => state.links,
}));
import Sidebar from "./Sidebar";
// Icons are Co-Lite seed icons (group-sidebar-categories trimmed the babyboo-only ones).
const BO_FIXTURE: SidebarLink[] = [
{ name: "Dashboard", url: "/", iconName: "LayoutDashboard", children: [] },
{ name: "Members", url: "/members", iconName: "Users", children: [] },
{ name: "Reports", url: "/reports", iconName: "Mail", children: [] },
{ name: "Network", url: "/network", iconName: "ScrollText", children: [] },
{ name: "Messages", url: "/messages", iconName: "Tags", children: [] },
];
describe("Sidebar icon rendering", () => {
it("renders the BO-shaped aclMenu fixture with each entry's seeded lucide icon", () => {
state.links = BO_FIXTURE;
const { container } = render(<Sidebar />);
expect(screen.getByText("Dashboard")).toBeInTheDocument();
expect(screen.getByText("Messages")).toBeInTheDocument();
expect(container.querySelector("svg.lucide-layout-dashboard")).not.toBeNull();
expect(container.querySelector("svg.lucide-users")).not.toBeNull();
expect(container.querySelector("svg.lucide-mail")).not.toBeNull();
expect(container.querySelector("svg.lucide-scroll-text")).not.toBeNull();
expect(container.querySelector("svg.lucide-tags")).not.toBeNull();
});
it("renders the neutral fallback icon, and does not throw, for an unknown or absent iconName", () => {
state.links = [
{ name: "Mystery", url: "/mystery", iconName: "NotARealLucideIconName", children: [] },
{ name: "NoIcon", url: "/no-icon", children: [] },
];
let container!: HTMLElement;
expect(() => {
container = render(<Sidebar />).container;
}).not.toThrow();
expect(screen.getByText("Mystery")).toBeInTheDocument();
expect(screen.getByText("NoIcon")).toBeInTheDocument();
expect(container.querySelectorAll("svg.lucide-circle")).toHaveLength(2);
});
});
// Spec (add-sidebar-i18n-labels): "Sidebar Labels Resolve Through i18n By
// Module Name" -- module.name is now a namespaced i18n code (e.g.
// BO_USERS), not a display string, so the sidebar must render t(name)
// rather than the raw code.
const BO_CODE_FIXTURE: SidebarLink[] = [
{ name: "BO_USERS", url: "/", iconName: "LayoutDashboard", children: [] },
{ name: "BO_ROLES", url: "/members", iconName: "Users", children: [] },
{ name: "BO_ROOT", url: "/network", iconName: "Network", children: [] },
];
describe("Sidebar label resolution via i18n t(name)", () => {
it("renders the resolved English labels (from defaultMessages), not the raw BO_* codes", () => {
state.links = BO_CODE_FIXTURE;
render(<Sidebar />);
expect(screen.getByText("Users")).toBeInTheDocument();
expect(screen.getByText("Roles")).toBeInTheDocument();
expect(screen.getByText("Co-Lite admin")).toBeInTheDocument();
expect(screen.queryByText("BO_USERS")).toBeNull();
expect(screen.queryByText("BO_ROLES")).toBeNull();
expect(screen.queryByText("BO_ROOT")).toBeNull();
});
it("renders a catalog override when the active locale has one (i18n actually resolves live)", () => {
state.links = BO_CODE_FIXTURE;
render(
<MessagesProvider locale="zh_CN" messages={{ BO_USERS: "仪表盘" }}>
<Sidebar />
</MessagesProvider>,
);
expect(screen.getByText("仪表盘")).toBeInTheDocument();
expect(screen.queryByText("Users")).toBeNull();
// Sibling codes with no catalog entry still fall back to their English default.
expect(screen.getByText("Roles")).toBeInTheDocument();
});
it("falls back to the raw code, without throwing, for a code with no catalog or default entry", () => {
state.links = [
{ name: "BO_TOTALLY_UNKNOWN", url: "/unknown", children: [] },
];
let container!: HTMLElement;
expect(() => {
container = render(<Sidebar />).container;
}).not.toThrow();
expect(screen.getByText("BO_TOTALLY_UNKNOWN")).toBeInTheDocument();
void container;
});
it("groups with children also resolve their header label via i18n", () => {
state.links = [
{
name: "BO_USERS",
url: "",
children: [{ name: "BO_ROLES", url: "/messages", children: [] }],
},
];
render(<Sidebar />);
expect(screen.getByText("Users")).toBeInTheDocument();
expect(screen.queryByText("BO_USERS")).toBeNull();
});
});
// fix-admin-sidebar-groups-icons: a group's own url is "", which used to match every path
// (pathname.startsWith("/")), so every group started expanded on every page.
describe("Sidebar group initial expansion", () => {
const GROUPS: SidebarLink[] = [
{ name: "Sales", url: "", children: [{ name: "Refunds", url: "/orders/refunds", children: [] }] },
{ name: "Stock", url: "", children: [{ name: "Stock in", url: "/inventory/stock-in", children: [] }] },
];
// Scoped to the nav: the brand row's collapse toggle also carries aria-expanded.
const expanded = () =>
within(screen.getByRole("navigation", { name: "Main" }))
.getAllByRole("button")
.map((b) => `${b.textContent}=${b.getAttribute("aria-expanded")}`);
afterEach(() => {
state.pathname = "/";
});
it("starts only the group holding the current page expanded", () => {
state.links = GROUPS;
state.pathname = "/orders/refunds";
render(<Sidebar />);
expect(expanded()).toEqual(["Sales=true", "Stock=false"]);
});
it("starts every group collapsed on the dashboard", () => {
state.links = GROUPS;
state.pathname = "/";
render(<Sidebar />);
expect(expanded()).toEqual(["Sales=false", "Stock=false"]);
});
it("opens a collapsed group when a navigation outside the sidebar lands on one of its pages", () => {
state.links = GROUPS;
state.pathname = "/orders/refunds";
const { rerender } = render(<Sidebar />);
state.pathname = "/inventory/stock-in";
rerender(<Sidebar />);
expect(expanded()).toEqual(["Sales=true", "Stock=true"]);
});
});
describe("Containerverse credit", () => {
it("is shown on the sidebar, linked to containerverse.dev", () => {
state.links = [];
render(<Sidebar />);
const link = screen.getByRole("link", { name: "Containerverse Sdn. Bhd." });
expect(link).toHaveAttribute("href", "https://containerverse.dev/");
expect(link).toHaveAttribute("target", "_blank");
expect(link).toHaveAttribute("rel", "noopener noreferrer");
expect(link.parentElement?.textContent).toBe("Powered by Containerverse Sdn. Bhd.");
});
// A class check is the most jsdom can prove (no layout engine); the real behaviour is Playwright-verified.
it("stays in view: the sidebar is sticky and one screen tall, so the credit is visible without scrolling", () => {
state.links = [];
render(<Sidebar />);
const aside = screen.getByRole("link", { name: "Containerverse Sdn. Bhd." }).closest("aside");
expect(aside).toHaveClass("sticky", "top-0", "h-svh");
});
});
// fix-admin-shell-foundation: a nested current page was marked by font weight alone, and a
// collapsed group gave no hint that it held the current page.
describe("Sidebar current-page marking", () => {
const GROUPS: SidebarLink[] = [
{ name: "Sales", url: "", children: [{ name: "Promotions", url: "/promotions", children: [] }] },
{ name: "Stock", url: "", children: [{ name: "Stock in", url: "/inventory/stock-in", children: [] }] },
];
afterEach(() => {
state.pathname = "/";
});
it("fills a nested current page, not just bolds it", () => {
state.links = GROUPS;
state.pathname = "/promotions";
render(<Sidebar />);
const link = screen.getByRole("link", { name: "Promotions" });
expect(link).toHaveAttribute("aria-current", "page");
expect(link.className).toContain("bg-sidebar-accent");
});
it("marks the group holding the current page and no other group", () => {
state.links = GROUPS;
state.pathname = "/promotions";
render(<Sidebar />);
expect(screen.getByRole("button", { name: "Sales" })).toHaveAttribute("data-current", "true");
expect(screen.getByRole("button", { name: "Stock" })).not.toHaveAttribute("data-current");
});
});
// fix-admin-shell-foundation: each link used to match on its own prefix, so /pos (Event POS) was
// also "current" on /pos/counter (Branch counter). Only the most specific match is current.
describe("Sidebar marks one current page", () => {
const POS: SidebarLink[] = [
{ name: "Sales", url: "", children: [{ name: "Branch counter", url: "/pos/counter", children: [] }] },
{ name: "Event", url: "", children: [{ name: "Event POS", url: "/pos", children: [] }] },
];
afterEach(() => {
state.pathname = "/";
});
it("marks only the most specific match on a nested url", () => {
state.links = POS;
state.pathname = "/pos/counter";
render(<Sidebar />);
expect(screen.getByRole("link", { name: "Branch counter" })).toHaveAttribute("aria-current", "page");
expect(screen.getByRole("button", { name: "Sales" })).toHaveAttribute("data-current", "true");
expect(screen.getByRole("button", { name: "Event" })).toHaveAttribute("aria-expanded", "false");
expect(screen.getByRole("button", { name: "Event" })).not.toHaveAttribute("data-current");
});
it("still marks the shorter url on its own page", () => {
state.links = POS;
state.pathname = "/pos";
render(<Sidebar />);
expect(screen.getByRole("link", { name: "Event POS" })).toHaveAttribute("aria-current", "page");
expect(screen.getByRole("button", { name: "Sales" })).not.toHaveAttribute("data-current");
});
});
// admin-portal-shell: "The Desktop Sidebar Collapses To An Icon Rail" and
// "The Icon Rail Keeps Every Entry Reachable". jsdom has no layout, so the rail is proven by the
// aside's data-collapsed attribute and its width token; Playwright covers the real widths.
describe("Sidebar icon rail", () => {
const RAIL: SidebarLink[] = [
{ name: "Dashboard", url: "/", iconName: "LayoutDashboard", children: [] },
{
name: "Sales & orders",
url: "",
iconName: "ShoppingCart",
children: [
{ name: "Sales listing", url: "/orders/web", children: [] },
{ name: "Refunds", url: "/orders/refunds", children: [] },
],
},
{ name: "Stock", url: "", iconName: "Boxes", children: [{ name: "Stock in", url: "/inventory/stock-in", children: [] }] },
];
const aside = () => screen.getByRole("complementary");
beforeEach(() => {
localStorage.clear();
state.links = RAIL;
state.pathname = "/orders/refunds";
});
afterEach(() => {
vi.restoreAllMocks();
localStorage.clear();
state.pathname = "/";
});
it("starts expanded with a Collapse sidebar toggle", () => {
render(<Sidebar />);
const toggle = screen.getByRole("button", { name: "Collapse sidebar" });
expect(toggle).toHaveAttribute("aria-expanded", "true");
expect(aside()).toHaveAttribute("data-collapsed", "false");
expect(aside().style.width).toBe("var(--sidebar-width)");
});
it("collapses to the rail width and expands back, switching the toggle's name and aria-expanded", async () => {
const user = userEvent.setup();
render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
const expand = screen.getByRole("button", { name: "Expand sidebar" });
expect(expand).toHaveAttribute("aria-expanded", "false");
expect(aside()).toHaveAttribute("data-collapsed", "true");
expect(aside().style.width).toBe("var(--sidebar-width-collapsed)");
await user.click(expand);
expect(screen.getByRole("button", { name: "Collapse sidebar" })).toHaveAttribute("aria-expanded", "true");
expect(aside().style.width).toBe("var(--sidebar-width)");
});
it("remembers the collapsed choice across a remount (bb_sidebar_collapsed)", async () => {
const user = userEvent.setup();
const { unmount } = render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
expect(localStorage.getItem("bb_sidebar_collapsed")).toBe("1");
unmount();
render(<Sidebar />);
await waitFor(() => expect(aside()).toHaveAttribute("data-collapsed", "true"));
expect(screen.getByRole("button", { name: "Expand sidebar" })).toBeInTheDocument();
});
it("starts expanded and still toggles when storage throws", async () => {
vi.spyOn(Storage.prototype, "getItem").mockImplementation(() => {
throw new Error("blocked");
});
vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {
throw new Error("blocked");
});
const user = userEvent.setup();
render(<Sidebar />);
expect(aside()).toHaveAttribute("data-collapsed", "false");
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
expect(aside()).toHaveAttribute("data-collapsed", "true");
await user.click(screen.getByRole("button", { name: "Expand sidebar" }));
expect(aside()).toHaveAttribute("data-collapsed", "false");
});
it("shows a leaf as an icon-only link that keeps its label as accessible name and tooltip", async () => {
const user = userEvent.setup();
render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
const link = screen.getByRole("link", { name: "Dashboard" });
expect(link).toHaveAttribute("href", "/");
expect(link).toHaveAttribute("title", "Dashboard");
expect(within(aside()).queryByText("Dashboard")).toBeNull();
});
it("opens a flyout from a group icon listing its children as links, and closes it on follow", async () => {
const user = userEvent.setup();
render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
const group = screen.getByRole("button", { name: "Sales & orders" });
expect(group).toHaveAttribute("title", "Sales & orders");
await user.click(group);
const flyout = await screen.findByRole("dialog", { name: "Sales & orders" });
expect(within(flyout).getByRole("link", { name: "Sales listing" })).toHaveAttribute("href", "/orders/web");
const current = within(flyout).getByRole("link", { name: "Refunds" });
expect(current).toHaveAttribute("aria-current", "page");
await user.click(current);
await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());
});
it("keeps the active fill on the group holding the current page, and no other", async () => {
const user = userEvent.setup();
render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
const sales = screen.getByRole("button", { name: "Sales & orders" });
const stock = screen.getByRole("button", { name: "Stock" });
expect(sales).toHaveAttribute("data-current", "true");
expect(sales).toHaveClass("bg-sidebar-accent", "text-sidebar-accent-foreground");
expect(stock).not.toHaveAttribute("data-current");
expect(stock).not.toHaveClass("bg-sidebar-accent");
});
it("keeps a current top-level leaf filled in the rail", async () => {
state.pathname = "/";
const user = userEvent.setup();
render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
const link = screen.getByRole("link", { name: "Dashboard" });
expect(link).toHaveAttribute("aria-current", "page");
expect(link).toHaveClass("bg-sidebar-accent", "text-sidebar-accent-foreground");
});
it("keeps the footer credit visible in a compact form", async () => {
const user = userEvent.setup();
render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
// WCAG 2.5.3 Label in Name: the accessible name starts with the visible "CV".
const credit = screen.getByRole("link", { name: "CV Containerverse Sdn. Bhd." });
expect(credit).not.toHaveAttribute("aria-label");
expect(credit).toHaveAttribute("href", "https://containerverse.dev/");
expect(credit.closest("aside")).toBe(aside());
});
it("keeps the brand row header-high with only the toggle while collapsed, and brings the logo back on expand", async () => {
const user = userEvent.setup();
render(<Sidebar />);
const brandRow = () => screen.getByRole("button", { name: /sidebar/ }).parentElement!;
expect(within(brandRow()).getByRole("link", { name: /Co-Lite/ })).toHaveAttribute("href", "/");
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
expect(within(brandRow()).queryByRole("link")).toBeNull();
expect(within(brandRow()).getByRole("button", { name: "Expand sidebar" })).toBeInTheDocument();
expect(brandRow()).toHaveClass("h-14", "justify-center");
await user.click(screen.getByRole("button", { name: "Expand sidebar" }));
expect(within(brandRow()).getByRole("link", { name: /Co-Lite/ })).toBeInTheDocument();
});
// A stored "collapsed" must not animate 272 -> 76px on every page load; only a toggle animates.
it("does not animate the width on load, only after a toggle", async () => {
localStorage.setItem("bb_sidebar_collapsed", "1");
const user = userEvent.setup();
render(<Sidebar />);
await waitFor(() => expect(aside()).toHaveAttribute("data-collapsed", "true"));
expect(aside().className).not.toContain("transition-[width]");
await user.click(screen.getByRole("button", { name: "Expand sidebar" }));
expect(aside().className).toContain("transition-[width]");
expect(aside()).toHaveClass("motion-reduce:transition-none");
});
it("renders expanded on the server even with a stored collapse, so hydration agrees", () => {
localStorage.setItem("bb_sidebar_collapsed", "1");
const html = renderToString(<Sidebar />);
expect(html).toContain('data-collapsed="false"');
expect(html).not.toContain("transition-[width]");
});
it("remembers an expand too: expanding writes 0 and a remount stays expanded", async () => {
localStorage.setItem("bb_sidebar_collapsed", "1");
const user = userEvent.setup();
const { unmount } = render(<Sidebar />);
await user.click(await screen.findByRole("button", { name: "Expand sidebar" }));
expect(localStorage.getItem("bb_sidebar_collapsed")).toBe("0");
unmount();
render(<Sidebar />);
// Give the mount effect its chance to (wrongly) collapse before asserting.
await new Promise((r) => setTimeout(r, 0));
expect(aside()).toHaveAttribute("data-collapsed", "false");
expect(screen.getByRole("button", { name: "Collapse sidebar" })).toBeInTheDocument();
});
it("closes an open flyout when the page changes from outside it (e.g. the back button)", async () => {
const user = userEvent.setup();
const { rerender } = render(<Sidebar />);
await user.click(screen.getByRole("button", { name: "Collapse sidebar" }));
await user.click(screen.getByRole("button", { name: "Sales & orders" }));
await screen.findByRole("dialog", { name: "Sales & orders" });
state.pathname = "/inventory/stock-in";
rerender(<Sidebar />);
await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());
});
});
// add-visual-foundation: Product Wordmark / One Accent Colour.
describe("Sidebar visual identity", () => {
afterEach(() => {
state.pathname = "/";
});
it("shows the Co-Lite mark and a solid wordmark, not gradient-filled text", () => {
state.links = [];
const { container } = render(<Sidebar />);
const word = screen.getByText("Co-Lite");
expect(word).not.toHaveClass("brand-wordmark");
expect(word.className).not.toMatch(/gradient|text-transparent/);
expect(container.querySelector("[data-slot='brand-mark']")).not.toBeNull();
});
it("marks only the current top-level item in the accent, not a solid primary fill", () => {
state.links = [
{ name: "Events", url: "/events", iconName: "Users", children: [] },
{ name: "Leads", url: "/leads", iconName: "Users", children: [] },
];
state.pathname = "/events";
render(<Sidebar />);
const events = screen.getByRole("link", { name: "Events" });
const leads = screen.getByRole("link", { name: "Leads" });
expect(events).toHaveAttribute("aria-current", "page");
expect(events).toHaveClass("bg-sidebar-accent", "text-sidebar-accent-foreground");
expect(events).not.toHaveClass("bg-sidebar-primary");
expect(leads).not.toHaveClass("bg-sidebar-accent");
expect(leads).not.toHaveClass("bg-sidebar-primary");
});
});