Files
co-lite/solvway-admin-portal/app/pos/_components/WalletAmountLeg.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

168 lines
6.4 KiB
TypeScript

// WalletAmountLeg contract (free-wallet-split Task 5), fixed here for the implementer. Named export
// WalletAmountLeg from "./WalletAmountLeg"; the admin twin of the customer portal's WalletAmountCard.
// props: { option: WalletOption; value: number; cap: number; yellowTotal?: number;
// error?: string | null; onChange: (amount: number) => void }
// - WalletOption = { walletTypeCode: string; balance: number; max: number } (exported from
// "@/app/lib/types" as PosWalletOption)
// - wallet name = t(walletLabelCode(code)): Product / Acorns / Apples / Gift card
// - cap is the most this wallet may take now (walletCap); typed and slider values clamp to it
// - error is a message CODE; the leg shows t(error, { wallet: name }) in role="alert"
// - yellowTotal (RM of yellow-label items) is shown by the ACORN leg only
// Copy codes reused from the customer portal and to be added to admin defaultMessages:
// WalletBalanceShort "Balance {amount}", WalletUseMax "Use max", WalletClear "Clear",
// WalletAmountLabel "{wallet} amount", WalletSliderLabel "{wallet} slider",
// AcornsYellowOnlyNote "Acorns pay for yellow-label items only. This order has RM {amount} of yellow-label items.",
// WalletAmountOverBalance, AcornsOverYellowLabel, WalletAmountsOverDue, WalletAmountInvalid, WalletLegsDisabled.
import { fireEvent, render, screen } from "@testing-library/react";
import { useState } from "react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { WalletAmountLeg } from "./WalletAmountLeg";
const onChange = vi.fn();
beforeEach(() => {
onChange.mockReset();
});
function setup(props: Partial<Parameters<typeof WalletAmountLeg>[0]> = {}) {
render(
<WalletAmountLeg
option={{ walletTypeCode: "PRODUCT", balance: 500, max: 200 }}
value={0}
cap={150}
onChange={onChange}
{...props}
/>,
);
}
describe("WalletAmountLeg", () => {
it.each([
["PRODUCT", "Product"],
["ACORN", "Acorns"],
["APPLE", "Apples"],
["GIFT", "Gift card"],
])("names the %s wallet and shows its balance", (code, name) => {
setup({ option: { walletTypeCode: code, balance: 500, max: 200 } });
expect(screen.getByRole("group", { name })).toBeInTheDocument();
expect(screen.getByText(/Balance.*500/)).toBeInTheDocument();
});
it("clamps a typed amount above the cap to the cap", () => {
setup();
fireEvent.change(screen.getByLabelText("Product amount"), { target: { value: "999" } });
expect(onChange).toHaveBeenLastCalledWith(150);
});
it("passes an amount within the cap through unchanged", () => {
setup();
fireEvent.change(screen.getByLabelText("Product amount"), { target: { value: "42.5" } });
expect(onChange).toHaveBeenLastCalledWith(42.5);
});
it("has a slider capped at the cap in 0.01 steps that reports its value", () => {
setup();
const slider = screen.getByLabelText("Product slider");
expect(slider).toHaveAttribute("type", "range");
expect(slider).toHaveAttribute("max", "150");
expect(slider).toHaveAttribute("step", "0.01");
fireEvent.change(slider, { target: { value: "75" } });
expect(onChange).toHaveBeenLastCalledWith(75);
});
it("shows the current value in the amount box and blank when zero", () => {
setup({ value: 30 });
expect(screen.getByLabelText("Product amount")).toHaveValue(30);
});
it("Use max sets the cap and Clear sets zero", async () => {
const user = userEvent.setup();
setup({ value: 30 });
await user.click(screen.getByRole("button", { name: "Use max" }));
expect(onChange).toHaveBeenLastCalledWith(150);
await user.click(screen.getByRole("button", { name: "Clear" }));
expect(onChange).toHaveBeenLastCalledWith(0);
});
it("shows the yellow-label note on the Acorns leg only", () => {
setup({ option: { walletTypeCode: "ACORN", balance: 80, max: 80 }, yellowTotal: 35 });
expect(
screen.getByText("Acorns pay for yellow-label items only. This order has RM 35.00 of yellow-label items."),
).toBeInTheDocument();
});
it("does not show the yellow-label note on other wallets", () => {
setup({ yellowTotal: 35 });
expect(screen.queryByText(/yellow-label/)).not.toBeInTheDocument();
});
it("shows no alert without an error", () => {
setup();
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
});
it.each([
["WalletAmountOverBalance", "This is more than the member's Product balance."],
["AcornsOverYellowLabel", "Acorns can only pay for yellow-label items."],
["WalletAmountsOverDue", "Wallet amounts add up to more than the amount due."],
["WalletAmountInvalid", "Enter an amount of 0 or more, up to 2 decimal places."],
["WalletLegsDisabled", "Wallet payment needs the member's PIN."],
])("shows the %s message in an alert on the leg", (code, text) => {
setup({ error: code });
expect(screen.getByRole("alert")).toHaveTextContent(text);
});
});
describe("WalletAmountLeg typing", () => {
it("keeps the typed decimals while a controlling parent re-renders", () => {
const seen = vi.fn();
function Parent() {
const [v, setV] = useState(0);
return (
<WalletAmountLeg
option={{ walletTypeCode: "PRODUCT", balance: 500, max: 200 }}
value={v}
cap={150}
onChange={(n: number) => {
seen(n);
setV(n);
}}
/>
);
}
render(<Parent />);
const input = screen.getByLabelText("Product amount") as HTMLInputElement;
for (const raw of ["1", "1.0", "1.05"]) {
fireEvent.change(input, { target: { value: raw } });
expect(input.value).toBe(raw);
}
expect(seen).toHaveBeenLastCalledWith(1.05);
});
it("keeps a pasted trailing zero while a controlling parent re-renders", () => {
const seen = vi.fn();
function Parent() {
const [v, setV] = useState(0);
return (
<WalletAmountLeg
option={{ walletTypeCode: "PRODUCT", balance: 500, max: 200 }}
value={v}
cap={150}
onChange={(n: number) => {
seen(n);
setV(n);
}}
/>
);
}
render(<Parent />);
const input = screen.getByLabelText("Product amount") as HTMLInputElement;
for (const raw of ["1.50"]) {
fireEvent.change(input, { target: { value: raw } });
expect(input.value).toBe(raw);
}
expect(seen).toHaveBeenLastCalledWith(1.5);
});
});