import { describe, it, expect, vi, beforeEach } from "vitest"; import { render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import type { ModuleNode } from "@/app/lib/types"; import { ICON_NAMES } from "@/app/components/app/icon-map"; const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false })); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }), })); const svc = vi.hoisted(() => ({ listModules: vi.fn(), createModule: vi.fn(), updateModule: vi.fn(), deleteModule: vi.fn(), })); vi.mock("@/app/lib/services/adminModules", () => ({ listModules: () => svc.listModules(), createModule: (p: unknown) => svc.createModule(p), updateModule: (id: number, p: unknown) => svc.updateModule(id, p), deleteModule: (id: number) => svc.deleteModule(id), })); import ModulesPage from "./page"; // A small Area-BO tree: root -> Settings group -> Locales/Modules, plus Wallet // (protected) and a disposable group under Settings. const tree: ModuleNode[] = [ { id: 40000000, name: "BO_ROOT", type: "R", url: undefined, parentId: null, grantCount: 0 }, { id: 40460000, name: "BO_SYSTEM", type: "M", url: "", parentId: 40000000, grantCount: 0 }, { id: 41400000, name: "BO_WALLET", type: "M", url: "/wallet", parentId: 40000000, grantCount: 2 }, { id: 41600000, name: "BO_LOCALES", type: "M", url: "/locales", parentId: 40460000, grantCount: 1 }, { id: 41700000, name: "BO_MODULES", type: "M", url: "/modules", parentId: 40460000, grantCount: 0 }, { id: 41800000, name: "BO_THROWAWAY", type: "M", url: "", parentId: 40460000, grantCount: 0 }, ]; function renderPage() { return render(); } beforeEach(() => { auth.hasModule.mockReset(); auth.loading = false; svc.listModules.mockReset(); svc.createModule.mockReset(); svc.updateModule.mockReset(); svc.deleteModule.mockReset(); svc.listModules.mockResolvedValue(tree); }); describe("page states", () => { it("shows the page header and a loading skeleton while the tree loads", () => { auth.hasModule.mockReturnValue(true); svc.listModules.mockReturnValue(new Promise(() => {})); render(); expect(screen.getByRole("heading", { level: 1, name: "Modules" })).toBeInTheDocument(); expect(screen.getByRole("status")).toBeInTheDocument(); }); }); describe("module gating (BO_MODULES, id 41700000)", () => { it("renders a not-authorised state and no tree for a user lacking the BO_MODULES module", async () => { auth.hasModule.mockReturnValue(false); renderPage(); expect(await screen.findByTestId("not-authorized")).toBeInTheDocument(); expect(screen.queryByText("BO_MODULES")).not.toBeInTheDocument(); expect(svc.listModules).not.toHaveBeenCalled(); }); it("renders the management page for a user granted the BO_MODULES module", async () => { auth.hasModule.mockReturnValue(true); renderPage(); expect(await screen.findByText("BO_MODULES")).toBeInTheDocument(); expect(screen.queryByTestId("not-authorized")).not.toBeInTheDocument(); }); }); describe("the tree view", () => { it("shows the code line only when the label differs from the module name", async () => { auth.hasModule.mockReturnValue(true); renderPage(); // BO_THROWAWAY has no translation, so label === name and the code must not repeat. expect(await screen.findAllByText("BO_THROWAWAY")).toHaveLength(1); expect(screen.getAllByText("BO_MODULES")).toHaveLength(1); // the code line }); beforeEach(() => { auth.hasModule.mockReturnValue(true); }); it("renders nested rows with label (t(name)), raw name, type, url and grant count", async () => { renderPage(); await screen.findByText("BO_MODULES"); // Labels resolve via the DEFAULT_MESSAGES floor (t(name)), raw names in the name column. expect(screen.getByText("babyboo admin")).toBeInTheDocument(); // BO_ROOT label expect(screen.getByText("Settings")).toBeInTheDocument(); // BO_SYSTEM label expect(screen.getByText("Wallet")).toBeInTheDocument(); // BO_WALLET label expect(screen.getByText("/wallet")).toBeInTheDocument(); // "Modules" appears twice: the page h1 and the BO_MODULES row label (t(name) resolves via // the DEFAULT_MESSAGES floor). expect(screen.getAllByText("Modules")).toHaveLength(2); expect(screen.getAllByText("Module")).toHaveLength(5); // 5x type M expect(screen.getByText("Root")).toBeInTheDocument(); // type R // grant counts: BO_WALLET=2, BO_LOCALES=1, others 0 expect(screen.getAllByText("0")).toHaveLength(4); expect(screen.getByText("1")).toBeInTheDocument(); expect(screen.getByText("2")).toBeInTheDocument(); }); it("marks protected nodes with a Protected badge", async () => { renderPage(); await screen.findByText("BO_MODULES"); // BO_ROOT, BO_SYSTEM, BO_WALLET (fixed five) + BO_LOCALES, BO_MODULES (route-gate names). expect(screen.getAllByText("Protected")).toHaveLength(5); }); it("collapses and expands a group", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); const collapse = screen.getByRole("button", { name: "Collapse Settings" }); await user.click(collapse); expect(screen.queryByText("BO_LOCALES")).not.toBeInTheDocument(); await user.click(screen.getByRole("button", { name: "Expand Settings" })); expect(await screen.findByText("BO_LOCALES")).toBeInTheDocument(); }); it("shows the error banner and NO empty-tree state when the load fails", async () => { // A failed listModules leaves nodes == [], so without the gate the empty branch would // render "No modules yet" alongside the error — misleading (the tree may be fine). svc.listModules.mockRejectedValue(new Error("server down")); renderPage(); expect(await screen.findByText("server down")).toBeInTheDocument(); expect(screen.queryByText("No modules yet — the tree is empty.")).not.toBeInTheDocument(); }); // Production shape: the module API (services-system findAllByArea) never returns the // BO_ROOT row, so every top-level node is parented to an id (40000000) that is ABSENT // from the returned list — there is no null-parent node at all. Such nodes must still // render as roots (mirrors the sidebar, which shows the same top-level modules with no // visible root). Regressed live: buildTree only rooted parentId==null, so the whole // tree collapsed to "No modules yet" against real data. it("renders top-level nodes whose (hidden) BO_ROOT parent is absent from the response", async () => { svc.listModules.mockResolvedValue([ { id: 40460000, name: "BO_SYSTEM", type: "M", url: "", parentId: 40000000, grantCount: 0 }, { id: 41700000, name: "BO_MODULES", type: "M", url: "/modules", parentId: 40460000, grantCount: 0 }, ]); renderPage(); expect(await screen.findByText("BO_SYSTEM")).toBeInTheDocument(); expect(await screen.findByText("BO_MODULES")).toBeInTheDocument(); expect(screen.queryByText("No modules yet — the tree is empty.")).not.toBeInTheDocument(); }); }); describe("the create flow", () => { beforeEach(() => { auth.hasModule.mockReturnValue(true); }); it("creates a node at Top level by default and refetches", async () => { const user = userEvent.setup(); svc.createModule.mockResolvedValue({ id: 41900000, name: "BO_REPORTS_V2", type: "M", url: "", parentId: 40000000, grantCount: 0 }); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add module" })); await user.type(screen.getByLabelText("Name"), "BO_REPORTS_V2"); await user.click(screen.getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalledWith( expect.objectContaining({ name: "BO_REPORTS_V2", type: "M", parentId: null }), ), ); await waitFor(() => expect(svc.listModules).toHaveBeenCalledTimes(2)); await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); }); it("discloses the code follow-up for a brand-new name (label + icon are code changes)", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add module" })); const dialog = await screen.findByRole("dialog"); // The hint text names both code touch-points (the matcher hits the

