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"; vi.mock("@/app/lib/services/posGiftCards", async (orig) => ({ ...(await orig()), checkPosGiftCard: vi.fn(), })); import { checkPosGiftCard } from "@/app/lib/services/posGiftCards"; import { GiftCardCodesField } from "./GiftCardCodesField"; // add-gift-card-counter: the cashier types up to 3 card codes; each is checked, shown masked with what // it pays, and the rest is left to the chosen cash, card or QR method. beforeEach(() => { vi.mocked(checkPosGiftCard).mockReset(); }); describe("GiftCardCodesField", () => { it("applies a usable card with its normalised code", async () => { vi.mocked(checkPosGiftCard).mockResolvedValue({ balance: 50, expiresAt: "2029-10-09", last4: "5678" }); const onChange = vi.fn(); render(); await userEvent.type(screen.getByLabelText(/gift card code/i), "abcd-1234 efgh-5678"); await userEvent.click(screen.getByRole("button", { name: /apply/i })); expect(checkPosGiftCard).toHaveBeenCalledWith("ABCD1234EFGH5678"); expect(onChange).toHaveBeenCalledWith([{ code: "ABCD1234EFGH5678", last4: "5678", balance: 50 }]); }); it("shows the refusal and applies nothing", async () => { vi.mocked(checkPosGiftCard).mockRejectedValue(new ApiError("This gift card has expired", 409)); const onChange = vi.fn(); render(); await userEvent.type(screen.getByLabelText(/gift card code/i), "ABCD1234EFGH5678"); await userEvent.click(screen.getByRole("button", { name: /apply/i })); expect(await screen.findByRole("alert")).toHaveTextContent(/expired/i); expect(onChange).not.toHaveBeenCalled(); }); it("refuses a code that is already applied without checking it", async () => { const onChange = vi.fn(); render( , ); await userEvent.type(screen.getByLabelText(/gift card code/i), "abcd-1234-efgh-5678"); await userEvent.click(screen.getByRole("button", { name: /apply/i })); expect(await screen.findByRole("alert")).toHaveTextContent(/already applied/i); expect(checkPosGiftCard).not.toHaveBeenCalled(); expect(onChange).not.toHaveBeenCalled(); }); it("lists applied cards masked with what each pays and what is left, and removes one", async () => { const onChange = vi.fn(); render( , ); expect(screen.getByText(/•••• 5678/)).toBeInTheDocument(); expect(screen.getByText(/RM\s?50\.00/)).toBeInTheDocument(); expect(screen.getByText(/left to pay/i).closest("p, div")).toHaveTextContent(/RM\s?70\.00/); expect(screen.queryByText(/ABCD1234EFGH5678|ABCD-1234/)).not.toBeInTheDocument(); await userEvent.click(screen.getByRole("button", { name: /remove/i })); expect(onChange).toHaveBeenCalledWith([]); }); it("allows no fourth card", () => { const three = ["A", "B", "C"].map((x, i) => ({ code: x.repeat(16), last4: x.repeat(4), balance: 1 + i })); render(); expect(screen.getByRole("button", { name: /apply/i })).toBeDisabled(); }); it("shows each applied card's balance next to what it pays", () => { render( , ); expect(screen.getByText(/•••• 5678/).closest("div")).toHaveTextContent(/RM\s?30\.00.*balance RM\s?80\.00/); }); });