Files
co-lite/solvway-admin-portal/app/lib/useAclSidebar.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

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

129 lines
4.4 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]");
});
it("collapses a lone granted inventory module to the Inventory link in the group's place", () => {
mocks.aclMenu = {
name: "root",
type: "R",
children: [
{ id: 40600000, name: "BO_NAV_INVENTORY", type: "M", children: [{ id: 40604000, name: "BO_STOCK_IN", type: "M", url: "/inventory/stock-in" }] },
{ id: 40700000, name: "BO_PROMOTIONS", type: "M", url: "/promotions" },
],
};
render(<Probe />);
expect(screen.getByTestId("names").textContent).toBe("BO_NAV_INVENTORY,BO_PROMOTIONS");
});
});