Files
co-lite/solvway-admin-portal/app/pos/components/CartPanel.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

117 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { fireEvent, render, screen, within } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import { CartPanel } from "./CartPanel";
import type { PosLine } from "@/app/pos/shared";
// redesign-pos-sale-flow (BABYBOO-38): each cart row shows the product image (or a placeholder), the
// name, code and unit price, the line amount, − qty + steppers and Remove. The total lives in the
// order pane's footer, not here.
const LINE_A: PosLine = {
key: 1,
pPricingId: "pr-001",
variationId: "v-001",
label: "Product A",
unitPrice: 10,
qty: 2,
imageUrl: "https://cdn.example/a.png",
productCode: "QY-ISU-0101",
};
const LINE_B: PosLine = {
key: 2,
pPricingId: "pr-002",
variationId: "v-002",
label: "Product B",
unitPrice: 25,
qty: 1,
};
function renderCart(props: Partial<React.ComponentProps<typeof CartPanel>> = {}) {
const handlers = { onInc: vi.fn(), onDec: vi.fn(), onRemove: vi.fn() };
render(<CartPanel lines={[LINE_A, LINE_B]} {...handlers} {...props} />);
return handlers;
}
const row = (key: number) => screen.getByTestId(`cart-line-${key}`);
describe("CartPanel — rows", () => {
it("shows name, code, unit price and the line amount", () => {
renderCart();
expect(within(row(1)).getByText("Product A")).toBeInTheDocument();
expect(within(row(1)).getByText("QY-ISU-0101 · RM 10.00 each")).toBeInTheDocument();
expect(within(row(1)).getByText("RM 20.00")).toBeInTheDocument();
expect(within(row(2)).getByText("RM 25.00 each")).toBeInTheDocument();
expect(within(row(1)).getByTestId("cart-qty-1")).toHaveTextContent("2");
});
it("shows the product image, or a placeholder when there is none", () => {
renderCart();
const img = within(row(1)).getByTestId("cart-image-1");
expect(img.tagName).toBe("IMG");
expect(img).toHaveAttribute("src", "https://cdn.example/a.png");
const placeholder = within(row(2)).getByTestId("cart-image-2");
expect(placeholder.tagName).not.toBe("IMG");
});
it("falls back to the placeholder when the image fails to load", () => {
renderCart();
fireEvent.error(screen.getByTestId("cart-image-1"));
expect(screen.getByTestId("cart-image-1").tagName).not.toBe("IMG");
});
it("marks a line that is sold out here as Order in", () => {
renderCart({ soldOutIds: new Set(["pr-002"]) });
expect(within(row(2)).getByText("Order in")).toBeInTheDocument();
expect(within(row(1)).queryByText("Order in")).not.toBeInTheDocument();
});
it("shows each discounted line's campaign and amount under that line, keyed by line index", () => {
renderCart({ discountLines: [{ lineIndex: 1, campaignLabel: "Expo 10%", amount: 2.5 } as never] });
expect(within(row(2)).getByText("Expo 10% -RM 2.50")).toBeInTheDocument();
expect(within(row(1)).queryByText(/Expo 10%/)).not.toBeInTheDocument();
});
it("does not render a total", () => {
renderCart();
expect(screen.queryByTestId("cart-total")).not.toBeInTheDocument();
});
});
describe("CartPanel — empty", () => {
it("says there are no items yet and how to add one", () => {
render(<CartPanel lines={[]} onInc={vi.fn()} onDec={vi.fn()} onRemove={vi.fn()} />);
expect(screen.getByText("No items yet")).toBeInTheDocument();
expect(screen.getByText("Scan a barcode or tap a product to add it.")).toBeInTheDocument();
});
});
describe("CartPanel — controls", () => {
it("+ increments the line", () => {
const h = renderCart();
fireEvent.click(screen.getByRole("button", { name: "Increase Product A" }));
expect(h.onInc).toHaveBeenCalledWith(1);
});
it("− decrements a line above 1", () => {
const h = renderCart();
fireEvent.click(screen.getByRole("button", { name: "Decrease Product A" }));
expect(h.onDec).toHaveBeenCalledWith(1);
expect(h.onRemove).not.toHaveBeenCalled();
});
it("− on a line of 1 removes it", () => {
const h = renderCart();
fireEvent.click(screen.getByRole("button", { name: "Decrease Product B" }));
expect(h.onRemove).toHaveBeenCalledWith(2);
expect(h.onDec).not.toHaveBeenCalled();
});
it("Remove removes the line", () => {
const h = renderCart();
fireEvent.click(screen.getByRole("button", { name: "Remove Product A" }));
expect(h.onRemove).toHaveBeenCalledWith(1);
});
});