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
221 lines
8.2 KiB
TypeScript
221 lines
8.2 KiB
TypeScript
import { fireEvent, render, screen, within } from "@testing-library/react";
|
|
import { describe, it, expect, vi } from "vitest";
|
|
|
|
import { ProductGrid } from "./ProductGrid";
|
|
import type { PosEventProduct } from "@/app/lib/types";
|
|
|
|
// redesign-pos-sale-flow (BABYBOO-38): tap-to-add tiles, at least 4 per row on a laptop, a scan/search
|
|
// box that adds an exact product code on Enter, and "Order in" for products sold out at this counter.
|
|
|
|
const PRODUCT_A: PosEventProduct = {
|
|
pPricingId: "pr-001",
|
|
variationId: "v-001",
|
|
label: "Baby Bottle",
|
|
unitPrice: 12.5,
|
|
productCode: "BC-001",
|
|
sku: "",
|
|
category: "Feeding",
|
|
imageUrl: "https://cdn.example/bottle.png",
|
|
availability: "IN_STOCK",
|
|
};
|
|
|
|
const PRODUCT_B: PosEventProduct = {
|
|
pPricingId: "pr-002",
|
|
variationId: "v-002",
|
|
label: "Baby Onesie",
|
|
unitPrice: 25.0,
|
|
productCode: "BC-002",
|
|
sku: "",
|
|
category: "Clothing",
|
|
};
|
|
|
|
const PRODUCT_C: PosEventProduct = {
|
|
pPricingId: "pr-003",
|
|
variationId: "v-003",
|
|
label: "Diaper Bag",
|
|
unitPrice: 45.0,
|
|
productCode: "BC-003",
|
|
sku: "",
|
|
category: null,
|
|
};
|
|
|
|
const SOLD_OUT: PosEventProduct = {
|
|
pPricingId: "pr-004",
|
|
variationId: "v-004",
|
|
label: "Magicfold Stroller",
|
|
unitPrice: 1099,
|
|
productCode: "QY-HAM-0301",
|
|
sku: "",
|
|
category: "Strollers",
|
|
availability: "SOLD_OUT",
|
|
};
|
|
|
|
const PRE_ORDER: PosEventProduct = { ...PRODUCT_B, pPricingId: "pr-005", label: "Carrier", productCode: "BC-005", availability: "PRE_ORDER" };
|
|
|
|
const PRODUCTS = [PRODUCT_A, PRODUCT_B, PRODUCT_C];
|
|
|
|
function renderGrid(props: Partial<React.ComponentProps<typeof ProductGrid>> = {}) {
|
|
const onAdd = vi.fn();
|
|
render(
|
|
<ProductGrid products={PRODUCTS} cartQtyByPricingId={{}} onAdd={onAdd} allowOrderIn={false} {...props} />,
|
|
);
|
|
return onAdd;
|
|
}
|
|
|
|
const search = () => screen.getByRole("searchbox", { name: "Scan barcode, or type SKU / product name" });
|
|
const tile = (label: string) => screen.getByRole("button", { name: new RegExp(`^Add ${label}`) });
|
|
|
|
describe("ProductGrid — category bar", () => {
|
|
it("renders 'All' plus each distinct category plus 'Other' for a blank category", () => {
|
|
renderGrid();
|
|
const bar = screen.getByRole("group", { name: "Categories" });
|
|
expect(within(bar).getAllByRole("button").map((b) => b.textContent)).toEqual(["All", "Feeding", "Clothing", "Other"]);
|
|
expect(within(bar).getByRole("button", { name: "All" })).toHaveAttribute("aria-pressed", "true");
|
|
});
|
|
|
|
it("selecting a category shows only its products", () => {
|
|
renderGrid();
|
|
fireEvent.click(screen.getByRole("button", { name: "Feeding" }));
|
|
expect(tile("Baby Bottle")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /^Add Baby Onesie/ })).not.toBeInTheDocument();
|
|
fireEvent.click(screen.getByRole("button", { name: "Other" }));
|
|
expect(tile("Diaper Bag")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /^Add Baby Bottle/ })).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("ProductGrid — search", () => {
|
|
it("narrows by name, case-insensitively", () => {
|
|
renderGrid();
|
|
fireEvent.change(search(), { target: { value: "onesie" } });
|
|
expect(tile("Baby Onesie")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /^Add Baby Bottle/ })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("narrows by product code", () => {
|
|
renderGrid();
|
|
fireEvent.change(search(), { target: { value: "bc-003" } });
|
|
expect(tile("Diaper Bag")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /^Add Baby Bottle/ })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("composes with the active category", () => {
|
|
renderGrid();
|
|
fireEvent.click(screen.getByRole("button", { name: "Clothing" }));
|
|
fireEvent.change(search(), { target: { value: "baby" } });
|
|
expect(tile("Baby Onesie")).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /^Add Baby Bottle/ })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("Enter on an exact product code adds it and clears the search", () => {
|
|
const onAdd = renderGrid();
|
|
fireEvent.change(search(), { target: { value: "bc-002" } });
|
|
fireEvent.keyDown(search(), { key: "Enter" });
|
|
expect(onAdd).toHaveBeenCalledWith(PRODUCT_B);
|
|
expect(search()).toHaveValue("");
|
|
});
|
|
|
|
it("Enter on a partial match adds nothing", () => {
|
|
const onAdd = renderGrid();
|
|
fireEvent.change(search(), { target: { value: "baby" } });
|
|
fireEvent.keyDown(search(), { key: "Enter" });
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
expect(search()).toHaveValue("baby");
|
|
});
|
|
|
|
it("Enter on a code shared by several variants adds nothing (the operator taps the variant)", () => {
|
|
const twin = { ...PRODUCT_B, pPricingId: "pr-006", variationId: "v-006", label: "Baby Onesie (L)" };
|
|
const onAdd = renderGrid({ products: [PRODUCT_B, twin] });
|
|
fireEvent.change(search(), { target: { value: "BC-002" } });
|
|
fireEvent.keyDown(search(), { key: "Enter" });
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("Enter on a sold-out code adds nothing while Order in is off", () => {
|
|
const onAdd = renderGrid({ products: [SOLD_OUT] });
|
|
fireEvent.change(search(), { target: { value: "QY-HAM-0301" } });
|
|
fireEvent.keyDown(search(), { key: "Enter" });
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("ProductGrid — tiles", () => {
|
|
it("tapping a tile adds one of that product", () => {
|
|
const onAdd = renderGrid();
|
|
fireEvent.click(tile("Baby Bottle"));
|
|
expect(onAdd).toHaveBeenCalledWith(PRODUCT_A);
|
|
});
|
|
|
|
it("shows the name, product code and price", () => {
|
|
renderGrid();
|
|
const t = tile("Baby Bottle");
|
|
expect(t).toHaveTextContent("Baby Bottle");
|
|
expect(t).toHaveTextContent("BC-001");
|
|
expect(t).toHaveTextContent("12.50");
|
|
});
|
|
|
|
it("shows a quantity badge and marks a tile that is in the cart", () => {
|
|
renderGrid({ cartQtyByPricingId: { "pr-001": 3 } });
|
|
expect(screen.getByTestId("product-qty-pr-001")).toHaveTextContent("3");
|
|
expect(screen.getByTestId("product-card-pr-001")).toHaveAttribute("data-in-cart", "true");
|
|
expect(screen.queryByTestId("product-qty-pr-002")).not.toBeInTheDocument();
|
|
expect(screen.getByTestId("product-card-pr-002")).toHaveAttribute("data-in-cart", "false");
|
|
});
|
|
|
|
it("renders the whole product image, or a placeholder", () => {
|
|
renderGrid();
|
|
const img = screen.getByTestId("product-image-pr-001");
|
|
expect(img.tagName).toBe("IMG");
|
|
expect(img).toHaveAttribute("alt", "Baby Bottle");
|
|
expect(img).toHaveAttribute("src", "https://cdn.example/bottle.png");
|
|
expect(img.className).toContain("object-contain");
|
|
expect(screen.getByTestId("product-image-pr-002")).toHaveTextContent("No image");
|
|
});
|
|
|
|
it("falls back to the placeholder when the image fails to load", () => {
|
|
renderGrid();
|
|
fireEvent.error(screen.getByTestId("product-image-pr-001"));
|
|
expect(screen.getByTestId("product-image-pr-001")).toHaveTextContent("No image");
|
|
});
|
|
|
|
it("lays out at least 4 tiles per row from the lg breakpoint", () => {
|
|
renderGrid();
|
|
const list = screen.getByRole("list", { name: "Products" });
|
|
expect(list.className).toContain("grid-cols-2");
|
|
expect(list.className).toContain("sm:grid-cols-3");
|
|
expect(list.className).toContain("lg:grid-cols-4");
|
|
});
|
|
});
|
|
|
|
describe("ProductGrid — stock state", () => {
|
|
it("a sold-out tile reads Order in and cannot be added for Take now", () => {
|
|
const onAdd = renderGrid({ products: [SOLD_OUT] });
|
|
const t = tile("Magicfold Stroller");
|
|
expect(t).toHaveTextContent("Order in");
|
|
expect(t).toBeDisabled();
|
|
fireEvent.click(t);
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("a sold-out tile can be added for Pick up or Deliver", () => {
|
|
const onAdd = renderGrid({ products: [SOLD_OUT], allowOrderIn: true });
|
|
const t = tile("Magicfold Stroller");
|
|
expect(t).toHaveTextContent("Order in");
|
|
expect(t).toBeEnabled();
|
|
fireEvent.click(t);
|
|
expect(onAdd).toHaveBeenCalledWith(SOLD_OUT);
|
|
});
|
|
|
|
it("a pre-order tile says so and stays addable", () => {
|
|
const onAdd = renderGrid({ products: [PRE_ORDER] });
|
|
expect(tile("Carrier")).toHaveTextContent("Pre-order");
|
|
fireEvent.click(tile("Carrier"));
|
|
expect(onAdd).toHaveBeenCalledWith(PRE_ORDER);
|
|
});
|
|
|
|
it("an in-stock tile says so", () => {
|
|
renderGrid();
|
|
expect(tile("Baby Bottle")).toHaveTextContent("In stock");
|
|
});
|
|
});
|