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
95 lines
4.1 KiB
TypeScript
95 lines
4.1 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";
|
|
|
|
vi.mock("@/app/lib/services/posGiftCards", async (orig) => ({
|
|
...(await orig<typeof import("@/app/lib/services/posGiftCards")>()),
|
|
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(<GiftCardCodesField cards={[]} total={120} onChange={onChange} disabled={false} />);
|
|
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(<GiftCardCodesField cards={[]} total={120} onChange={onChange} disabled={false} />);
|
|
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(
|
|
<GiftCardCodesField
|
|
cards={[{ code: "ABCD1234EFGH5678", last4: "5678", balance: 50 }]}
|
|
total={120}
|
|
onChange={onChange}
|
|
disabled={false}
|
|
/>,
|
|
);
|
|
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(
|
|
<GiftCardCodesField
|
|
cards={[{ code: "ABCD1234EFGH5678", last4: "5678", balance: 50 }]}
|
|
total={120}
|
|
onChange={onChange}
|
|
disabled={false}
|
|
/>,
|
|
);
|
|
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(<GiftCardCodesField cards={three} total={120} onChange={vi.fn()} disabled={false} />);
|
|
expect(screen.getByRole("button", { name: /apply/i })).toBeDisabled();
|
|
});
|
|
|
|
it("shows each applied card's balance next to what it pays", () => {
|
|
render(
|
|
<GiftCardCodesField
|
|
cards={[{ code: "ABCD1234EFGH5678", last4: "5678", balance: 80 }]}
|
|
total={30}
|
|
onChange={vi.fn()}
|
|
disabled={false}
|
|
/>,
|
|
);
|
|
expect(screen.getByText(/•••• 5678/).closest("div")).toHaveTextContent(/RM\s?30\.00.*balance RM\s?80\.00/);
|
|
});
|
|
});
|