import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, it, expect, vi, beforeEach } from "vitest"; import { ApiError } from "@/app/lib/clientApi"; const mocks = vi.hoisted(() => ({ listSalespersonOptions: vi.fn(), useAdminAuth: vi.fn() })); vi.mock("@/app/lib/services/adminEvents", () => ({ listSalespersonOptions: mocks.listSalespersonOptions })); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth })); import { EventForm } from "./EventForm"; const INITIAL = { uuid: "e1", name: "Expo", location: "KL", startDate: "2026-12-01", endDate: "2026-12-02", enabled: true, team: [], capacity: 200, invitationCap: 50, }; const asAdmin = (guests: boolean) => mocks.useAdminAuth.mockReturnValue({ isSuper: false, hasModule: (m: string) => guests && m === "BO_EVENT_GUESTS" }); beforeEach(() => { mocks.listSalespersonOptions.mockReset().mockResolvedValue([]); asAdmin(true); }); describe("EventForm capacity and invitation cap", () => { it("prefills both fields on edit", async () => { render(); expect(await screen.findByLabelText("Capacity")).toHaveValue(200); expect(screen.getByLabelText("Invitation cap")).toHaveValue(50); }); it("disables both fields and leaves them out of the payload without BO_EVENT_GUESTS", async () => { asAdmin(false); const onSubmit = vi.fn().mockResolvedValue(undefined); render(); expect(await screen.findByLabelText("Capacity")).toBeDisabled(); expect(screen.getByLabelText("Invitation cap")).toBeDisabled(); await userEvent.click(screen.getByRole("button", { name: "Edit" })); await waitFor(() => expect(onSubmit).toHaveBeenCalled()); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("capacity"); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("invitationCap"); }); it("sends the numbers, and null when a field is cleared", async () => { const user = userEvent.setup(); const onSubmit = vi.fn().mockResolvedValue(undefined); render(); const cap = await screen.findByLabelText("Capacity"); await user.clear(cap); await user.type(cap, "300"); await user.clear(screen.getByLabelText("Invitation cap")); await user.click(screen.getByRole("button", { name: "Edit" })); await waitFor(() => expect(onSubmit).toHaveBeenCalled()); expect(onSubmit.mock.calls[0][0]).toMatchObject({ capacity: 300, invitationCap: null }); }); it("shows CAP_BELOW_GUESTS on the invitation cap field", async () => { const user = userEvent.setup(); const onSubmit = vi.fn().mockRejectedValue(new ApiError("Cap is below the guests already invited", 409, { code: "CAP_BELOW_GUESTS" })); render(); await screen.findByLabelText("Capacity"); await user.click(screen.getByRole("button", { name: "Edit" })); expect(await screen.findByText("Cap is below the guests already invited")).toBeInTheDocument(); expect(screen.getByLabelText("Invitation cap")).toHaveAttribute("aria-invalid", "true"); expect(screen.getByLabelText("Capacity")).not.toHaveAttribute("aria-invalid", "true"); }); it("shows INVALID_LIMIT on the field the server names", async () => { const user = userEvent.setup(); const onSubmit = vi.fn().mockRejectedValue(new ApiError("Capacity must be at least 1", 400, { code: "INVALID_LIMIT", details: { field: "capacity" } })); render(); await screen.findByLabelText("Capacity"); await user.click(screen.getByRole("button", { name: "Edit" })); expect(await screen.findByText("Capacity must be at least 1")).toBeInTheDocument(); expect(screen.getByLabelText("Capacity")).toHaveAttribute("aria-invalid", "true"); }); });