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
145 lines
5.4 KiB
TypeScript
145 lines
5.4 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(),
|
|
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(<UserForm initial={BASE_USER} onSubmit={vi.fn()} />);
|
|
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(<UserForm initial={BASE_USER} onSubmit={vi.fn()} />);
|
|
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(<UserForm initial={{ ...BASE_USER, event: { id: 9 } } as never} onSubmit={vi.fn()} />);
|
|
await openEvent();
|
|
expect(await screen.findAllByText("Old Bazaar")).not.toHaveLength(0);
|
|
});
|
|
|
|
it("filters options by name or code, case-insensitively", async () => {
|
|
await ready(<UserForm initial={BASE_USER} onSubmit={vi.fn()} />);
|
|
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(<UserForm initial={BASE_USER} onSubmit={onSubmit} />);
|
|
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(<UserForm initial={{ ...BASE_USER, event: { id: 8 } } as never} onSubmit={onSubmit} />);
|
|
|
|
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(<UserForm initial={{ ...BASE_USER, event: { id: 8 } } as never} onSubmit={onSubmit} />);
|
|
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(<UserForm initial={BASE_USER} onSubmit={onSubmit} />);
|
|
await eventField();
|
|
await save();
|
|
|
|
await waitFor(() => expect(onSubmit).toHaveBeenCalled());
|
|
expect(screen.queryByText(/event is required/i)).not.toBeInTheDocument();
|
|
});
|
|
});
|