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: () =>
})); 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, 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, 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(); 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(); 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(); 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(); 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(); 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(); await screen.findByText("Country 03"); const t = within(screen.getByRole("table")); click(t, "ISO code"); expect(column(t, "ISO code")).toEqual(["X01", "X02", "\u2014"]); }); });