Files
co-lite/solvway-admin-portal/app/lib/useAclSidebar.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

116 lines
3.9 KiB
TypeScript

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 <div data-testid="names">{links.map((l) => l.name).join(",")}</div>;
}
describe("useAclSidebar", () => {
it("returns no entries when the session has not hydrated an aclMenu yet", () => {
mocks.aclMenu = null;
render(<Probe />);
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(<Probe />);
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(<Probe />);
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(<Probe />);
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 <div data-testid="tree">{links.map((l) => `${l.name}[${l.children.map((c) => c.name).join(",")}]`).join(";")}</div>;
}
render(<Tree />);
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 <div data-testid="tree">{links.map((l) => `${l.name}[${l.children.map((c) => c.name).join(",")}]`).join(";")}</div>;
}
render(<Tree />);
expect(screen.getByTestId("tree").textContent).toBe("BO_NAV_MEMBERS[BO_MEMBERS,BO_KYC,BO_NEW_THING]");
});
});