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
343 lines
13 KiB
TypeScript
343 lines
13 KiB
TypeScript
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi } from "vitest";
|
|
|
|
import { ProductGrid } from "./ProductGrid";
|
|
import type { PosEventProduct } from "@/app/lib/types";
|
|
|
|
// add-sku-barcodes: a scan matches a product's scan codes (its SKU variant's barcodes and code) as well as
|
|
// its product code; a scan that matches nothing, or only a sold-out product, says so; focus stays on the box.
|
|
|
|
const ROMPER_RED: PosEventProduct = {
|
|
pPricingId: "pr-101",
|
|
variationId: "101",
|
|
label: "Romper - Red",
|
|
unitPrice: 39.9,
|
|
productCode: "BA-002",
|
|
sku: "",
|
|
category: "Clothing",
|
|
availability: "IN_STOCK",
|
|
scanCodes: ["BA-002-RED", "9555123400011", "9555123400028"],
|
|
};
|
|
|
|
const ROMPER_BLUE: PosEventProduct = {
|
|
...ROMPER_RED,
|
|
pPricingId: "pr-102",
|
|
variationId: "102",
|
|
label: "Romper - Blue",
|
|
scanCodes: ["BA-002-BLU", "9555123400035"],
|
|
};
|
|
|
|
const STROLLER: PosEventProduct = {
|
|
pPricingId: "pr-103",
|
|
variationId: "103",
|
|
label: "Magicfold Stroller",
|
|
unitPrice: 1099,
|
|
productCode: "QY-HAM-0301",
|
|
sku: "",
|
|
category: "Strollers",
|
|
availability: "SOLD_OUT",
|
|
scanCodes: ["6971234567890"],
|
|
};
|
|
|
|
const PRODUCTS = [ROMPER_RED, ROMPER_BLUE, STROLLER];
|
|
|
|
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" });
|
|
|
|
function scan(value: string) {
|
|
fireEvent.change(search(), { target: { value } });
|
|
fireEvent.keyDown(search(), { key: "Enter" });
|
|
}
|
|
|
|
describe("ProductGrid — barcode scan", () => {
|
|
it("adds the product whose barcode matches, clears the box and keeps it focused", () => {
|
|
const onAdd = renderGrid();
|
|
search().focus();
|
|
scan("9555123400028");
|
|
expect(onAdd).toHaveBeenCalledWith(ROMPER_RED);
|
|
expect(onAdd).toHaveBeenCalledTimes(1);
|
|
expect(search()).toHaveValue("");
|
|
expect(search()).toHaveFocus();
|
|
});
|
|
|
|
it("matches a SKU variant code, trimmed and ignoring case", () => {
|
|
const onAdd = renderGrid();
|
|
scan(" ba-002-blu ");
|
|
expect(onAdd).toHaveBeenCalledWith(ROMPER_BLUE);
|
|
});
|
|
|
|
it("a product code shared by two variants still needs a tap (both tiles shown)", () => {
|
|
const onAdd = renderGrid();
|
|
scan("BA-002");
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
expect(screen.getByRole("button", { name: /^Add Romper - Red/ })).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: /^Add Romper - Blue/ })).toBeInTheDocument();
|
|
});
|
|
|
|
it("several matches show as tiles even when another category tab is active", () => {
|
|
const onAdd = renderGrid();
|
|
fireEvent.click(screen.getByRole("button", { name: "Strollers" }));
|
|
scan("BA-002");
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
expect(screen.getByRole("button", { name: /^Add Romper - Red/ })).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: /^Add Romper - Blue/ })).toBeInTheDocument();
|
|
});
|
|
|
|
it("the typed barcode filters the grid to its product", () => {
|
|
renderGrid();
|
|
fireEvent.change(search(), { target: { value: "9555123400035" } });
|
|
expect(screen.getByRole("button", { name: /^Add Romper - Blue/ })).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /^Add Romper - Red/ })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("an unknown barcode says nothing here matches and adds nothing", () => {
|
|
const onAdd = renderGrid();
|
|
scan("0000000000000");
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
expect(screen.getByRole("status")).toHaveTextContent("No product here matches 0000000000000");
|
|
expect(search()).toHaveValue("0000000000000");
|
|
});
|
|
|
|
it("a sold-out match that cannot be ordered in says so", () => {
|
|
const onAdd = renderGrid();
|
|
scan("6971234567890");
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
expect(screen.getByRole("status")).toHaveTextContent("Magicfold Stroller is sold out here");
|
|
});
|
|
|
|
it("a sold-out match is added when Order in is allowed", () => {
|
|
const onAdd = renderGrid({ allowOrderIn: true });
|
|
scan("6971234567890");
|
|
expect(onAdd).toHaveBeenCalledWith(STROLLER);
|
|
});
|
|
|
|
it("the message clears on the next keystroke", () => {
|
|
renderGrid();
|
|
scan("0000000000000");
|
|
fireEvent.change(search(), { target: { value: "9" } });
|
|
expect(screen.queryByText(/No product here matches/)).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("tapping a tile returns focus to the scan box", () => {
|
|
// group-pos-variants: the two Rompers now share one grouped tile, so tap a single-row product.
|
|
const onAdd = renderGrid({ allowOrderIn: true });
|
|
const tile = screen.getByRole("button", { name: /^Add Magicfold Stroller/ });
|
|
tile.focus();
|
|
fireEvent.click(tile);
|
|
expect(onAdd).toHaveBeenCalledWith(STROLLER);
|
|
expect(search()).toHaveFocus();
|
|
});
|
|
});
|
|
|
|
describe("ProductGrid — scanner keys redirected from outside the scan box", () => {
|
|
function renderBeside() {
|
|
const onAdd = vi.fn();
|
|
const onOther = vi.fn();
|
|
render(
|
|
<div>
|
|
<button onClick={onOther}>Other button</button>
|
|
<input aria-label="Other input" />
|
|
<ProductGrid products={PRODUCTS} cartQtyByPricingId={{}} onAdd={onAdd} allowOrderIn={false} />
|
|
</div>,
|
|
);
|
|
return { onAdd, onOther, user: userEvent.setup() };
|
|
}
|
|
|
|
it("a scan typed while another button is focused adds the product without pressing that button", async () => {
|
|
const { onAdd, onOther, user } = renderBeside();
|
|
await user.click(screen.getByRole("button", { name: "Other button" }));
|
|
onOther.mockClear();
|
|
await user.keyboard("9555123400028{Enter}");
|
|
expect(onAdd).toHaveBeenCalledTimes(1);
|
|
expect(onAdd).toHaveBeenCalledWith(ROMPER_RED);
|
|
expect(onOther).not.toHaveBeenCalled();
|
|
expect(search()).toHaveValue("");
|
|
expect(search()).toHaveFocus();
|
|
});
|
|
|
|
it("repeat scans, with a click on another button between, add the product each time", async () => {
|
|
const { onAdd, user } = renderBeside();
|
|
for (let i = 0; i < 3; i++) {
|
|
await user.click(screen.getByRole("button", { name: "Other button" }));
|
|
await user.keyboard("9555123400028{Enter}");
|
|
}
|
|
expect(onAdd).toHaveBeenCalledTimes(3);
|
|
expect(onAdd).toHaveBeenNthCalledWith(3, ROMPER_RED);
|
|
});
|
|
|
|
it("typing into another input is left alone", async () => {
|
|
const { onAdd, user } = renderBeside();
|
|
await user.click(screen.getByRole("textbox", { name: "Other input" }));
|
|
await user.keyboard("100");
|
|
expect(screen.getByRole("textbox", { name: "Other input" })).toHaveValue("100");
|
|
expect(search()).toHaveValue("");
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("a Ctrl+key on a focused button does not move focus into the scan box", async () => {
|
|
const { user } = renderBeside();
|
|
const other = screen.getByRole("button", { name: "Other button" });
|
|
await user.click(other);
|
|
await user.keyboard("{Control>}a{/Control}");
|
|
expect(other).toHaveFocus();
|
|
expect(search()).toHaveValue("");
|
|
});
|
|
|
|
it("Enter on a focused button with nothing typed presses that button", async () => {
|
|
const { onAdd, onOther, user } = renderBeside();
|
|
screen.getByRole("button", { name: "Other button" }).focus();
|
|
await user.keyboard("{Enter}");
|
|
expect(onOther).toHaveBeenCalledTimes(1);
|
|
expect(onAdd).not.toHaveBeenCalled();
|
|
});
|
|
it("Space on a focused button still presses it and does not reach the scan box", async () => {
|
|
const { onOther, user } = renderBeside();
|
|
screen.getByRole("button", { name: "Other button" }).focus();
|
|
await user.keyboard(" ");
|
|
expect(onOther).toHaveBeenCalledTimes(1);
|
|
expect(search()).toHaveValue("");
|
|
});
|
|
|
|
it("with the variant dialog open, a typed letter is not redirected into the scan box", async () => {
|
|
const user = userEvent.setup();
|
|
const tee = (id: string, variantName: string): PosEventProduct => ({
|
|
pPricingId: `pr-${id}`,
|
|
variationId: id,
|
|
label: `Baby Tee - ${variantName}`,
|
|
productName: "Baby Tee",
|
|
variantName,
|
|
unitPrice: 29.9,
|
|
productCode: "BB-TEE",
|
|
sku: "",
|
|
category: "Clothing",
|
|
availability: "IN_STOCK",
|
|
scanCodes: [`BB-TEE-${id}`],
|
|
});
|
|
render(
|
|
<ProductGrid
|
|
products={[tee("201", "Red - S"), tee("202", "Blue - M")]}
|
|
cartQtyByPricingId={{}}
|
|
onAdd={vi.fn()}
|
|
allowOrderIn={false}
|
|
/>,
|
|
);
|
|
await user.click(screen.getByRole("button", { name: "Choose an option of Baby Tee" }));
|
|
expect(screen.getByRole("dialog", { name: "Baby Tee" })).toBeInTheDocument();
|
|
await user.keyboard("x");
|
|
expect(screen.getByRole("searchbox", { hidden: true })).toHaveValue("");
|
|
});
|
|
|
|
it.each(["menu", "listbox"])("a key typed inside a role=%s is not redirected", async (role) => {
|
|
const onAdd = vi.fn();
|
|
const user = userEvent.setup();
|
|
render(
|
|
<div>
|
|
<div role={role} tabIndex={0} aria-label="Popup">
|
|
<span>item</span>
|
|
</div>
|
|
<ProductGrid products={PRODUCTS} cartQtyByPricingId={{}} onAdd={onAdd} allowOrderIn={false} />
|
|
</div>,
|
|
);
|
|
const popup = screen.getByRole(role, { name: "Popup" });
|
|
popup.focus();
|
|
await user.keyboard("a");
|
|
expect(search()).toHaveValue("");
|
|
expect(popup).toHaveFocus();
|
|
});
|
|
|
|
it("an open alertdialog anywhere in the document stops redirection", async () => {
|
|
const user = userEvent.setup();
|
|
render(
|
|
<div>
|
|
<div role="alertdialog" aria-label="Confirm">
|
|
<p>Are you sure?</p>
|
|
</div>
|
|
<button>Other button</button>
|
|
<ProductGrid products={PRODUCTS} cartQtyByPricingId={{}} onAdd={vi.fn()} allowOrderIn={false} />
|
|
</div>,
|
|
);
|
|
const other = screen.getByRole("button", { name: "Other button" });
|
|
other.focus();
|
|
await user.keyboard("a");
|
|
expect(search()).toHaveValue("");
|
|
expect(other).toHaveFocus();
|
|
});
|
|
|
|
it("redirects in the capture phase and prevents default, so other keydown handlers never see the scan", async () => {
|
|
const user = userEvent.setup();
|
|
const seen: string[] = [];
|
|
const hammer: PosEventProduct = { ...ROMPER_RED, pPricingId: "pr-900", variationId: "900", label: "Hammer", productCode: "HM-9", scanCodes: ["HAM-003"] };
|
|
const onAdd = vi.fn();
|
|
render(
|
|
<div>
|
|
<button onKeyDown={(e) => { if (!e.defaultPrevented) seen.push(e.key); }}>Recorder</button>
|
|
<ProductGrid products={[hammer]} cartQtyByPricingId={{}} onAdd={onAdd} allowOrderIn={false} />
|
|
</div>,
|
|
);
|
|
screen.getByRole("button", { name: "Recorder" }).focus();
|
|
await user.keyboard("HAM-003{Enter}");
|
|
expect(onAdd).toHaveBeenCalledWith(hammer);
|
|
expect(seen).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("ProductGrid — a refused scan leaves its text selected", () => {
|
|
it("no-match: message and text stay, text is selected, the next scan replaces it", async () => {
|
|
const user = userEvent.setup();
|
|
const onAdd = renderGrid();
|
|
await user.click(search());
|
|
await user.keyboard("NOPE-1{Enter}");
|
|
expect(screen.getByRole("status")).toHaveTextContent("No product here matches NOPE-1");
|
|
expect(search()).toHaveValue("NOPE-1");
|
|
expect((search() as HTMLInputElement).selectionStart).toBe(0);
|
|
expect((search() as HTMLInputElement).selectionEnd).toBe(6);
|
|
await user.keyboard("9555123400028{Enter}");
|
|
expect(onAdd).toHaveBeenCalledTimes(1);
|
|
expect(onAdd).toHaveBeenCalledWith(ROMPER_RED);
|
|
expect(search()).toHaveValue("");
|
|
});
|
|
|
|
it("sold-out: message and text stay, text is selected, the next scan replaces it", async () => {
|
|
const user = userEvent.setup();
|
|
const onAdd = renderGrid();
|
|
await user.click(search());
|
|
await user.keyboard("6971234567890{Enter}");
|
|
expect(screen.getByRole("status")).toHaveTextContent("Magicfold Stroller is sold out here");
|
|
expect(search()).toHaveValue("6971234567890");
|
|
expect((search() as HTMLInputElement).selectionStart).toBe(0);
|
|
expect((search() as HTMLInputElement).selectionEnd).toBe(13);
|
|
await user.keyboard("9555123400028{Enter}");
|
|
expect(onAdd).toHaveBeenCalledTimes(1);
|
|
expect(onAdd).toHaveBeenCalledWith(ROMPER_RED);
|
|
expect(search()).toHaveValue("");
|
|
});
|
|
|
|
it("after focus leaves the box, a redirected scan replaces the selected refused text", async () => {
|
|
const user = userEvent.setup();
|
|
const onAdd = vi.fn();
|
|
const onOther = vi.fn();
|
|
render(
|
|
<div>
|
|
<button onClick={onOther}>Other button</button>
|
|
<ProductGrid products={PRODUCTS} cartQtyByPricingId={{}} onAdd={onAdd} allowOrderIn={false} />
|
|
</div>,
|
|
);
|
|
await user.click(search());
|
|
await user.keyboard("NOPE-1{Enter}");
|
|
expect(screen.getByRole("status")).toHaveTextContent("No product here matches NOPE-1");
|
|
await user.click(screen.getByRole("button", { name: "Other button" }));
|
|
expect(search()).not.toHaveFocus();
|
|
await user.keyboard("9555123400028{Enter}");
|
|
expect(onAdd).toHaveBeenCalledTimes(1);
|
|
expect(onAdd).toHaveBeenCalledWith(ROMPER_RED);
|
|
expect(search()).toHaveValue("");
|
|
expect(onOther).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|