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(), listSellableCountries: 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/sellableCountries", () => ({ listSellableCountries: mocks.listSellableCountries, })); import { UserForm } from "./UserForm"; const ROLES = [{ id: 1, name: "ROLE_IT", 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 } }, ]; const MY = { code: "MY", name: "Malaysia", currency: "MYR" }; const SG = { code: "SG", name: "Singapore", currency: "SGD" }; beforeEach(() => { mocks.listAdminRoles.mockReset().mockResolvedValue(ROLES); mocks.listAdminLocations.mockReset().mockResolvedValue(LOCATIONS); mocks.listSellableCountries.mockReset().mockResolvedValue([MY]); }); async function ready() { await waitFor(() => expect(screen.getByLabelText("IT")).toBeInTheDocument()); } async function fillNewUser() { 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/ })); } const save = () => userEvent.click(screen.getByRole("button", { name: /save/i })); describe("UserForm country scope", () => { it("renders no country field and omits countryScope with one sellable country", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await ready(); await waitFor(() => expect(mocks.listSellableCountries).toHaveBeenCalled()); expect(screen.queryByText("Countries")).not.toBeInTheDocument(); expect(screen.queryByLabelText("All countries")).not.toBeInTheDocument(); await fillNewUser(); await save(); await waitFor(() => expect(onSubmit).toHaveBeenCalled()); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("countryScope"); }); it("behaves as one country when listSellableCountries rejects", async () => { mocks.listSellableCountries.mockRejectedValue(new Error("boom")); const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await ready(); expect(screen.queryByLabelText("All countries")).not.toBeInTheDocument(); await fillNewUser(); await save(); await waitFor(() => expect(onSubmit).toHaveBeenCalled()); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("countryScope"); }); it("defaults a new user to All countries and submits countryScope null", async () => { mocks.listSellableCountries.mockResolvedValue([MY, SG]); const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await ready(); const all = await screen.findByLabelText("All countries"); expect(all).toBeChecked(); expect(screen.queryByLabelText(/Singapore/)).not.toBeInTheDocument(); await fillNewUser(); await save(); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ countryScope: null })), ); }); it("submits the ticked codes when All countries is unchecked", async () => { mocks.listSellableCountries.mockResolvedValue([MY, SG]); const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await ready(); await userEvent.click(await screen.findByLabelText("All countries")); await userEvent.click(await screen.findByLabelText(/Singapore/)); await fillNewUser(); await save(); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ countryScope: ["SG"] })), ); }); it("blocks submit when All is unchecked and nothing is ticked", async () => { mocks.listSellableCountries.mockResolvedValue([MY, SG]); const onSubmit = vi.fn(); render(); await ready(); await userEvent.click(await screen.findByLabelText("All countries")); await fillNewUser(); await save(); expect(await screen.findByText("Choose at least one country")).toBeInTheDocument(); expect(onSubmit).not.toHaveBeenCalled(); }); it("shows an existing scope: All unchecked, SG ticked", async () => { mocks.listSellableCountries.mockResolvedValue([MY, SG]); render( , ); await ready(); expect(await screen.findByLabelText("All countries")).not.toBeChecked(); expect(await screen.findByLabelText(/Singapore/)).toBeChecked(); expect(screen.getByLabelText(/Malaysia/)).not.toBeChecked(); }); const existing = (countryScope: string[] | null) => ({ id: 9, name: "Jane", username: "jane.doe", email: "jane@example.com", enabled: true, status: "P" as const, roles: [], branch: { id: 3 }, countryScope, }); it("keeps a stored non-sellable country (TH) visible, ticked and removable", async () => { mocks.listSellableCountries.mockResolvedValue([MY, SG]); const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await ready(); const th = await screen.findByRole("checkbox", { name: /TH/ }); expect(th).toBeChecked(); expect(screen.getByLabelText(/Singapore/)).toBeChecked(); await userEvent.click(th); await save(); await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ countryScope: ["SG"] })), ); }); it("renders no country field and keeps the stored scope with one sellable country", async () => { const onSubmit = vi.fn().mockResolvedValue(undefined); render(); await ready(); await waitFor(() => expect(mocks.listSellableCountries).toHaveBeenCalled()); expect(screen.queryByLabelText("All countries")).not.toBeInTheDocument(); expect(screen.queryByRole("checkbox", { name: /Singapore|SG/ })).not.toBeInTheDocument(); await save(); await waitFor(() => expect(onSubmit).toHaveBeenCalled()); expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("countryScope"); }); it.each([[null], [[]]])("shows All countries checked for stored scope %j", async (scope) => { mocks.listSellableCountries.mockResolvedValue([MY, SG]); render(); await ready(); expect(await screen.findByLabelText("All countries")).toBeChecked(); }); it("names each country checkbox from its visible label, not aria-label", async () => { mocks.listSellableCountries.mockResolvedValue([MY, SG]); render(); await ready(); const sg = await screen.findByRole("checkbox", { name: /Singapore/ }); expect(sg).not.toHaveAttribute("aria-label"); expect(screen.getByRole("checkbox", { name: /Malaysia/ })).not.toHaveAttribute("aria-label"); }); });