import { describe, it, expect, vi, beforeEach } from "vitest"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { ApiError } from "@/app/lib/clientApi"; import type { GiftCardAmount, PosTender } from "@/app/lib/types"; import { StorefrontOriginProvider } from "@/app/lib/storefront"; import { SellGiftCardDialog } from "./SellGiftCardDialog"; // add-gift-card-counter: the POS sells a gift card from the amount list for cash, card or QR, then shows // the code once with a printable slip. const TENDERS: PosTender[] = [ { id: "1", label: "Cash", paymentType: "PC", wallet: false }, { id: "2", label: "Member Wallet", paymentType: "DWP", wallet: true }, ]; const AMOUNTS: GiftCardAmount[] = [ { id: 1, amount: 50, active: "Y", sortOrder: 1 }, { id: 2, amount: 100, active: "Y", sortOrder: 2 }, { id: 3, amount: 1, active: "N", sortOrder: 0 }, ]; const SOLD = { saleUuid: "s1", code: "ABCD1234EFGH5678", last4: "5678", amount: 50, expiresAt: "2029-10-09" }; const sell = vi.fn(); const onOpenChange = vi.fn(); function renderDialog() { render(); } async function choose() { await userEvent.click(screen.getByRole("radio", { name: /RM\s?50\.00/ })); await userEvent.click(screen.getByRole("radio", { name: /cash/i })); } beforeEach(() => { sell.mockReset(); onOpenChange.mockReset(); }); describe("SellGiftCardDialog", () => { it("offers only active amounts and non-wallet tenders", () => { renderDialog(); expect(screen.getByRole("radio", { name: /RM\s?50\.00/ })).toBeInTheDocument(); expect(screen.getByRole("radio", { name: /RM\s?100\.00/ })).toBeInTheDocument(); expect(screen.queryByRole("radio", { name: /RM\s?1\.00/ })).not.toBeInTheDocument(); expect(screen.queryByRole("radio", { name: /member wallet/i })).not.toBeInTheDocument(); }); it("shows every missing choice at once and sends nothing", async () => { renderDialog(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); expect(screen.getByText(/choose an amount/i)).toBeInTheDocument(); expect(screen.getByText(/choose how it is paid/i)).toBeInTheDocument(); expect(sell).not.toHaveBeenCalled(); }); it("refuses a bad recipient email", async () => { renderDialog(); await choose(); await userEvent.type(screen.getByLabelText(/recipient email/i), "aina@"); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); expect(screen.getByText(/enter a valid email/i)).toBeInTheDocument(); expect(sell).not.toHaveBeenCalled(); }); it("sends the choices with trimmed optional fields", async () => { sell.mockResolvedValue(SOLD); renderDialog(); await choose(); await userEvent.type(screen.getByLabelText(/reference/i), " R-9 "); await userEvent.type(screen.getByLabelText(/recipient name/i), "Aina"); await userEvent.type(screen.getByLabelText(/recipient email/i), "aina@example.com"); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); expect(sell).toHaveBeenCalledWith( expect.objectContaining({ amount: 50, paymentMethodId: "1", reference: "R-9", recipientName: "Aina", recipientEmail: "aina@example.com", clientKey: expect.any(String), }), ); }); it("keeps the client key on retry and then shows the code once with Print slip", async () => { sell.mockRejectedValueOnce(new ApiError("Try again", 503)).mockResolvedValueOnce(SOLD); renderDialog(); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); expect(await screen.findByRole("alert")).toHaveTextContent(/try again/i); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); expect(await screen.findByText("ABCD-1234-EFGH-5678")).toBeInTheDocument(); expect(sell.mock.calls[0][0].clientKey).toBe(sell.mock.calls[1][0].clientKey); expect(screen.getByRole("button", { name: /print slip/i })).toBeInTheDocument(); }); it("prints a slip with the code, amount, expiry and balance-check address", async () => { sell.mockResolvedValue(SOLD); const write = vi.fn(); const win = { document: { write, close: vi.fn() }, focus: vi.fn(), print: vi.fn(), close: vi.fn() }; const open = vi.spyOn(window, "open").mockReturnValue(win as unknown as Window); renderDialog(); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); await userEvent.click(await screen.findByRole("button", { name: /print slip/i })); expect(open).toHaveBeenCalled(); const html = write.mock.calls.map((c) => c[0]).join(""); expect(html).toContain("ABCD-1234-EFGH-5678"); expect(html).toMatch(/RM\s?50\.00/); expect(html).toContain("/gift-cards/balance"); expect(win.print).toHaveBeenCalled(); open.mockRestore(); }); it("prints the balance-check address from the runtime portal origin", async () => { sell.mockResolvedValue(SOLD); const write = vi.fn(); const win = { document: { write, close: vi.fn() }, focus: vi.fn(), print: vi.fn(), close: vi.fn() }; const open = vi.spyOn(window, "open").mockReturnValue(win as unknown as Window); render( , ); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); await userEvent.click(await screen.findByRole("button", { name: /print slip/i })); const html = write.mock.calls.map((c) => c[0]).join(""); expect(html).toContain("https://user.babyboo.my/gift-cards/balance"); open.mockRestore(); }); it("falls back to the dev portal origin when none is provided", async () => { sell.mockResolvedValue(SOLD); const write = vi.fn(); const win = { document: { write, close: vi.fn() }, focus: vi.fn(), print: vi.fn(), close: vi.fn() }; const open = vi.spyOn(window, "open").mockReturnValue(win as unknown as Window); renderDialog(); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); await userEvent.click(await screen.findByRole("button", { name: /print slip/i })); const html = write.mock.calls.map((c) => c[0]).join(""); expect(html).toContain("https://dev-user.babyboo.my/gift-cards/balance"); open.mockRestore(); }); it("Done hides the code and closes, and a new sale gets a new client key", async () => { sell.mockResolvedValue(SOLD); renderDialog(); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); await userEvent.click(await screen.findByRole("button", { name: /done/i })); expect(screen.queryByText("ABCD-1234-EFGH-5678")).not.toBeInTheDocument(); expect(onOpenChange).toHaveBeenCalledWith(false); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); expect(sell.mock.calls[1][0].clientKey).not.toBe(sell.mock.calls[0][0].clientKey); }); it("closing with Escape also forgets the code", async () => { sell.mockResolvedValue(SOLD); renderDialog(); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); expect(await screen.findByText("ABCD-1234-EFGH-5678")).toBeInTheDocument(); await userEvent.keyboard("{Escape}"); expect(onOpenChange).toHaveBeenCalledWith(false); expect(screen.queryByText("ABCD-1234-EFGH-5678")).not.toBeInTheDocument(); }); it("says when the amounts could not be loaded", () => { render( , ); expect(screen.getByRole("alert")).toHaveTextContent(/could not be loaded/); }); it("says so when the slip window is blocked", async () => { sell.mockResolvedValue(SOLD); const open = vi.spyOn(window, "open").mockReturnValue(null); renderDialog(); await choose(); await userEvent.click(screen.getByRole("button", { name: /^sell/i })); await userEvent.click(await screen.findByRole("button", { name: /print slip/i })); expect(await screen.findByRole("alert")).toHaveTextContent(/allow pop-ups/i); open.mockRestore(); }); });