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
226 lines
9.1 KiB
TypeScript
226 lines
9.1 KiB
TypeScript
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||
import { describe, it, expect, vi } from "vitest";
|
||
|
||
import { ProductGrid } from "./ProductGrid";
|
||
import type { PosEventProduct } from "@/app/lib/types";
|
||
|
||
// group-pos-variants: rows that share a product code are one tile; tapping it opens a dialog that adds a
|
||
// chosen variant per tap. Single-row products and exact scan matches keep their own tap-to-add tile.
|
||
|
||
const TEE_RED_S: PosEventProduct = {
|
||
pPricingId: "pr-201",
|
||
variationId: "201",
|
||
label: "Baby Tee - Red - S",
|
||
productName: "Baby Tee",
|
||
variantName: "Red - S",
|
||
unitPrice: 29.9,
|
||
productCode: "BB-TEE",
|
||
sku: "",
|
||
category: "Clothing",
|
||
imageUrl: "https://cdn.example/tee-red.png",
|
||
availability: "IN_STOCK",
|
||
scanCodes: ["BB-TEE-RS", "9555000000011"],
|
||
};
|
||
|
||
const TEE_BLUE_M: PosEventProduct = {
|
||
...TEE_RED_S,
|
||
pPricingId: "pr-202",
|
||
variationId: "202",
|
||
label: "Baby Tee - Blue - M",
|
||
variantName: "Blue - M",
|
||
unitPrice: 39.9,
|
||
imageUrl: null,
|
||
scanCodes: ["BB-TEE-BM"],
|
||
};
|
||
|
||
const TEE_GREEN_L: PosEventProduct = {
|
||
...TEE_RED_S,
|
||
pPricingId: "pr-203",
|
||
variationId: "203",
|
||
label: "Baby Tee - Green - L",
|
||
variantName: "Green - L",
|
||
availability: "SOLD_OUT",
|
||
scanCodes: ["BB-TEE-GL"],
|
||
};
|
||
|
||
const BOTTLE: PosEventProduct = {
|
||
pPricingId: "pr-301",
|
||
variationId: "301",
|
||
label: "Baby Bottle",
|
||
productName: "Baby Bottle",
|
||
unitPrice: 12.5,
|
||
productCode: "BC-001",
|
||
sku: "",
|
||
category: "Feeding",
|
||
availability: "IN_STOCK",
|
||
};
|
||
|
||
const PRODUCTS = [TEE_RED_S, BOTTLE, TEE_BLUE_M, TEE_GREEN_L];
|
||
|
||
function renderGrid(props: Partial<React.ComponentProps<typeof ProductGrid>> = {}) {
|
||
const onAdd = vi.fn();
|
||
const view = render(
|
||
<ProductGrid products={PRODUCTS} cartQtyByPricingId={{}} onAdd={onAdd} allowOrderIn={false} {...props} />,
|
||
);
|
||
return { onAdd, view };
|
||
}
|
||
|
||
const search = () => screen.getByRole("searchbox", { name: "Scan barcode, or type SKU / product name" });
|
||
const groupTile = () => screen.getByRole("button", { name: "Choose an option of Baby Tee" });
|
||
const dialog = () => screen.getByRole("dialog", { name: "Baby Tee" });
|
||
|
||
describe("ProductGrid — variants of one product share one tile", () => {
|
||
it("shows one tile for the product code, with its name, code, option count and price range", () => {
|
||
renderGrid();
|
||
const products = screen.getByRole("list", { name: "Products" });
|
||
expect(within(products).getAllByRole("listitem")).toHaveLength(2);
|
||
const tile = groupTile();
|
||
expect(tile).toHaveTextContent("Baby Tee");
|
||
expect(tile).toHaveTextContent("BB-TEE");
|
||
expect(tile).toHaveTextContent("3 options");
|
||
expect(tile).toHaveTextContent("RM 29.90 – RM 39.90");
|
||
expect(screen.queryByRole("button", { name: /^Add Baby Tee/ })).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("shows one price when every variant costs the same", () => {
|
||
renderGrid({ products: [TEE_RED_S, { ...TEE_BLUE_M, unitPrice: 29.9 }] });
|
||
expect(groupTile()).toHaveTextContent("RM 29.90");
|
||
expect(groupTile()).not.toHaveTextContent("–");
|
||
});
|
||
|
||
it("uses the first variant image that exists", () => {
|
||
renderGrid({ products: [TEE_BLUE_M, TEE_RED_S] });
|
||
expect(within(groupTile()).getByRole("img")).toHaveAttribute("src", "https://cdn.example/tee-red.png");
|
||
});
|
||
|
||
it("badges the cart quantity summed across its variants", () => {
|
||
renderGrid({ cartQtyByPricingId: { "pr-201": 2, "pr-202": 1 } });
|
||
expect(within(groupTile()).getByText("3")).toBeInTheDocument();
|
||
});
|
||
|
||
it("a single-row product still adds on one tap, without a dialog", () => {
|
||
const { onAdd } = renderGrid();
|
||
fireEvent.click(screen.getByRole("button", { name: /^Add Baby Bottle/ }));
|
||
expect(onAdd).toHaveBeenCalledWith(BOTTLE);
|
||
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("a product without a code is never grouped", () => {
|
||
const a = { ...BOTTLE, pPricingId: "pr-401", productCode: null, label: "Loose A" };
|
||
const b = { ...BOTTLE, pPricingId: "pr-402", productCode: null, label: "Loose B" };
|
||
renderGrid({ products: [a, b] });
|
||
expect(screen.getByRole("button", { name: /^Add Loose A/ })).toBeInTheDocument();
|
||
expect(screen.getByRole("button", { name: /^Add Loose B/ })).toBeInTheDocument();
|
||
});
|
||
|
||
it("falls back to the first row's label when the feed has no product name", () => {
|
||
const a = { ...TEE_RED_S, productName: undefined };
|
||
const b = { ...TEE_BLUE_M, productName: undefined };
|
||
renderGrid({ products: [a, b] });
|
||
expect(screen.getByRole("button", { name: "Choose an option of Baby Tee - Red - S" })).toBeInTheDocument();
|
||
});
|
||
});
|
||
|
||
describe("ProductGrid — variant dialog", () => {
|
||
it("lists each variant with its price and stock, and adds one unit per tap while staying open", () => {
|
||
const { onAdd } = renderGrid();
|
||
fireEvent.click(groupTile());
|
||
const d = dialog();
|
||
const red = within(d).getByRole("button", { name: /^Add Red - S/ });
|
||
expect(red).toHaveTextContent("RM 29.90");
|
||
expect(red).toHaveTextContent("In stock");
|
||
fireEvent.click(red);
|
||
fireEvent.click(red);
|
||
fireEvent.click(within(d).getByRole("button", { name: /^Add Blue - M/ }));
|
||
expect(onAdd.mock.calls).toEqual([[TEE_RED_S], [TEE_RED_S], [TEE_BLUE_M]]);
|
||
expect(dialog()).toBeInTheDocument();
|
||
});
|
||
|
||
it("shows each variant's cart quantity", () => {
|
||
renderGrid({ cartQtyByPricingId: { "pr-202": 4 } });
|
||
fireEvent.click(groupTile());
|
||
expect(within(dialog()).getByRole("button", { name: /^Add Blue - M/ })).toHaveTextContent("4");
|
||
});
|
||
|
||
it("a sold-out variant reads Order in and cannot be added for Take now", () => {
|
||
const { onAdd } = renderGrid();
|
||
fireEvent.click(groupTile());
|
||
const green = within(dialog()).getByRole("button", { name: /^Add Green - L/ });
|
||
expect(green).toHaveTextContent("Order in");
|
||
expect(green).toBeDisabled();
|
||
fireEvent.click(green);
|
||
expect(onAdd).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("a sold-out variant is added when Order in is allowed", () => {
|
||
const { onAdd } = renderGrid({ allowOrderIn: true });
|
||
fireEvent.click(groupTile());
|
||
fireEvent.click(within(dialog()).getByRole("button", { name: /^Add Green - L/ }));
|
||
expect(onAdd).toHaveBeenCalledWith(TEE_GREEN_L);
|
||
});
|
||
|
||
it("closing returns focus to the scan box, so the next barcode scan is not lost", async () => {
|
||
renderGrid();
|
||
fireEvent.click(groupTile());
|
||
fireEvent.click(within(dialog()).getAllByRole("button", { name: "Close" })[0]);
|
||
// Radix restores focus in a deferred callback once the dialog unmounts.
|
||
await waitFor(() => expect(search()).toHaveFocus());
|
||
});
|
||
|
||
it("a grouped tile whose variants all cannot be added reads Order in", () => {
|
||
const allOut = [TEE_RED_S, TEE_BLUE_M].map((r) => ({ ...r, availability: "SOLD_OUT" as const }));
|
||
renderGrid({ products: allOut });
|
||
expect(groupTile()).toHaveTextContent("Order in");
|
||
});
|
||
|
||
it("closes on Close", () => {
|
||
renderGrid();
|
||
fireEvent.click(groupTile());
|
||
const closeButtons = within(dialog()).getAllByRole("button", { name: "Close" });
|
||
fireEvent.click(closeButtons[0]);
|
||
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
||
});
|
||
});
|
||
|
||
describe("ProductGrid — search and scan reach variants", () => {
|
||
it("a name search matching one variant shows the grouped tile", () => {
|
||
renderGrid();
|
||
fireEvent.change(search(), { target: { value: "blue" } });
|
||
expect(groupTile()).toBeInTheDocument();
|
||
expect(screen.queryByRole("button", { name: /^Add Baby Bottle/ })).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("the category filter keeps the grouped tile under its category", () => {
|
||
renderGrid();
|
||
fireEvent.click(screen.getByRole("button", { name: "Feeding" }));
|
||
expect(screen.queryByRole("button", { name: "Choose an option of Baby Tee" })).not.toBeInTheDocument();
|
||
fireEvent.click(screen.getByRole("button", { name: "Clothing" }));
|
||
expect(groupTile()).toBeInTheDocument();
|
||
});
|
||
|
||
it("an exact scan code shows that variant as its own tile", () => {
|
||
renderGrid();
|
||
fireEvent.change(search(), { target: { value: "9555000000011" } });
|
||
expect(screen.getByRole("button", { name: /^Add Baby Tee - Red - S/ })).toBeInTheDocument();
|
||
expect(screen.queryByRole("button", { name: "Choose an option of Baby Tee" })).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("a scanned variant is not also shown inside its group tile", () => {
|
||
const red = { ...TEE_RED_S, scanCodes: ["500"] };
|
||
const blue = { ...TEE_BLUE_M, label: "Baby Tee - Blue - 500ml" };
|
||
renderGrid({ products: [red, blue, BOTTLE] });
|
||
fireEvent.change(search(), { target: { value: "500" } });
|
||
expect(screen.getByRole("button", { name: /^Add Baby Tee - Red - S/ })).toBeInTheDocument();
|
||
expect(screen.getByRole("button", { name: /^Add Baby Tee - Blue - 500ml/ })).toBeInTheDocument();
|
||
expect(screen.queryByRole("button", { name: "Choose an option of Baby Tee" })).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("Enter on an exact scan code adds the variant with no dialog", () => {
|
||
const { onAdd } = renderGrid();
|
||
fireEvent.change(search(), { target: { value: "BB-TEE-BM" } });
|
||
fireEvent.keyDown(search(), { key: "Enter" });
|
||
expect(onAdd).toHaveBeenCalledWith(TEE_BLUE_M);
|
||
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
||
});
|
||
});
|