Files
co-lite/solvway-admin-portal/app/components/app/Sidebar.sections.test.tsx
T
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

79 lines
3.9 KiB
TypeScript

import { render, screen, within } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import type { SidebarLink } from "@/app/lib/aclSidebar";
const state = vi.hoisted(() => ({ links: [] as SidebarLink[] }));
vi.mock("next/navigation", () => ({ usePathname: () => "/" }));
vi.mock("@/app/lib/useAclSidebar", () => ({ useAclSidebar: () => state.links }));
import { SidebarNav } from "./Sidebar";
const link = (name: string): SidebarLink => ({ name, url: `/${name}`, iconName: "Users", children: [] });
const names = (...n: string[]) => n.map(link);
const ALL = names(
"BO_DASHBOARD", "BO_EVENTS", "BO_EVENT_LEADS", "BO_INTERESTS", "BO_LEADS",
"BO_USERS", "BO_ROLES", "BO_EMAILS", "BO_LOGIN_SECURITY", "BO_AUDIT_LOG", "BO_COMPANIES",
);
const linkNames = (list: HTMLElement) => within(list).getAllByRole("link").map((a) => a.getAttribute("href")?.slice(1));
describe("Sidebar sections", () => {
it("lists Dashboard, then Events, Leads, Access and System sections in order", () => {
state.links = [...ALL].reverse();
render(<SidebarNav />);
const lists = screen.getAllByRole("list");
expect(lists).toHaveLength(5);
expect(linkNames(lists[0])).toEqual(["BO_DASHBOARD"]);
expect(linkNames(screen.getByRole("list", { name: "Events" }))).toEqual(["BO_EVENTS", "BO_EVENT_LEADS"]);
expect(linkNames(screen.getByRole("list", { name: "Leads" }))).toEqual(["BO_LEADS", "BO_INTERESTS"]);
expect(linkNames(screen.getByRole("list", { name: "Access" }))).toEqual(["BO_USERS", "BO_ROLES", "BO_LOGIN_SECURITY"]);
expect(linkNames(screen.getByRole("list", { name: "System" }))).toEqual(["BO_COMPANIES", "BO_EMAILS", "BO_AUDIT_LOG"]);
expect(screen.getAllByRole("link")).toHaveLength(11);
});
it("shows only the sections the user has pages in", () => {
state.links = names("BO_EVENTS", "BO_EVENT_LEADS");
const { container } = render(<SidebarNav />);
expect(screen.getByRole("list", { name: "Events" })).toBeTruthy();
for (const label of ["Leads", "Access", "System"]) expect(screen.queryByText(label)).toBeNull();
expect(container.querySelector("[data-slot='sidebar-divider']")).toBeNull();
});
it("puts a divider between sections but not above the first", () => {
state.links = ALL;
const { container } = render(<SidebarNav />);
expect(container.querySelectorAll("[data-slot='sidebar-divider']")).toHaveLength(4);
expect(container.querySelector("nav")!.firstElementChild!.tagName).toBe("UL");
});
it("lists an unknown page after System without a label", () => {
state.links = [...ALL, link("BO_NEW_THING")];
const { container } = render(<SidebarNav />);
const lists = screen.getAllByRole("list");
expect(lists).toHaveLength(6);
const last = lists[5];
expect(linkNames(last)).toEqual(["BO_NEW_THING"]);
expect(last.getAttribute("aria-labelledby")).toBeNull();
expect(last.getAttribute("aria-label")).toBeNull();
expect(container.querySelectorAll("[data-slot='sidebar-divider']")).toHaveLength(5);
});
it("shows dividers and no labels in the collapsed rail", () => {
state.links = ALL;
const { container } = render(<SidebarNav collapsed />);
for (const label of ["Events", "Leads", "Access", "System"]) expect(screen.queryByText(label)).toBeNull();
expect(container.querySelectorAll("[data-slot='sidebar-divider']")).toHaveLength(4);
expect(screen.getByRole("list", { name: "Access" })).toBeTruthy();
expect(screen.getAllByRole("link")).toHaveLength(11);
});
it("renders Login security with a different icon than Roles", () => {
state.links = [{ ...link("BO_ROLES"), iconName: "Shield" }, { ...link("BO_LOGIN_SECURITY"), iconName: "Shield" }];
render(<SidebarNav />);
const icon = (name: string) => document.querySelector(`a[href='/${name}'] svg`)!.getAttribute("class");
expect(icon("BO_ROLES")).toContain("lucide-shield");
expect(icon("BO_LOGIN_SECURITY")).toContain("lucide-lock-keyhole");
});
});