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(),
listEvents: vi.fn(),
}));
vi.mock("@/app/lib/services/adminRoles", () => ({ listAdminRoles: mocks.listAdminRoles }));
vi.mock("@/app/lib/services/adminLocations", () => ({ listAdminLocations: mocks.listAdminLocations }));
vi.mock("@/app/lib/services/adminEvents", () => ({ listEvents: mocks.listEvents }));
import { UserForm } from "./UserForm";
const ROLES = [{ id: 1, name: "ROLE_IT", level: 1, parent: { id: 0 } }];
const LOCATIONS = [
{ id: 3, code: "MYB", name: "Kuala Lumpur", type: "B", status: "Y", parent: { id: 2 } },
];
// Runtime ids are numbers (Long) even though AdminEvent types them as string.
const EVENTS = [
{ id: 7, code: "EV-SPR", name: "Spring Fair", status: "Y" },
{ id: 8, code: "EV-AUT", name: "Autumn Expo", status: "P" },
{ id: 9, code: "EV-OLD", name: "Old Bazaar", status: "C" },
];
beforeEach(() => {
mocks.listAdminRoles.mockReset().mockResolvedValue(ROLES);
mocks.listAdminLocations.mockReset().mockResolvedValue(LOCATIONS);
mocks.listEvents.mockReset().mockResolvedValue(EVENTS);
});
const BASE_USER = {
id: 9,
name: "Jane",
username: "jane.doe",
email: "jane@example.com",
enabled: true,
status: "Y" as const,
roles: [],
branch: { id: 3 },
};
async function ready(ui: React.ReactElement) {
render(ui);
await waitFor(() => expect(screen.getByRole("combobox", { name: "Branch" })).toBeInTheDocument());
}
const eventField = () => screen.findByRole("combobox", { name: "Event" });
async function openEvent() {
await userEvent.click(await eventField());
}
async function save() {
await userEvent.click(screen.getByRole("button", { name: /save/i }));
}
describe("UserForm event field", () => {
it("shows an Event combobox with the hint", async () => {
await ready();
expect(await eventField()).toBeInTheDocument();
expect(screen.getByText("This account sees only this event.")).toBeInTheDocument();
});
it("lists No event first, then non-closed events with name and code", async () => {
await ready();
await openEvent();
expect(await screen.findByText("No event")).toBeInTheDocument();
expect(screen.getByText("Spring Fair")).toBeInTheDocument();
expect(screen.getByText(/EV-SPR/)).toBeInTheDocument();
expect(screen.getByText("Autumn Expo")).toBeInTheDocument();
expect(screen.queryByText("Old Bazaar")).not.toBeInTheDocument();
});
it("keeps the user's current event even when it is closed", async () => {
await ready();
await openEvent();
expect(await screen.findAllByText("Old Bazaar")).not.toHaveLength(0);
});
it("filters options by name or code, case-insensitively", async () => {
await ready();
await openEvent();
const search = await screen.findByPlaceholderText("Search by name or code");
await userEvent.type(search, "ev-aut");
expect(screen.getByText("Autumn Expo")).toBeInTheDocument();
expect(screen.queryByText("Spring Fair")).not.toBeInTheDocument();
await userEvent.clear(search);
await userEvent.type(search, "SPRING");
expect(screen.getByText("Spring Fair")).toBeInTheDocument();
expect(screen.queryByText("Autumn Expo")).not.toBeInTheDocument();
});
it("saves the picked event as event: { id }", async () => {
const onSubmit = vi.fn().mockResolvedValue(undefined);
await ready();
await openEvent();
await userEvent.click(await screen.findByText("Spring Fair"));
await save();
await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ event: { id: 7 } })),
);
});
it("pre-selects the stored event on edit and keeps it on save", async () => {
const onSubmit = vi.fn().mockResolvedValue(undefined);
await ready();
await waitFor(async () => expect(await eventField()).toHaveTextContent("Autumn Expo"));
await save();
await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ event: { id: 8 } })),
);
});
it("choosing No event sends event: { id: null }", async () => {
const onSubmit = vi.fn().mockResolvedValue(undefined);
await ready();
await waitFor(async () => expect(await eventField()).toHaveTextContent("Autumn Expo"));
await openEvent();
await userEvent.click(await screen.findByText("No event"));
await save();
await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ event: { id: null } })),
);
});
it("is optional: saving without an event raises no event error", async () => {
const onSubmit = vi.fn().mockResolvedValue(undefined);
await ready();
await eventField();
await save();
await waitFor(() => expect(onSubmit).toHaveBeenCalled());
expect(screen.queryByText(/event is required/i)).not.toBeInTheDocument();
});
});