import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import WalletAdjustDialog from "./WalletAdjustDialog"; const svc = vi.hoisted(() => ({ getMemberWalletBalances: vi.fn(), adjustWallet: vi.fn() })); vi.mock("@/app/lib/services/wallet", () => svc); const balances = [ { walletTypeCode: "PRODUCT", balance: 100, enabled: "Y" }, { walletTypeCode: "ACORN", balance: 50, enabled: "Y" }, { walletTypeCode: "APPLE", balance: 7, enabled: "Y" }, { walletTypeCode: "GIFT", balance: 3, enabled: "Y" }, ]; let uuidN = 0; const onOpenChange = vi.fn(); const onSaved = vi.fn(); function ui(open = true) { return ; } beforeEach(() => { uuidN = 0; vi.stubGlobal("crypto", { randomUUID: () => `uuid-${++uuidN}` }); svc.getMemberWalletBalances.mockReset(); svc.adjustWallet.mockReset(); svc.getMemberWalletBalances.mockResolvedValue(balances); svc.adjustWallet.mockResolvedValue({ refNo: "x", trxnType: "AJI", amount: 25.5, balance: 125.5 }); onOpenChange.mockReset(); onSaved.mockReset(); }); afterEach(() => vi.unstubAllGlobals()); async function lookup(user: ReturnType, code = "MY10001") { await user.type(screen.getByLabelText("Member code"), code); await user.tab(); } async function pickWallet(user: ReturnType, name: RegExp = /Product/) { await user.click(await screen.findByRole("combobox", { name: "Wallet" })); await user.click(await screen.findByRole("option", { name })); } async function fill( user: ReturnType, o: { direction?: "Add" | "Deduct"; amount?: string; reason?: string } = {}, ) { await lookup(user); await pickWallet(user); await user.click(screen.getByRole("radio", { name: o.direction ?? "Add" })); await user.type(screen.getByLabelText("Amount"), o.amount ?? "25.50"); await user.type(screen.getByLabelText("Reason"), o.reason ?? " Goodwill "); } describe("WalletAdjustDialog (BABYBOO-54)", () => { it("shows the not-found text for an unknown member", async () => { svc.getMemberWalletBalances.mockResolvedValue([]); const user = userEvent.setup(); render(ui()); await lookup(user, "NOPE"); expect(await screen.findByText("No member found with this code.")).toBeInTheDocument(); expect(svc.getMemberWalletBalances).toHaveBeenCalledWith("NOPE"); }); it("shows the not-found text when the balances lookup fails", async () => { svc.getMemberWalletBalances.mockRejectedValue(new Error("404")); const user = userEvent.setup(); render(ui()); await lookup(user, "NOPE"); expect(await screen.findByText("No member found with this code.")).toBeInTheDocument(); }); it("lists only the member's PRODUCT/ACORN/APPLE wallets, each with its balance", async () => { const user = userEvent.setup(); render(ui()); await lookup(user); await user.click(await screen.findByRole("combobox", { name: "Wallet" })); const options = await screen.findAllByRole("option"); expect(options).toHaveLength(3); expect(screen.getByRole("option", { name: /Product.*Balance 100(\.00)?\b/ })).toBeInTheDocument(); expect(screen.getByRole("option", { name: /Acorns.*Balance 50(\.00)?\b/ })).toBeInTheDocument(); expect(screen.getByRole("option", { name: /Apples.*Balance 7(\.00)?\b/ })).toBeInTheDocument(); expect(screen.queryByRole("option", { name: /Gift/ })).not.toBeInTheDocument(); }); it("Review with a missing field shows the required text and does not post", async () => { const user = userEvent.setup(); render(ui()); await lookup(user); await user.click(screen.getByRole("button", { name: "Review" })); expect(await screen.findByText("Fill in every field.")).toBeInTheDocument(); expect(svc.adjustWallet).not.toHaveBeenCalled(); }); it.each(["0", "1.234"])("amount %s shows the invalid-amount text and does not post", async amount => { const user = userEvent.setup(); render(ui()); await fill(user, { amount }); await user.click(screen.getByRole("button", { name: "Review" })); expect( await screen.findByText("Enter an amount greater than 0 with at most 2 decimals."), ).toBeInTheDocument(); expect(svc.adjustWallet).not.toHaveBeenCalled(); }); it("confirms an Add with member, wallet and amount interpolated", async () => { const user = userEvent.setup(); render(ui()); await fill(user, { direction: "Add" }); await user.click(screen.getByRole("button", { name: "Review" })); expect(await screen.findByText("Add 25.50 to MY10001's Product wallet?")).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Back" })).toBeInTheDocument(); expect(svc.adjustWallet).not.toHaveBeenCalled(); }); it("confirms a Deduct with member, wallet and amount interpolated", async () => { const user = userEvent.setup(); render(ui()); await fill(user, { direction: "Deduct" }); await user.click(screen.getByRole("button", { name: "Review" })); expect(await screen.findByText("Deduct 25.50 from MY10001's Product wallet?")).toBeInTheDocument(); }); it("submits IN with a trimmed reason and the opening's refNo, and never sends doneBy", async () => { const user = userEvent.setup(); render(ui()); await fill(user, { direction: "Add" }); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); await waitFor(() => expect(svc.adjustWallet).toHaveBeenCalledTimes(1)); expect(svc.adjustWallet).toHaveBeenCalledWith({ memberCode: "MY10001", walletCode: "PRODUCT", direction: "IN", amount: 25.5, reason: "Goodwill", refNo: "uuid-1", }); }); it("submits OUT for Deduct", async () => { const user = userEvent.setup(); render(ui()); await fill(user, { direction: "Deduct" }); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); await waitFor(() => expect(svc.adjustWallet).toHaveBeenCalledWith(expect.objectContaining({ direction: "OUT" })), ); }); it("disables submit while saving", async () => { let resolve!: (v: unknown) => void; svc.adjustWallet.mockReturnValue(new Promise(r => (resolve = r))); const user = userEvent.setup(); render(ui()); await fill(user); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); expect(screen.getByRole("button", { name: /Confirm adjustment/ })).toBeDisabled(); resolve({ refNo: "x", trxnType: "AJI", amount: 25.5, balance: 1 }); await waitFor(() => expect(onSaved).toHaveBeenCalled()); }); it("a 409 shows the insufficient text and keeps the dialog open", async () => { svc.adjustWallet.mockRejectedValue(new Error("WalletAdjInsufficient")); const user = userEvent.setup(); render(ui()); await fill(user, { direction: "Deduct" }); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); expect(await screen.findByText("Not enough balance in this wallet.")).toBeInTheDocument(); expect(onOpenChange).not.toHaveBeenCalledWith(false); expect(onSaved).not.toHaveBeenCalled(); }); it("another error shows the failed text, and a retry reuses the same refNo", async () => { svc.adjustWallet.mockRejectedValueOnce(new Error("boom")); const user = userEvent.setup(); render(ui()); await fill(user); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); expect(await screen.findByText("Could not save the adjustment.")).toBeInTheDocument(); await waitFor(() => expect(screen.getByRole("button", { name: "Confirm adjustment" })).toBeEnabled()); await user.click(screen.getByRole("button", { name: "Confirm adjustment" })); await waitFor(() => expect(svc.adjustWallet).toHaveBeenCalledTimes(2)); const [first, second] = svc.adjustWallet.mock.calls.map(c => c[0].refNo); expect(first).toBe("uuid-1"); expect(second).toBe(first); }); it("a reopened dialog gets a new refNo and cleared fields", async () => { svc.adjustWallet.mockRejectedValueOnce(new Error("boom")); const user = userEvent.setup(); const { rerender } = render(ui(true)); await fill(user); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); await screen.findByText("Could not save the adjustment."); rerender(ui(false)); rerender(ui(true)); expect(screen.getByLabelText("Member code")).toHaveValue(""); await fill(user); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); await waitFor(() => expect(svc.adjustWallet).toHaveBeenCalledTimes(2)); expect(svc.adjustWallet.mock.calls[0][0].refNo).toBe("uuid-1"); expect(svc.adjustWallet.mock.calls[1][0].refNo).not.toBe("uuid-1"); }); it("on success closes the dialog and reports saved", async () => { const user = userEvent.setup(); render(ui()); await fill(user); await user.click(screen.getByRole("button", { name: "Review" })); await user.click(await screen.findByRole("button", { name: "Confirm adjustment" })); await waitFor(() => expect(onSaved).toHaveBeenCalledTimes(1)); expect(onOpenChange).toHaveBeenCalledWith(false); }); it("ignores a stale lookup that resolves after a newer one (out of order)", async () => { let resolveA!: (v: unknown) => void; let resolveB!: (v: unknown) => void; svc.getMemberWalletBalances.mockImplementation((code: string) => new Promise(r => { if (code === "MY1") resolveA = r; else resolveB = r; }), ); const user = userEvent.setup(); render(ui()); const input = screen.getByLabelText("Member code"); await user.type(input, "MY1"); await user.tab(); await user.clear(input); await user.type(input, "MY2"); await user.tab(); await waitFor(() => expect(svc.getMemberWalletBalances).toHaveBeenCalledTimes(2)); resolveB([{ walletTypeCode: "ACORN", balance: 50, enabled: "Y" }]); await screen.findByRole("combobox", { name: "Wallet" }); resolveA([{ walletTypeCode: "PRODUCT", balance: 100, enabled: "Y" }]); await new Promise(r => setTimeout(r, 20)); await user.click(screen.getByRole("combobox", { name: "Wallet" })); const options = await screen.findAllByRole("option"); expect(options).toHaveLength(1); expect(options[0]).toHaveTextContent(/Acorns/); }); });