Files
co-lite/solvway-admin-portal/app/components/users/UserForm.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

229 lines
8.9 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(),
}));
vi.mock("@/app/lib/services/adminRoles", () => ({ listAdminRoles: mocks.listAdminRoles }));
vi.mock("@/app/lib/services/adminLocations", () => ({ listAdminLocations: mocks.listAdminLocations }));
import { UserForm } from "./UserForm";
const ROLES = [
{ id: 1, name: "ROLE_IT", level: 1, parent: { id: 0 } },
{ id: 2, name: "ROLE_HQ", 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 } },
{ id: 4, code: "MYB2", name: "Penang", type: "B", status: "Y", parent: { id: 2 } },
];
beforeEach(() => {
mocks.listAdminRoles.mockReset().mockResolvedValue(ROLES);
mocks.listAdminLocations.mockReset().mockResolvedValue(LOCATIONS);
});
async function waitForPickers() {
await waitFor(() => expect(screen.getByLabelText("IT")).toBeInTheDocument());
}
describe("UserForm", () => {
it("renders all fields including the pickers", async () => {
render(<UserForm onSubmit={vi.fn()} />);
await waitForPickers();
expect(screen.getByLabelText("Name")).toBeInTheDocument();
expect(screen.getByLabelText("Surname")).toBeInTheDocument();
expect(screen.getByLabelText("Username")).toBeInTheDocument();
expect(screen.getByLabelText("Email")).toBeInTheDocument();
expect(screen.getByLabelText("IT")).toBeInTheDocument();
expect(screen.getByLabelText("HQ")).toBeInTheDocument();
expect(screen.queryByRole("combobox", { name: "User Group" })).not.toBeInTheDocument();
expect(screen.getByRole("combobox", { name: "Branch" })).toBeInTheDocument();
expect(screen.queryByRole("combobox", { name: "Status" })).not.toBeInTheDocument();
expect(screen.getByLabelText("Enabled")).toBeInTheDocument();
expect(screen.getByText("Disabled users cannot sign in.")).toBeInTheDocument();
});
it("derives status from the Enabled switch on save", async () => {
const onSubmit = vi.fn().mockResolvedValue(undefined);
render(
<UserForm
initial={{
id: 9,
name: "Jane",
username: "jane.doe",
email: "jane@example.com",
enabled: true,
status: "P",
roles: [],
branch: { id: 3 },
}}
onSubmit={onSubmit}
/>,
);
await waitForPickers();
await userEvent.click(screen.getByLabelText("Enabled"));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ enabled: false, status: "N" })),
);
});
it("lists only type-B nodes in the Branch select", async () => {
render(<UserForm onSubmit={vi.fn()} />);
await waitForPickers();
await userEvent.click(screen.getByRole("combobox", { name: "Branch" }));
expect(screen.getByRole("option", { name: /Kuala Lumpur \(MYB\)/ })).toBeInTheDocument();
expect(screen.getByRole("option", { name: /Penang/ })).toBeInTheDocument();
expect(screen.queryByRole("option", { name: /Malaysia/ })).not.toBeInTheDocument();
expect(screen.queryByRole("option", { name: /Global/ })).not.toBeInTheDocument();
});
it("blocks submit and shows errors when required fields are missing", async () => {
const onSubmit = vi.fn();
render(<UserForm onSubmit={onSubmit} />);
await waitForPickers();
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(await screen.findByText("Name is required.")).toBeInTheDocument();
expect(screen.getByText("Username is required.")).toBeInTheDocument();
expect(screen.getByText("Email is required.")).toBeInTheDocument();
expect(screen.getByText("Branch is required.")).toBeInTheDocument();
expect(onSubmit).not.toHaveBeenCalled();
});
it("blocks submit on an invalid email", async () => {
const onSubmit = vi.fn();
render(<UserForm onSubmit={onSubmit} />);
await waitForPickers();
await userEvent.type(screen.getByLabelText("Name"), "Jane");
await userEvent.type(screen.getByLabelText("Username"), "jane.doe");
await userEvent.type(screen.getByLabelText("Email"), "not-an-email");
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(await screen.findByText(/valid email/i)).toBeInTheDocument();
expect(onSubmit).not.toHaveBeenCalled();
});
it("builds the correct AdminUser payload on submit", async () => {
const onSubmit = vi.fn().mockResolvedValue(undefined);
render(<UserForm onSubmit={onSubmit} />);
await waitForPickers();
await userEvent.type(screen.getByLabelText("Name"), "Jane");
await userEvent.type(screen.getByLabelText("Surname"), "Doe");
await userEvent.type(screen.getByLabelText("Username"), "jane.doe");
await userEvent.type(screen.getByLabelText("Email"), "jane@example.com");
await userEvent.click(screen.getByLabelText("IT"));
await userEvent.click(screen.getByLabelText("HQ"));
await userEvent.click(screen.getByRole("combobox", { name: "Branch" }));
await userEvent.click(screen.getByRole("option", { name: /Kuala Lumpur/ }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith({
id: undefined,
name: "Jane",
surname: "Doe",
username: "jane.doe",
email: "jane@example.com",
enabled: true,
status: "Y",
roles: expect.arrayContaining([
{ id: 1, name: "ROLE_IT" },
{ id: 2, name: "ROLE_HQ" },
]),
branch: { id: 3 },
}),
);
});
it("removing a selected role chip unchecks it", async () => {
const onSubmit = vi.fn().mockResolvedValue(undefined);
render(<UserForm onSubmit={onSubmit} />);
await waitForPickers();
await userEvent.click(screen.getByLabelText("IT"));
expect(screen.getByRole("checkbox", { name: "IT" })).toBeChecked();
await userEvent.click(screen.getByRole("button", { name: "Remove IT" }));
expect(screen.getByRole("checkbox", { name: "IT" })).not.toBeChecked();
});
it("prefills from an initial AdminUser (edit mode)", async () => {
render(
<UserForm
initial={{
id: 9,
name: "Jane",
surname: "Doe",
username: "jane.doe",
email: "jane@example.com",
enabled: false,
status: "N",
roles: [{ id: 1, name: "ROLE_IT" }],
branch: { id: 3 },
}}
onSubmit={vi.fn()}
/>,
);
await waitForPickers();
expect(screen.getByLabelText("Name")).toHaveValue("Jane");
expect(screen.getByLabelText("Username")).toHaveValue("jane.doe");
expect(screen.getByLabelText("Email")).toHaveValue("jane@example.com");
expect(screen.getByRole("checkbox", { name: "IT" })).toBeChecked();
expect(screen.getByLabelText("Enabled")).not.toBeChecked();
});
it("disables submit while the save is pending", async () => {
let resolveSubmit!: () => void;
const onSubmit = vi.fn(
() =>
new Promise<void>((resolve) => {
resolveSubmit = resolve;
}),
);
render(<UserForm onSubmit={onSubmit} />);
await waitForPickers();
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/ }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
await waitFor(() => expect(screen.getByRole("button", { name: /save/i })).toBeDisabled());
resolveSubmit();
});
it("shows an error and re-enables submit when onSubmit rejects", async () => {
const onSubmit = vi.fn().mockRejectedValue(new Error("Username already taken"));
render(<UserForm onSubmit={onSubmit} />);
await waitForPickers();
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/ }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(await screen.findByText(/username already taken/i)).toBeInTheDocument();
expect(screen.getByRole("button", { name: /save/i })).not.toBeDisabled();
});
});