Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
207 lines
7.8 KiB
TypeScript
207 lines
7.8 KiB
TypeScript
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(<UserForm onSubmit={onSubmit} />);
|
|
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(<UserForm onSubmit={onSubmit} />);
|
|
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(<UserForm onSubmit={onSubmit} />);
|
|
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(<UserForm onSubmit={onSubmit} />);
|
|
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(<UserForm onSubmit={onSubmit} />);
|
|
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(
|
|
<UserForm
|
|
initial={{
|
|
id: 9,
|
|
name: "Jane",
|
|
username: "jane.doe",
|
|
email: "jane@example.com",
|
|
enabled: true,
|
|
status: "P",
|
|
roles: [],
|
|
branch: { id: 3 },
|
|
countryScope: ["SG"],
|
|
}}
|
|
onSubmit={vi.fn()}
|
|
/>,
|
|
);
|
|
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(<UserForm initial={existing(["SG", "TH"])} onSubmit={onSubmit} />);
|
|
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(<UserForm initial={existing(["SG"])} onSubmit={onSubmit} />);
|
|
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(<UserForm initial={existing(scope as string[] | null)} onSubmit={vi.fn()} />);
|
|
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(<UserForm initial={existing(["SG"])} onSubmit={vi.fn()} />);
|
|
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");
|
|
});
|
|
});
|