import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ listAdminRoles: vi.fn(), listAdminLocations: vi.fn(), })); vi.mock("@/app/lib/services/adminRoles", () => ({ listAdminRoles: mocks.listAdminRoles })); vi.mock("@/app/lib/services/adminLocations", () => ({ listAdminLocations: mocks.listAdminLocations })); import { UserForm } from "./UserForm"; const ROLES = [ { id: 1, name: "ROLE_IT", level: 1, parent: { id: 0 } }, { id: 2, name: "ROLE_HQ", level: 1, parent: { id: 0 } }, ]; const LOCATIONS = [ { id: 1, code: "GLOBAL", name: "Global", type: "G", status: "Y", parent: null }, { id: 2, code: "MY", name: "Malaysia", type: "C", status: "Y", parent: { id: 1 } }, { id: 3, code: "MYB", name: "Kuala Lumpur", type: "B", status: "Y", parent: { id: 2 } }, { id: 4, code: "MYB2", name: "Penang", type: "B", status: "Y", parent: { id: 2 } }, ]; beforeEach(() => { mocks.listAdminRoles.mockReset().mockResolvedValue(ROLES); mocks.listAdminLocations.mockReset().mockResolvedValue(LOCATIONS); }); async function waitForPickers() { await waitFor(() => expect(screen.getByLabelText("IT")).toBeInTheDocument()); } describe("UserForm", () => { it("renders all fields including the pickers", async () => { render(); await waitForPickers(); expect(screen.getByLabelText("Name")).toBeInTheDocument(); expect(screen.getByLabelText("Surname")).toBeInTheDocument(); expect(screen.getByLabelText("Username")).toBeInTheDocument(); expect(screen.getByLabelText("Email")).toBeInTheDocument(); expect(screen.getByLabelText("IT")).toBeInTheDocument(); expect(screen.getByLabelText("HQ")).toBeInTheDocument(); expect(screen.queryByRole("combobox", { name: "User Group" })).not.toBeInTheDocument(); expect(screen.getByRole("combobox", { name: "Branch" })).toBeInTheDocument(); expect(screen.queryByRole("combobox", { name: "Status" })).not.toBeInTheDocument(); expect(screen.getByLabelText("Enabled")).toBeInTheDocument(); expect(screen.getByText("Disabled users cannot sign in.")).toBeInTheDocument(); }); it("derives status from the Enabled switch on save", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); render( , ); await waitForPickers(); await userEvent.click(screen.getByLabelText("Enabled")); await userEvent.click(screen.getByRole("button", { name: /save/i })); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ enabled: false, status: "N" })), ); }); it("lists only type-B nodes in the Branch select", async () => { render(); await waitForPickers(); await userEvent.click(screen.getByRole("combobox", { name: "Branch" })); expect(screen.getByRole("option", { name: /Kuala Lumpur \(MYB\)/ })).toBeInTheDocument(); expect(screen.getByRole("option", { name: /Penang/ })).toBeInTheDocument(); expect(screen.queryByRole("option", { name: /Malaysia/ })).not.toBeInTheDocument(); expect(screen.queryByRole("option", { name: /Global/ })).not.toBeInTheDocument(); }); it("blocks submit and shows errors when required fields are missing", async () => { const onSubmit = vi.fn(); render(); await waitForPickers(); await userEvent.click(screen.getByRole("button", { name: /save/i })); expect(await screen.findByText("Name is required.")).toBeInTheDocument(); expect(screen.getByText("Username is required.")).toBeInTheDocument(); expect(screen.getByText("Email is required.")).toBeInTheDocument(); expect(screen.getByText("Branch is required.")).toBeInTheDocument(); expect(onSubmit).not.toHaveBeenCalled(); }); it("blocks submit on an invalid email", async () => { const onSubmit = vi.fn(); render(); await waitForPickers(); await userEvent.type(screen.getByLabelText("Name"), "Jane"); await userEvent.type(screen.getByLabelText("Username"), "jane.doe"); await userEvent.type(screen.getByLabelText("Email"), "not-an-email"); await userEvent.click(screen.getByRole("button", { name: /save/i })); expect(await screen.findByText(/valid email/i)).toBeInTheDocument(); expect(onSubmit).not.toHaveBeenCalled(); }); it("builds the correct AdminUser payload on submit", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await waitForPickers(); await userEvent.type(screen.getByLabelText("Name"), "Jane"); await userEvent.type(screen.getByLabelText("Surname"), "Doe"); await userEvent.type(screen.getByLabelText("Username"), "jane.doe"); await userEvent.type(screen.getByLabelText("Email"), "jane@example.com"); await userEvent.click(screen.getByLabelText("IT")); await userEvent.click(screen.getByLabelText("HQ")); await userEvent.click(screen.getByRole("combobox", { name: "Branch" })); await userEvent.click(screen.getByRole("option", { name: /Kuala Lumpur/ })); await userEvent.click(screen.getByRole("button", { name: /save/i })); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith({ id: undefined, name: "Jane", surname: "Doe", username: "jane.doe", email: "jane@example.com", enabled: true, status: "Y", roles: expect.arrayContaining([ { id: 1, name: "ROLE_IT" }, { id: 2, name: "ROLE_HQ" }, ]), branch: { id: 3 }, }), ); }); it("removing a selected role chip unchecks it", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await waitForPickers(); await userEvent.click(screen.getByLabelText("IT")); expect(screen.getByRole("checkbox", { name: "IT" })).toBeChecked(); await userEvent.click(screen.getByRole("button", { name: "Remove IT" })); expect(screen.getByRole("checkbox", { name: "IT" })).not.toBeChecked(); }); it("prefills from an initial AdminUser (edit mode)", async () => { render( , ); await waitForPickers(); expect(screen.getByLabelText("Name")).toHaveValue("Jane"); expect(screen.getByLabelText("Username")).toHaveValue("jane.doe"); expect(screen.getByLabelText("Email")).toHaveValue("jane@example.com"); expect(screen.getByRole("checkbox", { name: "IT" })).toBeChecked(); expect(screen.getByLabelText("Enabled")).not.toBeChecked(); }); it("disables submit while the save is pending", async () => { let resolveSubmit!: () => void; const onSubmit = vi.fn( () => new Promise((resolve) => { resolveSubmit = resolve; }), ); render(); await waitForPickers(); await userEvent.type(screen.getByLabelText("Name"), "Jane"); await userEvent.type(screen.getByLabelText("Username"), "jane.doe"); await userEvent.type(screen.getByLabelText("Email"), "jane@example.com"); await userEvent.click(screen.getByRole("combobox", { name: "Branch" })); await userEvent.click(screen.getByRole("option", { name: /Kuala Lumpur/ })); await userEvent.click(screen.getByRole("button", { name: /save/i })); await waitFor(() => expect(screen.getByRole("button", { name: /save/i })).toBeDisabled()); resolveSubmit(); }); it("shows an error and re-enables submit when onSubmit rejects", async () => { const onSubmit = vi.fn().mockRejectedValue(new Error("Username already taken")); render(); await waitForPickers(); await userEvent.type(screen.getByLabelText("Name"), "Jane"); await userEvent.type(screen.getByLabelText("Username"), "jane.doe"); await userEvent.type(screen.getByLabelText("Email"), "jane@example.com"); await userEvent.click(screen.getByRole("combobox", { name: "Branch" })); await userEvent.click(screen.getByRole("option", { name: /Kuala Lumpur/ })); await userEvent.click(screen.getByRole("button", { name: /save/i })); expect(await screen.findByText(/username already taken/i)).toBeInTheDocument(); expect(screen.getByRole("button", { name: /save/i })).not.toBeDisabled(); }); });