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 { BankCountry, BankRow } from "@/app/lib/types"; function deferred() { let resolve!: (value: T) => void; const promise = new Promise((res) => { resolve = res; }); return { promise, resolve }; } 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" }, { id: 9, code: "SG", name: "Singapore" }, ]; const myBanks: 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" }, ]; const sgBanks: BankRow[] = [{ id: 40, code: "DBS", name: "DBS Bank" }]; function renderPanel(adding = false, onAddingChange = vi.fn()) { return render(); } beforeEach(() => { Object.values(svc).forEach((f) => f.mockReset()); svc.listBankCountries.mockResolvedValue(countries); svc.listBanks.mockImplementation((c: string) => Promise.resolve(c === "SG" ? sgBanks : myBanks)); }); describe("BankPanel list", () => { it("preselects the first country and lists its banks with every column", async () => { renderPanel(); expect(await screen.findByText("Maybank")).toBeInTheDocument(); expect(svc.listBanks).toHaveBeenCalledWith("MY"); for (const header of ["Code", "Name", "SWIFT", "BNM code", "Bank ID", "Branch code", "Message tag"]) { expect(screen.getByRole("columnheader", { name: header })).toBeInTheDocument(); } expect(screen.getByText("MBBEMYKL")).toBeInTheDocument(); expect(screen.getByText("2 banks")).toBeInTheDocument(); }); it("search narrows by code or name, case-insensitively", async () => { const user = userEvent.setup(); renderPanel(); await screen.findByText("Maybank"); await user.type(screen.getByLabelText("Search"), "maybank"); expect(screen.getByText("Maybank")).toBeInTheDocument(); expect(screen.queryByText("CIMB Bank")).not.toBeInTheDocument(); expect(screen.getByText("1 of 2 banks")).toBeInTheDocument(); await user.clear(screen.getByLabelText("Search")); await user.type(screen.getByLabelText("Search"), "cimb"); expect(screen.getByText("CIMB Bank")).toBeInTheDocument(); }); it("shows a no-match state when the search matches nothing", async () => { const user = userEvent.setup(); renderPanel(); await screen.findByText("Maybank"); await user.type(screen.getByLabelText("Search"), "zzz"); expect(screen.getByText(/No banks match/)).toBeInTheDocument(); }); it("switching country loads that country's banks", async () => { const user = userEvent.setup(); renderPanel(); await screen.findByText("Maybank"); await user.click(screen.getByRole("combobox", { name: "Country" })); await user.click(await screen.findByRole("option", { name: "Singapore" })); expect(await screen.findByText("DBS Bank")).toBeInTheDocument(); expect(screen.queryByText("Maybank")).not.toBeInTheDocument(); }); it("error hides previous rows and offers a retry", async () => { const user = userEvent.setup(); renderPanel(); await screen.findByText("Maybank"); svc.listBanks.mockRejectedValueOnce(new Error("Gateway timeout")); await user.click(screen.getByRole("combobox", { name: "Country" })); await user.click(await screen.findByRole("option", { name: "Singapore" })); expect(await screen.findByText("Gateway timeout")).toBeInTheDocument(); expect(screen.queryByText("Maybank")).not.toBeInTheDocument(); expect(screen.queryByText("DBS Bank")).not.toBeInTheDocument(); }); it("a country with no banks shows the empty state naming it", async () => { svc.listBanks.mockResolvedValue([]); renderPanel(); expect(await screen.findByText("No banks for Malaysia yet — add the first one.")).toBeInTheDocument(); }); it("no active country explains where to activate one", async () => { svc.listBankCountries.mockResolvedValue([]); renderPanel(); expect(await screen.findByText(/No active countries/)).toBeInTheDocument(); expect(svc.listBanks).not.toHaveBeenCalled(); }); it("shows a loading skeleton while a country's banks load", async () => { svc.listBanks.mockReturnValue(new Promise(() => {})); renderPanel(); expect(await screen.findByRole("status")).toBeInTheDocument(); }); it("switching country never shows the old country's rows while the new fetch is in flight", async () => { const user = userEvent.setup(); const sg = deferred(); svc.listBanks.mockImplementation((c: string) => (c === "SG" ? sg.promise : Promise.resolve(myBanks))); renderPanel(); await screen.findByText("Maybank"); await user.click(screen.getByRole("combobox", { name: "Country" })); await user.click(await screen.findByRole("option", { name: "Singapore" })); expect(screen.queryByText("Maybank")).not.toBeInTheDocument(); sg.resolve(sgBanks); expect(await screen.findByText("DBS Bank")).toBeInTheDocument(); }); }); describe("BankPanel add-row without a ready country", () => { it("zero active countries: Add is a no-op, never a delayed dialog", async () => { const onAddingChange = vi.fn(); svc.listBankCountries.mockResolvedValue([]); renderPanel(true, onAddingChange); await waitFor(() => expect(onAddingChange).toHaveBeenCalledWith(false)); expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); }); it("countries still loading: Add is a no-op and the dialog never pops open once they resolve", async () => { const onAddingChange = vi.fn(); const countriesDeferred = deferred(); svc.listBankCountries.mockReturnValue(countriesDeferred.promise); const { rerender } = renderPanel(true, onAddingChange); await waitFor(() => expect(onAddingChange).toHaveBeenCalledWith(false)); expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); // The page (parent) honours the no-op by flipping `adding` back to false. rerender(); countriesDeferred.resolve(countries); await screen.findByText("Maybank"); // countries + banks finished loading expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); }); }); describe("BankPanel dialog", () => { it("add opens with the country read-only and creates with the full payload", async () => { const user = userEvent.setup(); const onAddingChange = vi.fn(); svc.createBank.mockResolvedValue({ id: 141, code: "NEWB", name: "New Bank" }); // Mount with the country already ready, then flip to adding=true (as the 409 test does): the // panel's own reset effect calling onAddingChange(false) at mount must not be what satisfies // the final assertion below. const { rerender } = renderPanel(false, onAddingChange); await screen.findByText("Maybank"); rerender(); const dialog = await screen.findByRole("dialog"); expect(within(dialog).getByRole("heading", { name: "Add bank to Malaysia" })).toBeInTheDocument(); expect(within(dialog).getByLabelText("Country")).toHaveAttribute("readonly"); expect(within(dialog).getByLabelText("Code")).not.toHaveAttribute("readonly"); await user.type(within(dialog).getByLabelText("Code"), " NEWB "); await user.type(within(dialog).getByLabelText("Name"), "New Bank"); await user.type(within(dialog).getByLabelText("SWIFT code"), "NEWBMYKL"); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.createBank).toHaveBeenCalledWith({ code: "NEWB", name: "New Bank", countryCode: "MY", swiftCode: "NEWBMYKL", bnmCode: "", bankid: "", branchCode: "", messageTag: "", }), ); // Proves the save path actually closes the dialog, not just the mount-time reset effect. expect(onAddingChange).toHaveBeenCalledWith(false); await waitFor(() => expect(svc.listBanks).toHaveBeenCalledTimes(2)); // refetched after save }); it("add opens with focus on the Code field, not the read-only Country field", async () => { renderPanel(true); const dialog = await screen.findByRole("dialog"); await waitFor(() => expect(within(dialog).getByLabelText("Code")).toHaveFocus()); }); it("edit opens with focus on the Name field, not the read-only Code field", async () => { const user = userEvent.setup(); renderPanel(); await screen.findByText("Maybank"); await user.click(screen.getByRole("button", { name: "Edit MBB" })); const dialog = await screen.findByRole("dialog"); await waitFor(() => expect(within(dialog).getByLabelText("Name")).toHaveFocus()); }); it("a 409 stays inline and keeps the dialog open", async () => { const user = userEvent.setup(); const onAddingChange = vi.fn(); svc.createBank.mockRejectedValue(new Error("MBB already exists in Malaysia.")); // Mount with the country already ready (a real "Add" click after the tab has loaded), not // adding=true at mount (that is the no-op-before-ready case covered separately). const { rerender } = renderPanel(false, onAddingChange); await screen.findByText("Maybank"); rerender(); const dialog = await screen.findByRole("dialog"); await user.type(within(dialog).getByLabelText("Code"), "MBB"); await user.type(within(dialog).getByLabelText("Name"), "Maybank"); await user.click(within(dialog).getByRole("button", { name: "Save" })); expect(await within(dialog).findByText("MBB already exists in Malaysia.")).toBeInTheDocument(); expect(screen.getByRole("dialog")).toBeInTheDocument(); expect(onAddingChange).not.toHaveBeenCalledWith(false); }); it("blank code or name is refused client-side without an API call", async () => { const user = userEvent.setup(); renderPanel(true); const dialog = await screen.findByRole("dialog"); await user.click(within(dialog).getByRole("button", { name: "Save" })); expect(await within(dialog).findByText("Code and name are required.")).toBeInTheDocument(); expect(svc.createBank).not.toHaveBeenCalled(); }); it("edit locks the code, shows the caveat, and sends cleared fields as empty strings", async () => { const user = userEvent.setup(); svc.updateBank.mockResolvedValue({ ...myBanks[0], swiftCode: "" }); renderPanel(); await screen.findByText("Maybank"); await user.click(screen.getByRole("button", { name: "Edit MBB" })); const dialog = await screen.findByRole("dialog"); expect(within(dialog).getByRole("heading", { name: "Edit bank MBB" })).toBeInTheDocument(); expect(within(dialog).getByLabelText("Code")).toHaveAttribute("readonly"); expect(within(dialog).getByText(/cannot be renamed/)).toBeInTheDocument(); await user.clear(within(dialog).getByLabelText("SWIFT code")); await user.click(within(dialog).getByRole("button", { name: "Save" })); await waitFor(() => expect(svc.updateBank).toHaveBeenCalledWith(5, { code: "MBB", name: "Maybank", countryCode: "MY", swiftCode: "", bnmCode: "27", bankid: "MBB0228", branchCode: "", messageTag: "bank.MBB", }), ); await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument()); }); });