import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within, fireEvent } from "@testing-library/react";
import type { BankCountry, BankRow } from "@/app/lib/types";
// BABYBOO-54 task 1.1: the bank list sorts in the browser; the Actions column does not sort.
const svc = vi.hoisted(() => ({ listBankCountries: vi.fn(), listBanks: vi.fn(), createBank: vi.fn(), updateBank: vi.fn() }));
vi.mock("@/app/lib/services/adminBank", () => ({
listBankCountries: () => svc.listBankCountries(),
listBanks: (c: string) => svc.listBanks(c),
createBank: (p: unknown) => svc.createBank(p),
updateBank: (id: number, p: unknown) => svc.updateBank(id, p),
}));
import { BankPanel } from "./BankPanel";
const countries: BankCountry[] = [{ id: 2, code: "MY", name: "Malaysia" }];
// API order is deliberately sorted by none of the columns.
const banks: BankRow[] = [
{ id: 5, code: "MBB", name: "Maybank", swiftCode: "MBBEMYKL", bnmCode: "27", bankid: "MBB0228", messageTag: "bank.MBB" },
{ id: 6, code: "CIMB", name: "CIMB Bank", swiftCode: "CIBBMYKL", bnmCode: "35", bankid: "CIM0001", messageTag: "bank.CIMB" },
{ id: 7, code: "HLB", name: "Hong Leong", swiftCode: "HLBBMYKL", bnmCode: "11", bankid: "HLB0099", messageTag: "bank.HLB" },
];
beforeEach(() => {
Object.values(svc).forEach((f) => f.mockReset());
svc.listBankCountries.mockResolvedValue(countries);
svc.listBanks.mockResolvedValue(banks);
});
async function table() {
render();
await screen.findByText("Maybank");
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("BankPanel - client-side header sorting", () => {
it("Code header reorders asc then desc", async () => {
const t = await table();
expect(column(t, "Code")).toEqual(["MBB", "CIMB", "HLB"]);
click(t, "Code");
expect(column(t, "Code")).toEqual(["CIMB", "HLB", "MBB"]);
expect(t.getByRole("columnheader", { name: "Code" })).toHaveAttribute("aria-sort", "ascending");
click(t, "Code");
expect(column(t, "Code")).toEqual(["MBB", "HLB", "CIMB"]);
expect(t.getByRole("columnheader", { name: "Code" })).toHaveAttribute("aria-sort", "descending");
});
it.each([
["Name", ["CIMB Bank", "Hong Leong", "Maybank"]],
["SWIFT", ["CIBBMYKL", "HLBBMYKL", "MBBEMYKL"]],
["BNM code", ["11", "27", "35"]],
["Bank ID", ["CIM0001", "HLB0099", "MBB0228"]],
["Message tag", ["bank.CIMB", "bank.HLB", "bank.MBB"]],
])("%s header reorders asc then desc", async (header, asc) => {
const t = await table();
click(t, header);
expect(column(t, header)).toEqual(asc);
click(t, header);
expect(column(t, header)).toEqual([...asc].reverse());
});
it("Actions column has no sort button; sorting makes no new request", async () => {
const t = await table();
const heads = t.getAllByRole("columnheader");
expect(within(heads[heads.length - 1]).queryByRole("button")).toBeNull();
click(t, "Code");
expect(svc.listBanks).toHaveBeenCalledTimes(1);
});
});