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
119 lines
4.8 KiB
TypeScript
119 lines
4.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent } from "@testing-library/react";
|
|
import type { MemberVoucherRow } from "@/app/lib/types";
|
|
import VoucherModal from "./VoucherModal";
|
|
|
|
const onClose = vi.fn();
|
|
|
|
const base: MemberVoucherRow = {
|
|
uuid: "1",
|
|
name: "Welcome gift voucher ", // the API really does send trailing spaces
|
|
description: "Spend RM50 at any booth during the event.",
|
|
currencySymbol: "RM",
|
|
amount: 50,
|
|
redeemable: true,
|
|
reason: null,
|
|
imageUrl: "https://example.com/voucher.png",
|
|
};
|
|
|
|
function open(overrides: Partial<MemberVoucherRow> = {}) {
|
|
return render(
|
|
<VoucherModal voucher={{ ...base, ...overrides }} onClose={onClose} />,
|
|
);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
onClose.mockReset();
|
|
});
|
|
|
|
describe("VoucherModal", () => {
|
|
it("renders nothing when no voucher is selected", () => {
|
|
render(<VoucherModal voucher={null} onClose={onClose} />);
|
|
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the image, full title, amount, description and a Redeem button", () => {
|
|
open();
|
|
|
|
const dialog = screen.getByRole("dialog", { name: "Welcome gift voucher" });
|
|
expect(dialog).toHaveAttribute("aria-modal", "true");
|
|
expect(screen.getByAltText("Welcome gift voucher")).toHaveAttribute("src", "https://example.com/voucher.png");
|
|
expect(screen.getByRole("heading", { name: "Welcome gift voucher" })).toBeInTheDocument();
|
|
expect(screen.getByText("RM50")).toBeInTheDocument();
|
|
expect(screen.getByText("Spend RM50 at any booth during the event.")).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: /redeem/i })).toHaveAttribute("href", "/vouchers/1");
|
|
});
|
|
|
|
it("never truncates a long title or description", () => {
|
|
const longName = "Exclusive Tastefully Star Club Member Welcome Gift Voucher For New Members";
|
|
open({ name: longName, description: "A".repeat(400) });
|
|
|
|
const heading = screen.getByRole("heading", { name: longName });
|
|
expect(heading.className).not.toContain("truncate");
|
|
expect(heading.className).toContain("break-words");
|
|
expect(screen.getByText("A".repeat(400)).className).not.toContain("line-clamp");
|
|
});
|
|
|
|
it("falls back to a ticket icon, not a broken img, when the voucher has no image", () => {
|
|
const { container } = open({ imageUrl: null });
|
|
expect(container.querySelector("img")).toBeNull();
|
|
expect(container.querySelector("svg")).not.toBeNull();
|
|
});
|
|
|
|
it("omits the description when it is empty or just repeats the title", () => {
|
|
const { rerender } = open({ description: "" });
|
|
expect(screen.queryByTestId("voucher-description")).not.toBeInTheDocument();
|
|
|
|
rerender(
|
|
<VoucherModal voucher={{ ...base, description: "welcome gift voucher" }} onClose={onClose} />,
|
|
);
|
|
expect(screen.queryByTestId("voucher-description")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("closes from the close button, the backdrop and Escape", () => {
|
|
open();
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Close" }));
|
|
fireEvent.click(screen.getByTestId("voucher-modal-backdrop"));
|
|
fireEvent.keyDown(document, { key: "Escape" });
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(3);
|
|
});
|
|
|
|
it("does not close when the panel itself is clicked", () => {
|
|
open();
|
|
fireEvent.click(screen.getByRole("dialog"));
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("carries the complete-profile message and button itself when the profile is incomplete", () => {
|
|
open({ redeemable: false, reason: "PROFILE_INCOMPLETE" });
|
|
|
|
// Same voucher design — image, title, amount — plus the prompt, in one modal (no second popup).
|
|
expect(screen.getByRole("heading", { name: "Welcome gift voucher" })).toBeInTheDocument();
|
|
expect(screen.getByText("Complete your profile to unlock rewards!")).toBeInTheDocument();
|
|
const cta = screen.getByRole("link", { name: "Complete Profile" });
|
|
expect(cta).toHaveAttribute("href", "/settings/profile");
|
|
// there is nothing to "Redeem" yet
|
|
expect(screen.queryByRole("button", { name: "Redeem" })).toBeNull();
|
|
expect(screen.queryByRole("link", { name: /redeem/i })).toBeNull();
|
|
|
|
fireEvent.click(cta);
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("shows a disabled button carrying the reason when the voucher cannot be redeemed", () => {
|
|
open({ redeemable: false, reason: "ALREADY_REDEEMED" });
|
|
|
|
const button = screen.getByRole("button", { name: "Already redeemed" });
|
|
expect(button).toBeDisabled();
|
|
expect(button.className).toContain("cursor-not-allowed");
|
|
expect(screen.queryByRole("link")).toBeNull();
|
|
});
|
|
|
|
it("keeps every digit of a Snowflake-sized uuid in the Redeem link", () => {
|
|
open({ uuid: "3305293441989062656" });
|
|
expect(screen.getByRole("link", { name: /redeem/i })).toHaveAttribute("href", "/vouchers/3305293441989062656");
|
|
});
|
|
});
|