Files
co-lite/solvway-admin-portal/app/reference/page.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

485 lines
20 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { ReferenceRow } from "@/app/lib/types";
const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
}));
const svc = vi.hoisted(() => ({
listReferenceRows: vi.fn(),
createReferenceRow: vi.fn(),
updateReferenceRow: vi.fn(),
}));
vi.mock("@/app/lib/services/adminReference", () => ({
listReferenceRows: (table: string) => svc.listReferenceRows(table),
createReferenceRow: (table: string, p: unknown) => svc.createReferenceRow(table, p),
updateReferenceRow: (table: string, id: number, p: unknown) => svc.updateReferenceRow(table, id, p),
}));
const bankPanel = vi.hoisted(() => ({ props: [] as { adding: boolean }[] }));
vi.mock("./BankPanel", () => ({
BankPanel: (props: { adding: boolean; onAddingChange: (open: boolean) => void }) => {
bankPanel.props.push(props);
return (
<div data-testid="bank-panel" data-adding={String(props.adding)}>
<button onClick={() => props.onAddingChange(false)}>close bank dialog</button>
</div>
);
},
}));
import ReferencePage from "./page";
const nationalityRows: ReferenceRow[] = [
{ id: 1, code: "MY", name: "Malaysia", isoCode: "MYS", messageTag: "nationality.MY" },
{ id: 7, code: "SG", name: "Singapore", isoCode: "SGP", messageTag: "nationality.SG" },
];
const preferredLanguageRows: ReferenceRow[] = [
{ id: 2, code: "EN", name: "English" },
{ id: 3, code: "CN", name: "Chinese" },
];
const marritalStatusRows: ReferenceRow[] = [
{ id: 4, code: "MAR", name: "Married", messageTag: "ms.MAR", spouseRequired: "Y" },
{ id: 5, code: "SIN", name: "Single", messageTag: "ms.SIN", spouseRequired: "N" },
];
function renderPage() {
return render(<ReferencePage />);
}
beforeEach(() => {
auth.hasModule.mockReset();
auth.loading = false;
svc.listReferenceRows.mockReset();
svc.createReferenceRow.mockReset();
svc.updateReferenceRow.mockReset();
svc.listReferenceRows.mockResolvedValue(nationalityRows);
});
describe("module gating (BO_REFERENCE_DATA, id 41800000)", () => {
it("renders the not-authorised state and no rows for a user lacking the module", async () => {
auth.hasModule.mockReturnValue(false);
renderPage();
expect(await screen.findByTestId("not-authorized")).toBeInTheDocument();
expect(screen.queryByText("Malaysia")).not.toBeInTheDocument();
expect(svc.listReferenceRows).not.toHaveBeenCalled();
});
it("renders the management page for a user granted the module", async () => {
auth.hasModule.mockReturnValue(true);
renderPage();
expect(await screen.findByText("Malaysia")).toBeInTheDocument();
expect(screen.queryByTestId("not-authorized")).not.toBeInTheDocument();
});
});
describe("table picker + generic grid", () => {
beforeEach(() => {
auth.hasModule.mockReturnValue(true);
});
it("lists the five reference tables in the picker and loads the first one", async () => {
renderPage();
await screen.findByText("Malaysia");
expect(svc.listReferenceRows).toHaveBeenCalledWith("nationality");
for (const label of ["Nationality", "Marital status", "Race", "Preferred language", "Salutation"]) {
expect(screen.getByRole("tab", { name: label })).toBeInTheDocument();
}
});
it("puts the table inside the active tab's tabpanel", async () => {
renderPage();
await screen.findByText("Malaysia");
expect(within(screen.getByRole("tabpanel")).getByRole("table")).toBeInTheDocument();
});
it("renders exactly the picked table's columns: nationality shows an iso_code column", async () => {
renderPage();
await screen.findByText("Malaysia");
expect(screen.getByText("ISO code")).toBeInTheDocument();
expect(screen.getByText("MYS")).toBeInTheDocument();
expect(screen.getByText("Message tag")).toBeInTheDocument();
});
it("switching to preferredLanguage refetches and hides the message_tag column", async () => {
const user = userEvent.setup();
svc.listReferenceRows.mockResolvedValueOnce(nationalityRows).mockResolvedValueOnce(preferredLanguageRows);
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("tab", { name: "Preferred language" }));
await waitFor(() => expect(svc.listReferenceRows).toHaveBeenCalledWith("preferredLanguage"));
expect(await screen.findByText("English")).toBeInTheDocument();
// The spec scenario: preferredLanguage's grid MUST NOT render a message_tag column.
expect(screen.queryByText("Message tag")).not.toBeInTheDocument();
expect(screen.queryByText("ISO code")).not.toBeInTheDocument();
expect(screen.getByText("Code")).toBeInTheDocument();
expect(screen.getByText("Name")).toBeInTheDocument();
});
it("shows Yes/No for the spouse-required option column, under a level-1 heading", async () => {
const user = userEvent.setup();
svc.listReferenceRows.mockResolvedValueOnce(nationalityRows).mockResolvedValueOnce(marritalStatusRows);
renderPage();
await screen.findByText("Malaysia");
expect(screen.getByRole("heading", { level: 1, name: "Reference data" })).toBeInTheDocument();
await user.click(screen.getByRole("tab", { name: "Marital status" }));
const married = (await screen.findByText("Married")).closest("tr")!;
expect(married).toHaveTextContent("Yes");
expect(screen.getByText("Single").closest("tr")).toHaveTextContent("No");
expect(screen.getByText("Single").closest("tr")).not.toHaveTextContent(/\bN\b/);
});
it("shows a loading skeleton while the rows load", () => {
auth.hasModule.mockReturnValue(true);
svc.listReferenceRows.mockReturnValue(new Promise(() => {}));
renderPage();
expect(screen.getByRole("status")).toBeInTheDocument();
});
it("switching back to nationality shows the iso_code column again", async () => {
const user = userEvent.setup();
svc.listReferenceRows
.mockResolvedValueOnce(nationalityRows)
.mockResolvedValueOnce(preferredLanguageRows)
.mockResolvedValueOnce(nationalityRows);
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("tab", { name: "Preferred language" }));
await screen.findByText("English");
await user.click(screen.getByRole("tab", { name: "Nationality" }));
expect(await screen.findByText("ISO code")).toBeInTheDocument();
expect(screen.getByText("MYS")).toBeInTheDocument();
});
it("surfaces a load failure inline", async () => {
svc.listReferenceRows.mockRejectedValueOnce(new Error("reference service down"));
renderPage();
expect(await screen.findByText("reference service down")).toBeInTheDocument();
});
// fix-pass Item 3: a failed fetch must not leave the previous table's rows on screen —
// switching tables with the new fetch failing shows the error and drops the stale rows.
it("clears the previous table's rows when the new table's fetch fails", async () => {
const user = userEvent.setup();
svc.listReferenceRows
.mockResolvedValueOnce(nationalityRows)
.mockRejectedValueOnce(new Error("race service down"));
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("tab", { name: "Race" }));
expect(await screen.findByText("race service down")).toBeInTheDocument();
// The stale nationality rows are gone — no phantom data under race's headers.
expect(screen.queryByText("Malaysia")).not.toBeInTheDocument();
expect(screen.queryByText("MYS")).not.toBeInTheDocument();
expect(svc.listReferenceRows).toHaveBeenCalledWith("race");
});
});
describe("the add form", () => {
beforeEach(() => {
auth.hasModule.mockReturnValue(true);
});
it("leaves code editable on create and saves through createReferenceRow", async () => {
const user = userEvent.setup();
svc.createReferenceRow.mockResolvedValue({ id: 9, code: "AAA", name: "Alpha" });
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("button", { name: "Add row" }));
const dialog = await screen.findByRole("dialog");
const codeInput = within(dialog).getByLabelText("Code");
expect(codeInput).not.toHaveAttribute("readonly");
await user.type(codeInput, "AAA");
await user.type(within(dialog).getByLabelText("Name"), "Alpha");
await user.click(within(dialog).getByRole("button", { name: "Save" }));
await waitFor(() =>
expect(svc.createReferenceRow).toHaveBeenCalledWith(
"nationality",
expect.objectContaining({ code: "AAA", name: "Alpha" }),
),
);
await waitFor(() => expect(svc.listReferenceRows).toHaveBeenCalledTimes(2));
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
});
it("shows a duplicate-code 409 inline and keeps the dialog open (no false success)", async () => {
const user = userEvent.setup();
svc.createReferenceRow.mockRejectedValue(new Error("code already exists: MY"));
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("button", { name: "Add row" }));
const dialog = await screen.findByRole("dialog");
await user.type(within(dialog).getByLabelText("Code"), "MY");
await user.type(within(dialog).getByLabelText("Name"), "Malaysia");
await user.click(within(dialog).getByRole("button", { name: "Save" }));
expect(await screen.findByText("code already exists: MY")).toBeInTheDocument();
expect(screen.getByRole("dialog")).toBeInTheDocument(); // still open, nothing was saved
expect(svc.listReferenceRows).toHaveBeenCalledTimes(1); // no refetch after a failed save
});
it("shows a validation 422 inline and keeps the dialog open", async () => {
const user = userEvent.setup();
svc.createReferenceRow.mockRejectedValue(new Error("code must be at most 3 characters."));
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("button", { name: "Add row" }));
const dialog = await screen.findByRole("dialog");
await user.type(within(dialog).getByLabelText("Code"), "TOOLONG");
await user.type(within(dialog).getByLabelText("Name"), "Alpha");
await user.click(within(dialog).getByRole("button", { name: "Save" }));
expect(await screen.findByText("code must be at most 3 characters.")).toBeInTheDocument();
expect(screen.getByRole("dialog")).toBeInTheDocument();
});
// fix-pass Item 2: the marital spouse_required select's DISPLAYED default must be the value
// actually submitted — an untouched add form persists "Y" (what "Yes" shows), never the
// server's "N" default.
it("persists the displayed spouse_required default when the marital add form is untouched", async () => {
const user = userEvent.setup();
svc.listReferenceRows
.mockResolvedValueOnce(nationalityRows)
.mockResolvedValueOnce(marritalStatusRows);
svc.createReferenceRow.mockResolvedValue({ id: 10, code: "WID", name: "Widowed", spouseRequired: "Y" });
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("tab", { name: "Marital status" }));
await screen.findByText("Married");
await user.click(screen.getByRole("button", { name: "Add row" }));
const dialog = await screen.findByRole("dialog");
// The select shows its default without any interaction.
expect(within(dialog).getByLabelText("Spouse required")).toHaveTextContent("Yes");
await user.type(within(dialog).getByLabelText("Code"), "WID");
await user.type(within(dialog).getByLabelText("Name"), "Widowed");
await user.click(within(dialog).getByRole("button", { name: "Save" }));
// The submitted payload carries the same value the select displayed.
await waitFor(() =>
expect(svc.createReferenceRow).toHaveBeenCalledWith(
"marritalStatus",
expect.objectContaining({ code: "WID", name: "Widowed", spouseRequired: "Y" }),
),
);
});
});
describe("the edit form", () => {
beforeEach(() => {
auth.hasModule.mockReturnValue(true);
});
it("prefills from the row, locks code read-only with the rename caveat, and saves via updateReferenceRow(id)", async () => {
const user = userEvent.setup();
svc.updateReferenceRow.mockResolvedValue({ ...nationalityRows[0], name: "Malaysia (MY)" });
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("button", { name: /edit MY/i }));
const dialog = await screen.findByRole("dialog");
expect(within(dialog).getByLabelText("Code")).toHaveValue("MY");
expect(within(dialog).getByLabelText("Name")).toHaveValue("Malaysia");
expect(within(dialog).getByLabelText("ISO code")).toHaveValue("MYS");
// Code is the stable key — read-only on edit, with the rename caveat.
expect(within(dialog).getByLabelText("Code")).toHaveAttribute("readonly");
expect(within(dialog).getByText(/cannot be renamed/i)).toBeInTheDocument();
await user.clear(within(dialog).getByLabelText("Name"));
await user.type(within(dialog).getByLabelText("Name"), "Malaysia (MY)");
await user.click(within(dialog).getByRole("button", { name: "Save" }));
await waitFor(() =>
expect(svc.updateReferenceRow).toHaveBeenCalledWith(
"nationality",
1,
expect.objectContaining({ code: "MY", name: "Malaysia (MY)", isoCode: "MYS" }),
),
);
});
it("shows a conflict inline on edit too and keeps the dialog open", async () => {
const user = userEvent.setup();
svc.updateReferenceRow.mockRejectedValue(new Error("code already exists: SG"));
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("button", { name: /edit MY/i }));
const dialog = await screen.findByRole("dialog");
await user.clear(within(dialog).getByLabelText("Name"));
await user.type(within(dialog).getByLabelText("Name"), "Malaysia Baru");
await user.click(within(dialog).getByRole("button", { name: "Save" }));
expect(await screen.findByText("code already exists: SG")).toBeInTheDocument();
expect(screen.getByRole("dialog")).toBeInTheDocument();
});
});
describe("Bank tab", () => {
beforeEach(() => {
auth.hasModule.mockReturnValue(true);
bankPanel.props = [];
});
it("shows a Bank button after the registry tables and renders the bank panel", async () => {
const user = userEvent.setup();
renderPage();
await screen.findByText("Malaysia");
const picker = screen.getByRole("tablist", { name: "Reference table" });
const buttons = within(picker).getAllByRole("tab").map((b) => b.textContent);
expect(buttons[buttons.length - 1]).toBe("Bank");
svc.listReferenceRows.mockClear();
await user.click(within(picker).getByRole("tab", { name: "Bank" }));
expect(screen.getByTestId("bank-panel")).toBeInTheDocument();
expect(screen.queryByText("Malaysia")).not.toBeInTheDocument(); // generic grid gone
expect(svc.listReferenceRows).not.toHaveBeenCalled();
});
it("Add row opens the bank dialog while Bank is active", async () => {
const user = userEvent.setup();
renderPage();
await screen.findByText("Malaysia");
await user.click(screen.getByRole("tab", { name: "Bank" }));
expect(screen.getByTestId("bank-panel")).toHaveAttribute("data-adding", "false");
await user.click(screen.getByRole("button", { name: /Add row/ }));
expect(screen.getByTestId("bank-panel")).toHaveAttribute("data-adding", "true");
expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); // no generic dialog
await user.click(screen.getByText("close bank dialog"));
expect(screen.getByTestId("bank-panel")).toHaveAttribute("data-adding", "false");
});
});
// unify-admin-listing-toolbar: "Code or name" search in the filter bar over the loaded rows.
describe("code or name search", () => {
beforeEach(() => {
auth.hasModule.mockReturnValue(true);
});
it("filters the loaded rows by part of the code or name, ignoring case", async () => {
const user = userEvent.setup();
renderPage();
await screen.findByText("Malaysia");
const bar = document.querySelector('[data-slot="filter-panel"]') as HTMLElement;
expect(within(bar).getByRole("group", { name: "Code or name" })).toBeInTheDocument();
await user.type(screen.getByRole("searchbox", { name: "Code or name" }), "SINGA");
await waitFor(() => expect(screen.queryByText("Malaysia")).not.toBeInTheDocument());
expect(screen.getByText("Singapore")).toBeInTheDocument();
expect(svc.listReferenceRows).toHaveBeenCalledTimes(1);
});
it("says when nothing matches, and Clear filters brings every row back", async () => {
const user = userEvent.setup();
renderPage();
await screen.findByText("Malaysia");
await user.type(screen.getByRole("searchbox", { name: "Code or name" }), "zzz");
expect(await screen.findByText("No rows match your search")).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Clear filters" }));
expect(await screen.findByText("Malaysia")).toBeInTheDocument();
expect(screen.getByText("Singapore")).toBeInTheDocument();
});
});
// fit-admin-listing-pages: the loaded rows are paged in the browser with the shared pager.
describe("paging the loaded rows", () => {
const many: ReferenceRow[] = Array.from({ length: 45 }, (_, i) => {
const n = String(i + 1).padStart(2, "0");
return { id: 100 + i, code: `C${n}`, name: `Country ${n}`, isoCode: `X${n}`, messageTag: `nationality.C${n}` };
});
beforeEach(() => {
auth.hasModule.mockReturnValue(true);
svc.listReferenceRows.mockResolvedValue(many);
});
it("shows one page of rows and Next shows the following rows", async () => {
const user = userEvent.setup();
renderPage();
expect(await screen.findByText("Country 01")).toBeInTheDocument();
expect(screen.getByText("Country 20")).toBeInTheDocument();
expect(screen.queryByText("Country 21")).not.toBeInTheDocument();
expect(screen.getByText("1–20 of 45")).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: /next/i }));
expect(await screen.findByText("Country 21")).toBeInTheDocument();
expect(screen.queryByText("Country 01")).not.toBeInTheDocument();
expect(screen.getByText("21–40 of 45")).toBeInTheDocument();
expect(svc.listReferenceRows).toHaveBeenCalledTimes(1);
});
it("filters before paging, and a new search returns to page 1", async () => {
const user = userEvent.setup();
renderPage();
await screen.findByText("Country 01");
await user.click(screen.getByRole("button", { name: /next/i }));
await user.click(screen.getByRole("button", { name: /next/i }));
expect(await screen.findByText("Country 41")).toBeInTheDocument();
await user.type(screen.getByRole("searchbox", { name: "Code or name" }), "Country 0");
expect(await screen.findByText("1–9 of 9")).toBeInTheDocument();
expect(screen.getByText("Country 01")).toBeInTheDocument();
expect(screen.queryByText("Country 41")).not.toBeInTheDocument();
});
it("returns to page 1 when another table is picked", async () => {
const user = userEvent.setup();
renderPage();
await screen.findByText("Country 01");
await user.click(screen.getByRole("button", { name: /next/i }));
expect(await screen.findByText("Country 21")).toBeInTheDocument();
await user.click(screen.getByRole("tab", { name: "Preferred language" }));
expect(await screen.findByText("1–20 of 45")).toBeInTheDocument();
});
it("shows the last page when a reload leaves fewer rows than the page needs", async () => {
const user = userEvent.setup();
svc.updateReferenceRow.mockResolvedValue(many[40]);
renderPage();
await screen.findByText("Country 01");
await user.click(screen.getByRole("button", { name: /next/i }));
await user.click(screen.getByRole("button", { name: /next/i }));
await user.click(await screen.findByRole("button", { name: "Edit C41" }));
svc.listReferenceRows.mockResolvedValue(many.slice(0, 25));
await user.click(within(await screen.findByRole("dialog")).getByRole("button", { name: "Save" }));
expect(await screen.findByText("21–25 of 25")).toBeInTheDocument();
expect(screen.getByText("Country 21")).toBeInTheDocument();
});
});