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
92 lines
5.0 KiB
TypeScript
92 lines
5.0 KiB
TypeScript
import { describe, it, expect, vi } from "vitest";
|
|
import { render, screen, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import type { ProductListRow } from "@/app/lib/types";
|
|
import { ProductSelectDialog } from "./ProductSelectDialog";
|
|
|
|
// promo-product-picker-and-hints: choose a campaign's eligible products in one dialog, by category.
|
|
|
|
const row = (productCode: string, productName: string, productCategory?: string, productStatus: "Y" | "N" = "Y") =>
|
|
({ id: productCode, productCode, productName, productCategory, productStatus }) as ProductListRow;
|
|
const products = [
|
|
row("STR-XL-001", "Baby Stroller XL", "Stroller"),
|
|
row("STR-MOD-002", "Modern Stroller 360", "Stroller"),
|
|
row("CS-001", "Cabrio Car Seat", "Car seat"),
|
|
row("HAM-001", "Hamilton Backpack", undefined, "N"),
|
|
];
|
|
|
|
function open(selected: string[] = [], onDone = vi.fn(), onClose = vi.fn()) {
|
|
render(<ProductSelectDialog products={products} selected={selected} onDone={onDone} onClose={onClose} />);
|
|
return { onDone, onClose, dialog: screen.getByRole("dialog", { name: "Select eligible products" }) };
|
|
}
|
|
|
|
describe("ProductSelectDialog", () => {
|
|
it("lists every product under its category with a chosen count", () => {
|
|
const { dialog } = open(["CS-001"]);
|
|
const stroller = within(dialog).getByRole("group", { name: "Stroller" });
|
|
expect(within(stroller).getByRole("checkbox", { name: "Baby Stroller XL" })).not.toBeChecked();
|
|
expect(within(stroller).getByText("STR-MOD-002")).toBeInTheDocument();
|
|
expect(within(stroller).getByText("0/2")).toBeInTheDocument();
|
|
const carSeat = within(dialog).getByRole("group", { name: "Car seat" });
|
|
expect(within(carSeat).getByRole("checkbox", { name: "Cabrio Car Seat" })).toBeChecked();
|
|
expect(within(carSeat).getByText("1/1")).toBeInTheDocument();
|
|
// A product without a category falls under "Other"; an inactive one is tagged.
|
|
const other = within(dialog).getByRole("group", { name: "Other" });
|
|
expect(within(other).getByText("Inactive")).toBeInTheDocument();
|
|
expect(within(dialog).getByText("1 selected")).toBeInTheDocument();
|
|
});
|
|
|
|
it("selects and clears a whole category from its header", async () => {
|
|
const user = userEvent.setup();
|
|
const { dialog } = open(["STR-XL-001"]);
|
|
const header = within(dialog).getByRole("checkbox", { name: "Select all Stroller" });
|
|
expect(header).toHaveAttribute("data-state", "indeterminate");
|
|
await user.click(header);
|
|
expect(within(dialog).getByRole("checkbox", { name: "Modern Stroller 360" })).toBeChecked();
|
|
expect(within(dialog).getByText("2 selected")).toBeInTheDocument();
|
|
await user.click(header);
|
|
expect(within(dialog).getByRole("checkbox", { name: "Baby Stroller XL" })).not.toBeChecked();
|
|
expect(within(dialog).getByText("0 selected")).toBeInTheDocument();
|
|
});
|
|
|
|
it("filters by name or code, ignoring case", async () => {
|
|
const user = userEvent.setup();
|
|
const { dialog } = open();
|
|
await user.type(within(dialog).getByPlaceholderText("Search name or code"), "str-mod");
|
|
expect(within(dialog).getByRole("checkbox", { name: "Modern Stroller 360" })).toBeInTheDocument();
|
|
expect(within(dialog).queryByRole("checkbox", { name: "Baby Stroller XL" })).not.toBeInTheDocument();
|
|
expect(within(dialog).queryByRole("group", { name: "Car seat" })).not.toBeInTheDocument();
|
|
await user.clear(within(dialog).getByPlaceholderText("Search name or code"));
|
|
await user.type(within(dialog).getByPlaceholderText("Search name or code"), "cabrio");
|
|
expect(within(dialog).getByRole("checkbox", { name: "Cabrio Car Seat" })).toBeInTheDocument();
|
|
await user.type(within(dialog).getByPlaceholderText("Search name or code"), "zzz");
|
|
expect(within(dialog).getByText("No products match.")).toBeInTheDocument();
|
|
});
|
|
|
|
it("returns the ticked codes in catalogue order on Done", async () => {
|
|
const user = userEvent.setup();
|
|
const { dialog, onDone } = open();
|
|
await user.click(within(dialog).getByRole("checkbox", { name: "Cabrio Car Seat" }));
|
|
await user.click(within(dialog).getByRole("checkbox", { name: "Baby Stroller XL" }));
|
|
await user.click(within(dialog).getByRole("button", { name: "Done" }));
|
|
expect(onDone).toHaveBeenCalledWith(["STR-XL-001", "CS-001"]);
|
|
});
|
|
|
|
it("keeps a chosen code that is not in the catalogue", async () => {
|
|
const user = userEvent.setup();
|
|
const { dialog, onDone } = open(["GONE-1"]);
|
|
expect(within(dialog).getByText("1 selected")).toBeInTheDocument();
|
|
await user.click(within(dialog).getByRole("button", { name: "Done" }));
|
|
expect(onDone).toHaveBeenCalledWith(["GONE-1"]);
|
|
});
|
|
|
|
it("discards changes on Cancel", async () => {
|
|
const user = userEvent.setup();
|
|
const { dialog, onDone, onClose } = open();
|
|
await user.click(within(dialog).getByRole("checkbox", { name: "Cabrio Car Seat" }));
|
|
await user.click(within(dialog).getByRole("button", { name: "Cancel" }));
|
|
expect(onClose).toHaveBeenCalled();
|
|
expect(onDone).not.toHaveBeenCalled();
|
|
});
|
|
});
|