and the ). expect(within(dialog).getAllByText(/defaultMessages\.ts/).length).toBeGreaterThan(0); expect(within(dialog).getAllByText(/icon-map\.ts/).length).toBeGreaterThan(0); }); it("offers exactly the icon-map.ts allowlist in the icon dropdown", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add module" })); await user.click(screen.getByTestId("module-icon-select")); const options = await screen.findAllByRole("option"); const labels = options.map((o) => o.textContent); expect(labels).toEqual(expect.arrayContaining([...ICON_NAMES])); expect(labels).toHaveLength(ICON_NAMES.length); // exactly the allowlist — no dead icons }); it("shows an inline duplicate-name error and keeps the dialog open", async () => { const user = userEvent.setup(); svc.createModule.mockRejectedValue(new Error("Module name already exists: BO_MODULES")); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add module" })); await user.type(screen.getByLabelText("Name"), "BO_MODULES"); await user.click(screen.getByRole("button", { name: "Save" })); expect(await screen.findByText("Module name already exists: BO_MODULES")).toBeInTheDocument(); expect(screen.getByRole("dialog")).toBeInTheDocument(); }); // Root (type R) is the single seeded tree root; the server refuses to create one // (only M/A allowed), so offering it in the dialog is a guaranteed-fail round-trip. it("does not offer Root as a selectable type when adding", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add module" })); await user.click(screen.getByTestId("module-type-select")); const labels = (await screen.findAllByRole("option")).map((o) => o.textContent); expect(labels).toEqual(["Module", "Action"]); }); }); describe("the create dialog parent choice (module-id-category-allocation)", () => { const HINT = /stored under the root/; const withAction: ModuleNode[] = [ ...tree, { id: 41700001, name: "BO_MODULES_EXPORT", type: "A", url: "", parentId: 40000000, grantCount: 0 }, ]; beforeEach(() => { auth.hasModule.mockReturnValue(true); }); async function openCreate() { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add module" })); const dialog = await screen.findByRole("dialog"); return { user, dialog }; } it("offers 'Top level' first, and saving with it sends parentId null", async () => { svc.createModule.mockResolvedValue({ id: 41900000, name: "BO_NEW_TOP", type: "M", url: "", parentId: null, grantCount: 0 }); const { user, dialog } = await openCreate(); await user.type(within(dialog).getByLabelText("Name"), "BO_NEW_TOP"); await user.click(within(dialog).getByTestId("module-parent-select")); const options = await screen.findAllByRole("option"); expect(options[0]).toHaveTextContent("Top level"); await user.click(screen.getByRole("option", { name: "Top level" })); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalledWith( expect.objectContaining({ name: "BO_NEW_TOP", type: "M", parentId: null }), ), ); }); it("labels the parent 'Belongs to', hides 'Top level' and actions, and shows the hint for type Action", async () => { svc.listModules.mockResolvedValue(withAction); const { user, dialog } = await openCreate(); expect(within(dialog).queryByText(HINT)).not.toBeInTheDocument(); await user.click(within(dialog).getByTestId("module-type-select")); await user.click(await screen.findByRole("option", { name: "Action" })); expect(within(dialog).getByText("Belongs to")).toBeInTheDocument(); expect(within(dialog).getByText(HINT)).toBeInTheDocument(); await user.click(within(dialog).getByTestId("module-parent-select")); const labels = (await screen.findAllByRole("option")).map((o) => o.textContent ?? ""); expect(labels.some((l) => l.includes("Top level"))).toBe(false); expect(labels.some((l) => l.includes("BO_MODULES_EXPORT"))).toBe(false); expect(labels.some((l) => l.includes("BO_WALLET"))).toBe(true); }); async function chooseType(user: ReturnType, dialog: HTMLElement, name: string) { await user.click(within(dialog).getByTestId("module-type-select")); await user.click(await screen.findByRole("option", { name })); } async function chooseParent(user: ReturnType, dialog: HTMLElement, name: RegExp | string) { await user.click(within(dialog).getByTestId("module-parent-select")); await user.click(await screen.findByRole("option", { name })); } it("never offers a root node as a parent, and defaults to Top level", async () => { const { user, dialog } = await openCreate(); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent("Top level"); await user.click(within(dialog).getByTestId("module-parent-select")); const labels = (await screen.findAllByRole("option")).map((o) => o.textContent ?? ""); expect(labels.some((l) => l.includes("BO_ROOT") || l.includes("babyboo admin"))).toBe(false); expect(labels.some((l) => l.includes("BO_WALLET"))).toBe(true); }); it("blocks saving an Action created at Top level and asks for a module", async () => { const { user, dialog } = await openCreate(); await user.type(within(dialog).getByLabelText("Name"), "BO_NEW_ACT"); await chooseType(user, dialog, "Action"); await user.click(within(dialog).getByRole("button", { name: "Save" })); expect(await within(dialog).findByText("Choose the module this action belongs to.")).toBeInTheDocument(); expect(svc.createModule).not.toHaveBeenCalled(); }); it("keeps a chosen module when switching to Action and sends it as the parent", async () => { svc.createModule.mockResolvedValue({ id: 41400001, name: "BO_NEW_ACT", type: "A", url: "", parentId: 41400000, grantCount: 0 }); const { user, dialog } = await openCreate(); await user.type(within(dialog).getByLabelText("Name"), "BO_NEW_ACT"); await chooseParent(user, dialog, /BO_WALLET/); await chooseType(user, dialog, "Action"); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent(/Wallet|BO_WALLET/); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalledWith( expect.objectContaining({ name: "BO_NEW_ACT", type: "A", parentId: 41400000 }), ), ); }); it("still sends the chosen module id after switching Action back to Module", async () => { svc.createModule.mockResolvedValue({ id: 41400000, name: "BO_NEW_MOD", type: "M", url: "", parentId: 41400000, grantCount: 0 }); const { user, dialog } = await openCreate(); await user.type(within(dialog).getByLabelText("Name"), "BO_NEW_MOD"); await chooseParent(user, dialog, /BO_WALLET/); await chooseType(user, dialog, "Action"); await chooseType(user, dialog, "Module"); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalledWith( expect.objectContaining({ name: "BO_NEW_MOD", type: "M", parentId: 41400000 }), ), ); }); it("leaves the edit dialog without 'Top level' or the hint", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Edit BO_THROWAWAY" })); const dialog = await screen.findByRole("dialog"); expect(within(dialog).queryByText(HINT)).not.toBeInTheDocument(); await user.click(within(dialog).getByTestId("module-parent-select")); const labels = (await screen.findAllByRole("option")).map((o) => o.textContent ?? ""); expect(labels.some((l) => l.includes("Top level"))).toBe(false); }); }); describe("the per-row add-child button (only modules can be a create parent)", () => { beforeEach(() => { auth.hasModule.mockReturnValue(true); }); it("is not shown on an action row", async () => { svc.listModules.mockResolvedValue([ ...tree, { id: 41400001, name: "BO_WALLET_EXPORT", type: "A", url: "", parentId: 41400000, grantCount: 0 }, ]); renderPage(); await screen.findByText("BO_WALLET_EXPORT"); expect(screen.queryByRole("button", { name: /Add child under .*BO_WALLET_EXPORT/ })).not.toBeInTheDocument(); // sibling module rows keep it expect(screen.getByRole("button", { name: "Add child under Wallet" })).toBeInTheDocument(); }); it("is not shown on a root (type R) row", async () => { renderPage(); await screen.findByText("BO_MODULES"); expect(screen.getByText("babyboo admin")).toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Add child under babyboo admin" })).not.toBeInTheDocument(); }); it("stays on a module row, pre-selecting it as parent and sending its id", async () => { const user = userEvent.setup(); svc.createModule.mockResolvedValue({ id: 41400001, name: "BO_WALLET_SUB", type: "M", url: "", parentId: 41400000, grantCount: 0 }); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add child under Wallet" })); const dialog = await screen.findByRole("dialog"); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent(/Wallet|BO_WALLET/); await user.type(within(dialog).getByLabelText("Name"), "BO_WALLET_SUB"); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalledWith( expect.objectContaining({ name: "BO_WALLET_SUB", type: "M", parentId: 41400000 }), ), ); }); }); describe("the edit flow", () => { beforeEach(() => { auth.hasModule.mockReturnValue(true); }); it("locks the name field on edit and states the rename-is-create-new caveat", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Edit Modules" })); const dialog = await screen.findByRole("dialog"); expect(within(dialog).getByLabelText("Name")).toHaveAttribute("readonly"); expect(within(dialog).getByText(/cannot be renamed/)).toBeInTheDocument(); expect(within(dialog).getByText(/rename means creating a new module/)).toBeInTheDocument(); }); it("edits a node's mutable fields and sends the locked name unchanged", async () => { const user = userEvent.setup(); svc.updateModule.mockResolvedValue({ ...tree[5], desc: "New desc" }); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Edit BO_THROWAWAY" })); const dialog = await screen.findByRole("dialog"); const desc = within(dialog).getByLabelText("Description"); await user.clear(desc); await user.type(desc, "New desc"); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.updateModule).toHaveBeenCalledWith( 41800000, expect.objectContaining({ name: "BO_THROWAWAY", desc: "New desc" }), ), ); }); it("disables structural fields for a protected node, keeping desc/icon editable", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Edit Wallet" })); const dialog = await screen.findByRole("dialog"); expect(within(dialog).getByText(/protected/)).toBeInTheDocument(); expect(within(dialog).getByTestId("module-type-select")).toBeDisabled(); expect(within(dialog).getByLabelText("URL")).toBeDisabled(); expect(within(dialog).getByTestId("module-parent-select")).toBeDisabled(); expect(within(dialog).getByLabelText("Description")).toBeEnabled(); expect(within(dialog).getByTestId("module-icon-select")).toBeEnabled(); }); it("reparents a node via the edit dialog", async () => { const user = userEvent.setup(); svc.updateModule.mockResolvedValue({ ...tree[5], parentId: 41400000 }); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Edit BO_THROWAWAY" })); const dialog = await screen.findByRole("dialog"); await user.click(within(dialog).getByTestId("module-parent-select")); await user.click(await screen.findByRole("option", { name: /BO_WALLET/ })); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.updateModule).toHaveBeenCalledWith( 41800000, expect.objectContaining({ parentId: 41400000 }), ), ); }); it("warns when an edit flips a non-protected node between Module and Action", async () => { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Edit BO_THROWAWAY" })); const dialog = await screen.findByRole("dialog"); // BO_THROWAWAY is a Module and not protected — no warning until the type actually changes. expect(within(dialog).queryByText(/removes this node from the sidebar/i)).not.toBeInTheDocument(); await user.click(within(dialog).getByTestId("module-type-select")); await user.click(await screen.findByRole("option", { name: "Action" })); expect(await within(dialog).findByText(/removes this node from the sidebar/i)).toBeInTheDocument(); }); }); describe("the delete flow", () => { beforeEach(() => { auth.hasModule.mockReturnValue(true); }); it("confirms and states grants are removed, then deletes and refetches", async () => { const user = userEvent.setup(); svc.deleteModule.mockResolvedValue(undefined); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Delete Wallet" })); const dialog = await screen.findByRole("dialog"); expect(within(dialog).getByText(/2 grants/)).toBeInTheDocument(); await user.click(within(dialog).getByRole("button", { name: "Delete" })); await waitFor(() => expect(svc.deleteModule).toHaveBeenCalledWith(41400000)); await waitFor(() => expect(svc.listModules).toHaveBeenCalledTimes(2)); await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); }); it("surfaces the has-children 422 inline and keeps the dialog open", async () => { const user = userEvent.setup(); svc.deleteModule.mockRejectedValue( new Error("Module 41800000 has children; remove or reparent them first."), ); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Delete BO_THROWAWAY" })); const dialog = await screen.findByRole("dialog"); await user.click(within(dialog).getByRole("button", { name: "Delete" })); expect(await within(dialog).findByText(/41800000 has children/)).toBeInTheDocument(); expect(screen.getByRole("dialog")).toBeInTheDocument(); }); it("surfaces the protected-name refusal inline", async () => { const user = userEvent.setup(); svc.deleteModule.mockRejectedValue(new Error("Module BO_WALLET is protected and cannot be deleted.")); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Delete Wallet" })); const dialog = await screen.findByRole("dialog"); await user.click(within(dialog).getByRole("button", { name: "Delete" })); expect(await within(dialog).findByText(/protected and cannot be deleted/)).toBeInTheDocument(); }); }); // unify-admin-listing-toolbar: "Name or URL" search in the filter bar; matches stay in their tree. describe("module search", () => { it("shows only matching modules and their ancestors", async () => { auth.hasModule.mockReturnValue(true); const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); const bar = document.querySelector('[data-slot="filter-panel"]') as HTMLElement; expect(within(bar).getByRole("group", { name: "Name or URL" })).toBeInTheDocument(); await user.type(screen.getByRole("searchbox", { name: "Name or URL" }), "/locales"); await waitFor(() => expect(screen.queryByText("BO_MODULES")).not.toBeInTheDocument()); expect(screen.getByText("Locales")).toBeInTheDocument(); expect(screen.getByText("Settings")).toBeInTheDocument(); expect(screen.queryByText("Wallet")).not.toBeInTheDocument(); }); it("says when nothing matches", async () => { auth.hasModule.mockReturnValue(true); const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.type(screen.getByRole("searchbox", { name: "Name or URL" }), "zzz"); expect(await screen.findByText("No modules match your search")).toBeInTheDocument(); }); }); describe("category/slot parent rules (module-create-root-slot-fixes)", () => { const withSlot: ModuleNode[] = [ ...tree, { id: 40500000, name: "BO_NAV_MEMBERS", type: "M", url: "", parentId: 40000000, grantCount: 0 }, { id: 40501000, name: "BO_MEMBERS", type: "M", url: "/members", parentId: 40500000, grantCount: 0 }, ]; beforeEach(() => { auth.hasModule.mockReturnValue(true); svc.listModules.mockResolvedValue(withSlot); }); async function openCreate() { const user = userEvent.setup(); renderPage(); await screen.findByText("BO_MODULES"); await user.click(screen.getByRole("button", { name: "Add module" })); return { user, dialog: await screen.findByRole("dialog") }; } async function parentLabels(user: ReturnType, dialog: HTMLElement) { await user.click(within(dialog).getByTestId("module-parent-select")); return (await screen.findAllByRole("option")).map((o) => o.textContent ?? ""); } // Both rows label as "Members" (BO_NAV_MEMBERS and BO_MEMBERS), so pick by tree order: // the category (40500000) precedes its child slot (40501000). async function addChild(user: ReturnType, which: "category" | "slot") { await screen.findByText("BO_MEMBERS"); const btns = screen.getAllByRole("button", { name: "Add child under Members" }); expect(btns).toHaveLength(2); await user.click(btns[which === "category" ? 0 : 1]); return await screen.findByRole("dialog"); } it("does not offer a slot as parent for a Module, but offers Top level and the category", async () => { const { user, dialog } = await openCreate(); const labels = await parentLabels(user, dialog); expect(labels.some((l) => l.includes("Top level"))).toBe(true); expect(labels.some((l) => l.includes("BO_NAV_MEMBERS"))).toBe(true); expect(labels.some((l) => l.includes("(BO_MEMBERS)"))).toBe(false); }); it("add-child on a slot row opens an Action dialog belonging to that slot and saves it", async () => { const user = userEvent.setup(); svc.createModule.mockResolvedValue({ id: 40501001, name: "BO_MEMBERS_EXPORT", type: "A", url: "", parentId: 40501000, grantCount: 0 }); renderPage(); const dialog = await addChild(user, "slot"); expect(within(dialog).getByTestId("module-type-select")).toHaveTextContent("Action"); expect(within(dialog).getByText("Belongs to")).toBeInTheDocument(); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent("BO_MEMBERS"); await user.type(within(dialog).getByLabelText("Name"), "BO_MEMBERS_EXPORT"); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalledWith( expect.objectContaining({ name: "BO_MEMBERS_EXPORT", type: "A", parentId: 40501000 }), ), ); }); it("add-child on a category row still opens a Module dialog under that category", async () => { const user = userEvent.setup(); renderPage(); const dialog = await addChild(user, "category"); expect(within(dialog).getByTestId("module-type-select")).toHaveTextContent("Module"); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent("BO_NAV_MEMBERS"); }); it("still offers the slot under 'Belongs to' for type Action", async () => { const { user, dialog } = await openCreate(); await user.click(within(dialog).getByTestId("module-type-select")); await user.click(await screen.findByRole("option", { name: "Action" })); const labels = await parentLabels(user, dialog); expect(labels.some((l) => l.includes("(BO_MEMBERS)"))).toBe(true); }); async function pickType(user: ReturnType, dialog: HTMLElement, name: "Module" | "Action") { await user.click(within(dialog).getByTestId("module-type-select")); await user.click(await screen.findByRole("option", { name })); } it("resets a slot parent to Top level when the type switches from Action to Module", async () => { const user = userEvent.setup(); svc.createModule.mockResolvedValue({ id: 1, name: "BO_X", type: "M", url: "", parentId: null, grantCount: 0 }); renderPage(); const dialog = await addChild(user, "slot"); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent("BO_MEMBERS"); await pickType(user, dialog, "Module"); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent("Top level"); await user.type(within(dialog).getByLabelText("Name"), "BO_X"); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalled()); expect(svc.createModule).toHaveBeenCalledWith(expect.objectContaining({ type: "M", parentId: null })); }); it("keeps a category parent when the type switches from Action to Module", async () => { const user = userEvent.setup(); svc.createModule.mockResolvedValue({ id: 1, name: "BO_X", type: "M", url: "", parentId: 40500000, grantCount: 0 }); renderPage(); const dialog = await addChild(user, "category"); await pickType(user, dialog, "Action"); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent("BO_NAV_MEMBERS"); await pickType(user, dialog, "Module"); expect(within(dialog).getByTestId("module-parent-select")).toHaveTextContent("BO_NAV_MEMBERS"); await user.type(within(dialog).getByLabelText("Name"), "BO_X"); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createModule).toHaveBeenCalled()); expect(svc.createModule).toHaveBeenCalledWith(expect.objectContaining({ type: "M", parentId: 40500000 })); }); });