import { render, screen } from "@testing-library/react"; import { describe, it, expect, vi } from "vitest"; const mocks = vi.hoisted(() => ({ aclMenu: null as unknown })); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ aclMenu: mocks.aclMenu }), })); import { useAclSidebar } from "./useAclSidebar"; function Probe() { const links = useAclSidebar(); return
{links.map((l) => l.name).join(",")}
; } describe("useAclSidebar", () => { it("returns no entries when the session has not hydrated an aclMenu yet", () => { mocks.aclMenu = null; render(); expect(screen.getByTestId("names")).toHaveTextContent(""); }); it("derives sidebar entries from the hydrated aclMenu, excluding modules absent from it", () => { mocks.aclMenu = { name: "root", type: "R", children: [ { name: "BO_DASHBOARD", type: "M", url: "/" }, { name: "BO_REPORTS", type: "M", url: "/reports" }, { name: "Export Members", type: "A" }, ], }; render(); const names = screen.getByTestId("names").textContent; expect(names).toBe("BO_DASHBOARD,BO_REPORTS"); expect(names).not.toContain("Export Members"); }); // module-id-category-order: order and grouping come from the tree, siblings sorted by module id. it("orders siblings by module id, not by the order the aclMenu lists them", () => { mocks.aclMenu = { name: "root", type: "R", children: [ { id: 40200000, name: "BO_REPORTS", type: "M", url: "/reports" }, { id: 40100000, name: "BO_DASHBOARD", type: "M", url: "/" }, ], }; render(); expect(screen.getByTestId("names").textContent).toBe("BO_DASHBOARD,BO_REPORTS"); }); it("follows id order even where it contradicts the old fixed layout", () => { mocks.aclMenu = { name: "root", type: "R", children: [ { id: 40100000, name: "BO_REPORTS", type: "M", url: "/reports" }, { id: 40200000, name: "BO_DASHBOARD", type: "M", url: "/" }, ], }; render(); expect(screen.getByTestId("names").textContent).toBe("BO_REPORTS,BO_DASHBOARD"); }); it("renders a url-less module row as its group, with children in id order", () => { mocks.aclMenu = { name: "root", type: "R", children: [ { id: 40100000, name: "BO_DASHBOARD", type: "M", url: "/" }, { id: 40400000, name: "BO_NAV_MEMBERS", type: "M", children: [ { id: 40402000, name: "BO_MEMBERS", type: "M", url: "/members" }, { id: 40401000, name: "BO_KYC", type: "M", url: "/kyc" }, ], }, ], }; function Tree() { const links = useAclSidebar(); return
{links.map((l) => `${l.name}[${l.children.map((c) => c.name).join(",")}]`).join(";")}
; } render(); expect(screen.getByTestId("tree").textContent).toBe("BO_DASHBOARD[];BO_NAV_MEMBERS[BO_KYC,BO_MEMBERS]"); }); it("shows a newly seeded module last in its group and creates no Other group", () => { mocks.aclMenu = { name: "root", type: "R", children: [ { id: 40500000, name: "BO_NAV_MEMBERS", type: "M", children: [ { id: 40507000, name: "BO_NEW_THING", type: "M", url: "/new-thing" }, { id: 40501000, name: "BO_MEMBERS", type: "M", url: "/members" }, { id: 40502000, name: "BO_KYC", type: "M", url: "/kyc" }, ], }, ], }; function Tree() { const links = useAclSidebar(); return
{links.map((l) => `${l.name}[${l.children.map((c) => c.name).join(",")}]`).join(";")}
; } render(); expect(screen.getByTestId("tree").textContent).toBe("BO_NAV_MEMBERS[BO_MEMBERS,BO_KYC,BO_NEW_THING]"); }); });