Files
co-lite/solvway-admin-portal/app/pos/_components/SellGiftCardDialog.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

190 lines
8.3 KiB
TypeScript

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(<SellGiftCardDialog open onOpenChange={onOpenChange} tenders={TENDERS} amounts={AMOUNTS} sell={sell} />);
}
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(
<StorefrontOriginProvider origin="https://user.babyboo.my">
<SellGiftCardDialog open onOpenChange={onOpenChange} tenders={TENDERS} amounts={AMOUNTS} sell={sell} />
</StorefrontOriginProvider>,
);
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(
<SellGiftCardDialog open onOpenChange={onOpenChange} tenders={TENDERS} amounts={[]} amountsError="Gift card amounts could not be loaded." sell={sell} />,
);
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();
});
});