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
145 lines
5.9 KiB
TypeScript
145 lines
5.9 KiB
TypeScript
import { describe, it, expect, vi } from "vitest";
|
||
import { render, screen, fireEvent, within } from "@testing-library/react";
|
||
import { PaymentDialog } from "./PaymentDialog";
|
||
import type { PosTender } from "@/app/lib/types";
|
||
import type { PosLine } from "@/app/pos/shared";
|
||
|
||
// redesign-pos-sale-flow (BABYBOO-38): payment happens in a dialog — the sale summary on one side, the
|
||
// amount due, one card per configured tender plus Split, and the chosen method's content on the other.
|
||
|
||
const TENDERS: PosTender[] = [
|
||
{ id: "1", label: "Cash", paymentType: "PC", wallet: false },
|
||
{ id: "2", label: "Member Wallet", paymentType: "DWP", wallet: true },
|
||
];
|
||
const LINES: PosLine[] = [
|
||
{ key: 1, pPricingId: "p1", variationId: "v1", label: "Product A", unitPrice: 10, qty: 2 },
|
||
{ key: 2, pPricingId: "p2", variationId: "v2", label: "Product B", unitPrice: 5, qty: 1 },
|
||
];
|
||
|
||
function renderDialog(props: Partial<React.ComponentProps<typeof PaymentDialog>> = {}) {
|
||
const handlers = { onOpenChange: vi.fn(), onSelect: vi.fn(), onComplete: vi.fn() };
|
||
render(
|
||
<PaymentDialog
|
||
open
|
||
locked={false}
|
||
customerName="Nur Aisyah"
|
||
customerDetail="MY3619941"
|
||
lines={LINES}
|
||
total={25}
|
||
tenders={TENDERS}
|
||
selection={{ kind: "TENDER", id: "1" }}
|
||
isMember
|
||
error={null}
|
||
showComplete
|
||
completeDisabled={false}
|
||
placing={false}
|
||
{...handlers}
|
||
{...props}
|
||
>
|
||
<p>method content</p>
|
||
</PaymentDialog>,
|
||
);
|
||
return handlers;
|
||
}
|
||
|
||
describe("PaymentDialog", () => {
|
||
it("summarises the customer, the lines and the total", () => {
|
||
renderDialog();
|
||
const dialog = screen.getByRole("dialog", { name: "Payment" });
|
||
const summary = within(dialog).getByTestId("payment-summary");
|
||
expect(summary).toHaveTextContent("Nur Aisyah");
|
||
expect(summary).toHaveTextContent("MY3619941");
|
||
expect(summary).toHaveTextContent("2× Product A");
|
||
expect(summary).toHaveTextContent("RM 20.00");
|
||
expect(within(summary).getByTestId("payment-total")).toHaveTextContent("RM 25.00");
|
||
});
|
||
|
||
it("shows the amount due", () => {
|
||
renderDialog();
|
||
expect(screen.getByTestId("amount-due")).toHaveTextContent("RM 25.00");
|
||
});
|
||
|
||
it("offers one card per tender plus Split, with the selection checked", () => {
|
||
renderDialog();
|
||
const group = screen.getByRole("radiogroup", { name: "Payment method" });
|
||
const cards = within(group).getAllByRole("radio");
|
||
expect(cards.map((c) => c.textContent)).toEqual([
|
||
expect.stringContaining("Cash"),
|
||
expect.stringContaining("Member Wallet"),
|
||
expect.stringContaining("Split"),
|
||
]);
|
||
expect(within(group).getByRole("radio", { name: /Cash/ })).toHaveAttribute("aria-checked", "true");
|
||
expect(within(group).getByRole("radio", { name: /Split/ })).toHaveAttribute("aria-checked", "false");
|
||
});
|
||
|
||
it("selecting a card reports it", () => {
|
||
const h = renderDialog();
|
||
fireEvent.click(screen.getByRole("radio", { name: /Member Wallet/ }));
|
||
expect(h.onSelect).toHaveBeenCalledWith({ kind: "TENDER", id: "2" });
|
||
fireEvent.click(screen.getByRole("radio", { name: /Split/ }));
|
||
expect(h.onSelect).toHaveBeenCalledWith({ kind: "SPLIT" });
|
||
});
|
||
|
||
it("disables the wallet card for a walk-in, saying Members only", () => {
|
||
renderDialog({ isMember: false });
|
||
const wallet = screen.getByRole("radio", { name: /Member Wallet/ });
|
||
expect(wallet).toBeDisabled();
|
||
expect(wallet).toHaveTextContent("Members only");
|
||
expect(screen.getByRole("radio", { name: /Split/ })).toBeEnabled();
|
||
});
|
||
|
||
it("renders the chosen method's content", () => {
|
||
renderDialog();
|
||
expect(screen.getByText("method content")).toBeInTheDocument();
|
||
});
|
||
|
||
it("shows a sale error inside the dialog", () => {
|
||
renderDialog({ error: "Incorrect PIN." });
|
||
expect(screen.getByRole("alert")).toHaveTextContent("Incorrect PIN.");
|
||
});
|
||
|
||
it("Complete sale places the sale, and is disabled or busy as told", () => {
|
||
const h = renderDialog();
|
||
fireEvent.click(screen.getByRole("button", { name: "Complete sale" }));
|
||
expect(h.onComplete).toHaveBeenCalled();
|
||
});
|
||
|
||
it("Complete sale is disabled when the sale is not ready", () => {
|
||
renderDialog({ completeDisabled: true });
|
||
expect(screen.getByRole("button", { name: "Complete sale" })).toBeDisabled();
|
||
});
|
||
|
||
it("shows progress while placing", () => {
|
||
renderDialog({ placing: true });
|
||
expect(screen.getByRole("button", { name: "Completing sale…" })).toBeDisabled();
|
||
});
|
||
|
||
it("hides Complete sale when the method brings its own (Split)", () => {
|
||
renderDialog({ showComplete: false, selection: { kind: "SPLIT" } });
|
||
expect(screen.queryByRole("button", { name: "Complete sale" })).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("Back to sale closes the dialog", () => {
|
||
const h = renderDialog();
|
||
fireEvent.click(screen.getByRole("button", { name: /Back to sale/ }));
|
||
expect(h.onOpenChange).toHaveBeenCalledWith(false);
|
||
});
|
||
|
||
it("cannot be closed or switched while a split payment is pending", () => {
|
||
const h = renderDialog({ locked: true, selection: { kind: "SPLIT" }, showComplete: false });
|
||
expect(screen.getByRole("button", { name: /Back to sale/ })).toBeDisabled();
|
||
expect(screen.queryByRole("button", { name: "Close" })).not.toBeInTheDocument();
|
||
fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
|
||
expect(h.onOpenChange).not.toHaveBeenCalledWith(false);
|
||
expect(screen.getByRole("radio", { name: /Cash/ })).toBeDisabled();
|
||
});
|
||
|
||
it("disables wallet tenders and Split while a gift card is applied (add-gift-card-counter)", () => {
|
||
renderDialog({ giftCardsApplied: true });
|
||
const group = screen.getByRole("radiogroup", { name: "Payment method" });
|
||
expect(within(group).getByRole("radio", { name: /Member Wallet/ })).toBeDisabled();
|
||
expect(within(group).getByRole("radio", { name: /Split/ })).toBeDisabled();
|
||
expect(within(group).getByRole("radio", { name: /Cash/ })).toBeEnabled();
|
||
});
|
||
});
|