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

109 lines
4.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within, fireEvent } from "@testing-library/react";
import type { ReferenceRow } from "@/app/lib/types";
// BABYBOO-54 task 1.1: the reference grid pages in the browser (page size mocked to 20), so a sort
// must order the WHOLE list before paging, with no new request.
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),
}));
vi.mock("./BankPanel", () => ({ BankPanel: () => <div data-testid="bank-panel" /> }));
import ReferencePage from "./page";
const pad = (n: number) => String(n).padStart(2, "0");
// 45 rows delivered in DESCENDING code order, so page 1 unsorted holds the globally LARGEST codes.
const BIG: ReferenceRow[] = Array.from({ length: 45 }, (_, i) => {
const n = 45 - i;
return { id: 100 + n, code: `C${pad(n)}`, name: `Country ${pad(n)}`, isoCode: `X${pad(n)}`, messageTag: `nationality.C${pad(n)}` };
});
beforeEach(() => {
auth.hasModule.mockReset().mockReturnValue(true);
svc.listReferenceRows.mockReset().mockResolvedValue(BIG);
});
async function table() {
await screen.findByText("Country 45");
return within(screen.getByRole("table"));
}
function column(t: ReturnType<typeof within>, header: string) {
const idx = t.getAllByRole("columnheader").findIndex((h: HTMLElement) => h.textContent?.trim() === header);
return t.getAllByRole("row").slice(1).map((r: HTMLElement) => within(r).getAllByRole("cell")[idx].textContent);
}
const click = (t: ReturnType<typeof within>, header: string) => fireEvent.click(t.getByRole("button", { name: header }));
describe("/reference - client-side header sorting", () => {
it("Code asc orders the whole list before paging: page 1 holds the globally smallest codes", async () => {
render(<ReferencePage />);
const t = await table();
expect(column(t, "Code")[0]).toBe("C45");
click(t, "Code");
const codes = column(t, "Code");
expect(codes).toHaveLength(20);
expect(codes[0]).toBe("C01");
expect(codes[19]).toBe("C20");
expect(t.getByRole("columnheader", { name: "Code" })).toHaveAttribute("aria-sort", "ascending");
});
it("Code desc after asc puts the globally largest codes back on page 1", async () => {
render(<ReferencePage />);
const t = await table();
click(t, "Code");
click(t, "Code");
const codes = column(t, "Code");
expect(codes[0]).toBe("C45");
expect(codes[19]).toBe("C26");
expect(t.getByRole("columnheader", { name: "Code" })).toHaveAttribute("aria-sort", "descending");
});
it("sorting makes no new request", async () => {
render(<ReferencePage />);
const t = await table();
click(t, "Code");
click(t, "Code");
expect(svc.listReferenceRows).toHaveBeenCalledTimes(1);
});
it("Name and ISO code headers sort too; Message tag headers keep their labels", async () => {
render(<ReferencePage />);
const t = await table();
click(t, "Name");
expect(column(t, "Name")[0]).toBe("Country 01");
click(t, "ISO code");
expect(column(t, "ISO code")[0]).toBe("X01");
click(t, "ISO code");
expect(column(t, "ISO code")[0]).toBe("X45");
expect(t.getByRole("columnheader", { name: "Message tag" })).toBeInTheDocument();
});
it("Actions column has no sort button", async () => {
render(<ReferencePage />);
const t = await table();
const heads = t.getAllByRole("columnheader");
expect(within(heads[heads.length - 1]).queryByRole("button")).toBeNull();
});
it("a row whose ISO code is empty (shown as the dash placeholder) sorts last ascending", async () => {
svc.listReferenceRows.mockResolvedValue([
{ id: 1, code: "C03", name: "Country 03", isoCode: "", messageTag: "nationality.C03" },
{ id: 2, code: "C02", name: "Country 02", isoCode: "X02", messageTag: "nationality.C02" },
{ id: 3, code: "C01", name: "Country 01", isoCode: "X01", messageTag: "nationality.C01" },
] as ReferenceRow[]);
render(<ReferencePage />);
await screen.findByText("Country 03");
const t = within(screen.getByRole("table"));
click(t, "ISO code");
expect(column(t, "ISO code")).toEqual(["X01", "X02", "\u2014"]);
});
});