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(), listSalespersonOptions: vi.fn(), useAdminAuth: vi.fn(), useCompanyScope: vi.fn(), })); vi.mock("@/app/lib/services/adminRoles", () => ({ listAdminRoles: mocks.listAdminRoles })); vi.mock("@/app/lib/services/adminEvents", () => ({ listSalespersonOptions: mocks.listSalespersonOptions })); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth })); vi.mock("@/app/lib/CompanyScope", () => ({ useCompanyScope: mocks.useCompanyScope })); import { UserForm } from "@/app/components/users/UserForm"; import { EventForm } from "@/app/components/events/EventForm"; import { pickDate } from "@/test/pickDate"; import { ApiError } from "@/app/lib/clientApi"; const COMPANIES = [ { id: 1, name: "Ins Co", code: "INS", status: "A" }, { id: 2, name: "CV Co", code: "CV", status: "A" }, ]; const scope = (companyId?: number) => ({ companyId, companies: COMPANIES, showCompanyColumn: false, codeOf: () => "", setCompanyId: vi.fn() }); const ROLES = [ { id: 1, name: "ROLE_SUPER_ADMIN", level: 1, parent: { id: 0 } }, { id: 2, name: "ROLE_HQ", level: 1, parent: { id: 0 } }, ]; beforeEach(() => { mocks.listAdminRoles.mockReset().mockResolvedValue(ROLES); mocks.listSalespersonOptions.mockReset().mockResolvedValue([{ id: 1, name: "Alice", username: "alice" }]); mocks.useAdminAuth.mockReset().mockReturnValue({ id: 9, isSuper: true, hasModule: () => false }); mocks.useCompanyScope.mockReset().mockReturnValue(scope(2)); }); async function fillUser(user: ReturnType) { await screen.findByLabelText("HQ"); await user.type(screen.getByLabelText("Name"), "Jane"); await user.type(screen.getByLabelText("Surname"), "Doe"); await user.type(screen.getByLabelText("Username"), "jane"); await user.type(screen.getByLabelText("Email"), "jane@x.test"); } describe("UserForm company", () => { it("shows Company to a super admin, defaulted to the picker, and sends it", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); const user = userEvent.setup(); render(); const company = await screen.findByRole("combobox", { name: "Company" }); expect(company).toHaveTextContent("CV"); await fillUser(user); await user.click(screen.getByLabelText("HQ")); await user.click(screen.getByRole("button", { name: /create|save/i })); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ companyId: 2 }))); }); it("offers Super admin (no company), which sends superAdmin and requires role 1", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); const user = userEvent.setup(); render(); await user.click(await screen.findByRole("combobox", { name: "Company" })); await user.click(await screen.findByRole("option", { name: "Super admin (no company)" })); await fillUser(user); await user.click(screen.getByLabelText("HQ")); await user.click(screen.getByRole("button", { name: /create|save/i })); expect(onSubmit).not.toHaveBeenCalled(); await user.click(screen.getByLabelText("Super admin")); await user.click(screen.getByRole("button", { name: /create|save/i })); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ superAdmin: true }))); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("companyId", expect.anything()); }); it("requires a company when none is chosen", async () => { mocks.useCompanyScope.mockReturnValue(scope()); const onSubmit = vi.fn(); const user = userEvent.setup(); render(); await fillUser(user); await user.click(screen.getByLabelText("HQ")); await user.click(screen.getByRole("button", { name: /create|save/i })); expect(onSubmit).not.toHaveBeenCalled(); expect(await screen.findByText("Choose a company.")).toBeInTheDocument(); }); it("does not show Company to staff or send it", async () => { mocks.useAdminAuth.mockReturnValue({ id: 9, isSuper: false, hasModule: () => false }); const onSubmit = vi.fn().mockResolvedValue(undefined); const user = userEvent.setup(); render(); await fillUser(user); expect(screen.queryByRole("combobox", { name: "Company" })).not.toBeInTheDocument(); await user.click(screen.getByLabelText("HQ")); await user.click(screen.getByRole("button", { name: /create|save/i })); await waitFor(() => expect(onSubmit).toHaveBeenCalled()); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("companyId"); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("superAdmin"); }); it("shows the server's COMPANY_REQUIRED message", async () => { const onSubmit = vi.fn().mockRejectedValue(new ApiError("Choose a company.", 400, { code: "COMPANY_REQUIRED" })); const user = userEvent.setup(); render(); await fillUser(user); await user.click(screen.getByLabelText("HQ")); await user.click(screen.getByRole("button", { name: /create|save/i })); expect(await screen.findByText("Choose a company.")).toBeInTheDocument(); }); it("lists only the company codes and Super admin in the Company menu, with no blank option", async () => { mocks.useCompanyScope.mockReturnValue(scope(undefined)); const user = userEvent.setup(); render(); await user.click(await screen.findByRole("combobox", { name: "Company" })); expect((await screen.findAllByRole("option")).map((o) => o.textContent)).toEqual(["INS", "CV", "Super admin (no company)"]); }); }); describe("EventForm company", () => { it("shows Company to a super admin, defaulted to the picker, and sends it on create", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); const user = userEvent.setup(); render(); expect(await screen.findByRole("combobox", { name: "Company" })).toHaveTextContent("CV"); await user.type(screen.getByLabelText("Name"), "Expo"); await user.type(screen.getByLabelText("Location"), "KL"); pickDate("Start date", "2026-12-01"); pickDate("End date", "2026-12-02"); await user.click(screen.getByRole("button", { name: /create|save/i })); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ companyId: 2 }))); }); it("does not show Company to staff", async () => { mocks.useAdminAuth.mockReturnValue({ id: 9, isSuper: false, hasModule: () => false }); render(); await waitFor(() => expect(mocks.listSalespersonOptions).toHaveBeenCalled()); expect(screen.queryByRole("combobox", { name: "Company" })).not.toBeInTheDocument(); }); it("asks for salesperson options with the company chosen, and with the event's own company on edit", async () => { const user = userEvent.setup(); const { unmount } = render(); await waitFor(() => expect(mocks.listSalespersonOptions).toHaveBeenLastCalledWith(2)); await user.click(screen.getByRole("combobox", { name: "Company" })); await user.click(await screen.findByRole("option", { name: "INS" })); await waitFor(() => expect(mocks.listSalespersonOptions).toHaveBeenLastCalledWith(1)); unmount(); mocks.useCompanyScope.mockReturnValue(scope(1)); render( , ); await waitFor(() => expect(mocks.listSalespersonOptions).toHaveBeenLastCalledWith(2)); }); }); describe("EventForm company required", () => { it("refuses a super-admin create with no company chosen", async () => { mocks.useCompanyScope.mockReturnValue(scope()); const onSubmit = vi.fn(); const user = userEvent.setup(); render(); await user.type(await screen.findByLabelText("Name"), "Expo"); await user.type(screen.getByLabelText("Location"), "KL"); pickDate("Start date", "2026-12-01"); pickDate("End date", "2026-12-02"); await user.click(screen.getByRole("button", { name: /create|save/i })); expect(onSubmit).not.toHaveBeenCalled(); expect(await screen.findByText("Choose a company.")).toBeInTheDocument(); }); it("lists only the company codes in the Company menu, with no blank option", async () => { mocks.useCompanyScope.mockReturnValue(scope(undefined)); const user = userEvent.setup(); render(); await user.click(await screen.findByRole("combobox", { name: "Company" })); expect((await screen.findAllByRole("option")).map((o) => o.textContent)).toEqual(["INS", "CV"]); }); }); describe("company picker placeholder and team hint", () => { it("shows a placeholder in the empty company picker on the user form", async () => { mocks.useCompanyScope.mockReturnValue(scope(undefined)); render(); expect(await screen.findByRole("combobox", { name: "Company" })).toHaveTextContent("Select a company"); }); it("shows a placeholder in the empty company picker on the event form", async () => { mocks.useCompanyScope.mockReturnValue(scope(undefined)); render(); expect(await screen.findByRole("combobox", { name: "Company" })).toHaveTextContent("Select a company"); }); it("hints to choose a company before the team can be listed, then lists it", async () => { mocks.useCompanyScope.mockReturnValue(scope(undefined)); const user = userEvent.setup(); render(); expect(await screen.findByText("Choose a company to list its team")).toBeInTheDocument(); expect(screen.queryByRole("checkbox", { name: /Alice/ })).not.toBeInTheDocument(); await user.click(screen.getByRole("combobox", { name: "Company" })); await user.click(await screen.findByRole("option", { name: "CV" })); expect(await screen.findByRole("checkbox", { name: /Alice/ })).toBeInTheDocument(); expect(screen.queryByText("Choose a company to list its team")).not.toBeInTheDocument(); }); });