Files
co-lite/solvway-admin-portal/app/components/product-page/BundleSection.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

231 lines
13 KiB
TypeScript

import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
// simplify-product-setup: the Bundle page's What's in the box and Price sections.
const catalog = vi.hoisted(() => ({ saveCatalog: vi.fn(), getSalesItem: vi.fn(), getSku: vi.fn(), listSkus: vi.fn(), searchSkuVariations: vi.fn() }));
const product = vi.hoisted(() => ({ getProductOptions: vi.fn(), listProductTags: vi.fn(), uploadProductImage: vi.fn(), listProducts: vi.fn() }));
vi.mock("@/app/lib/services/adminCatalogSetup", () => catalog);
vi.mock("@/app/lib/services/adminProduct", () => ({
...product,
PRODUCT_STATUS_FILTER_OPTIONS: [{ value: "Y", labelKey: "Active" }, { value: "N", labelKey: "Inactive" }],
}));
vi.mock("@/app/lib/services/adminLocations", () => ({ listAdminLocations: vi.fn().mockResolvedValue([{ id: 7, code: "MY", name: "Malaysia", type: "C", status: "Y", parent: null }]) }));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
vi.mock("@/app/components/products/QianyiSkuPicker", () => ({ QianyiSkuPicker: () => null }));
import { memoryRouter } from "@/test/memoryRouter";
import { ProductSetupPage } from "./ProductSetupPage";
import type { SkuVariationOption } from "@/app/lib/catalogSetup";
const opt = (id: number, code: string): SkuVariationOption => ({
skuVariationId: id, code, skuId: 50 + id, skuCode: code, skuName: "Thing", variant: "DEFAULT", variantName: "DEFAULT",
});
beforeEach(() => {
Object.values(catalog).forEach((f) => f.mockReset());
Object.values(product).forEach((f) => f.mockReset());
product.getProductOptions.mockResolvedValue({
brands: [{ code: "NIKE", name: "Nike", status: "Y" }],
categories: [{ code: "FW", name: "Footwear", status: "Y" }],
priceTypes: [{ code: "MEMBER", status: "Y" }, { code: "EVENT", status: "Y" }],
});
product.listProductTags.mockResolvedValue([]);
product.uploadProductImage.mockResolvedValue({ url: "https://cdn/new.png" });
catalog.listSkus.mockResolvedValue({ items: [], total: 0 });
catalog.searchSkuVariations.mockImplementation(async (q: string) => [opt(q.startsWith("MOM") ? 2 : 1, q.startsWith("MOM") ? "MOM-001" : "YELLOW-001")]);
catalog.getSku.mockResolvedValue(undefined);
memoryRouter.reset("/products/new");
});
async function pick(label: string, option: string) {
await userEvent.click(screen.getByRole("combobox", { name: label }));
await userEvent.click(await screen.findByRole("option", { name: option }));
}
async function fillBasics() {
await userEvent.type(await screen.findByLabelText("Product code"), "B-NEW");
await userEvent.type(screen.getByLabelText("Product name"), "New bundle");
await pick("Brand", "NIKE");
await pick("Category", "FW");
await userEvent.upload(screen.getByLabelText("Product images"), new File(["x"], "a.png", { type: "image/png" }));
await waitFor(() => expect(product.uploadProductImage).toHaveBeenCalled());
}
async function addItem(scope: { getByRole: typeof screen.getByRole }, query: string, code: string) {
await userEvent.type(scope.getByRole("combobox", { name: "Add an item" }), query);
await userEvent.click(await screen.findByRole("option", { name: new RegExp(code) }));
}
describe("BundleSection", () => {
it("Save sends one DEFAULT variation with both items and its selling price", async () => {
catalog.saveCatalog.mockResolvedValue({ salesItem: { id: 90, code: "B-NEW", name: "New bundle", status: "N", productType: "STANDARD", variations: [], images: [], groupComponentProductIds: [] } });
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
await addItem(screen, "YELLOW", "YELLOW-001");
await addItem(screen, "MOM", "MOM-001");
await userEvent.clear(screen.getByLabelText("Item 2 qty"));
await userEvent.type(screen.getByLabelText("Item 2 qty"), "2");
expect(screen.queryByLabelText(/free qty/i)).not.toBeInTheDocument();
await userEvent.type(screen.getByLabelText("Selling price for New bundle"), "199.00");
await userEvent.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(catalog.saveCatalog).toHaveBeenCalledTimes(1));
const body = catalog.saveCatalog.mock.calls[0][0];
expect(body).not.toHaveProperty("sku");
expect(body.salesItem.variations).toEqual([
expect.objectContaining({
variant: "",
components: [
expect.objectContaining({ skuVariationCode: "YELLOW-001", qty: 1, qtyFoc: 0 }),
expect.objectContaining({ skuVariationCode: "MOM-001", qty: 2, qtyFoc: 0 }),
],
}),
]);
expect(body.pricing.rows).toEqual([expect.objectContaining({ priceType: "MEMBER", price: 199, currency: "MYR", country: "7" })]);
}, 90_000);
it("a bundle with options lists price rows per option, named as the options", async () => {
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
expect(screen.queryByRole("columnheader", { name: "Variation" })).not.toBeInTheDocument();
await userEvent.click(screen.getByRole("switch", { name: "Sell this bundle in options" }));
await userEvent.click(screen.getByRole("button", { name: "Add an option" }));
await userEvent.type(screen.getByLabelText("Option 1 name"), "Duo");
const list = within(screen.getByTestId("price-list"));
const rows = list.getAllByTestId("price-row");
expect(rows.map((r) => within(r).getAllByRole("cell").slice(0, 2).map((c) => c.textContent))).toEqual([
["Duo", "Selling price"], ["Duo", "Event price"], ["Option 2", "Selling price"], ["Option 2", "Event price"],
]);
expect(list.getByLabelText("Selling price for Duo")).toBeInTheDocument();
}, 90_000);
it("an option without a type and name is refused and nothing is sent", async () => {
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
await userEvent.click(screen.getByRole("switch", { name: "Sell this bundle in options" }));
await userEvent.click(screen.getByRole("button", { name: "Add an option" }));
await addItem(within(screen.getByRole("group", { name: "Option 2" })), "YELLOW", "YELLOW-001");
await userEvent.type(screen.getByLabelText("Option 1 type"), "Pack");
await userEvent.type(screen.getByLabelText("Option 1 name"), "Duo");
await addItem(within(screen.getByRole("group", { name: "Option 1" })), "MOM", "MOM-001");
await userEvent.click(screen.getByRole("button", { name: "Save" }));
expect(await screen.findByText("Variant and name are required")).toBeInTheDocument();
expect(catalog.saveCatalog).not.toHaveBeenCalled();
}, 90_000);
it("switching options off folds the last option back into the plain bundle", async () => {
catalog.saveCatalog.mockResolvedValue({ salesItem: { id: 90, code: "B-NEW", name: "New bundle", status: "N", productType: "STANDARD", variations: [], images: [], groupComponentProductIds: [] } });
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
const toggle = screen.getByRole("switch", { name: "Sell this bundle in options" });
await userEvent.click(toggle);
await userEvent.type(screen.getByLabelText("Option 1 type"), "Pack");
await userEvent.type(screen.getByLabelText("Option 1 name"), "Duo");
await addItem(within(screen.getByRole("group", { name: "Option 1" })), "YELLOW", "YELLOW-001");
await addItem(within(screen.getByRole("group", { name: "Option 1" })), "MOM", "MOM-001");
await userEvent.click(toggle);
await userEvent.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(catalog.saveCatalog).toHaveBeenCalled());
expect(catalog.saveCatalog.mock.calls[0][0].salesItem.variations[0].variantName).not.toBe("Duo");
}, 90_000);
it("Discard returns a saved plain bundle to its plain layout", async () => {
catalog.getSalesItem.mockResolvedValue({
id: 91, code: "B-OLD", name: "Old bundle", brand: "NIKE", category: "FW", status: "N", productType: "BUNDLE",
groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false,
variations: [{
id: 911, variant: "DEFAULT", variantName: "DEFAULT", removable: true, pricing: [],
components: [1, 2].map((id) => ({ skuVariationId: id, skuVariationCode: `C-00${id}`, skuId: 50 + id, skuCode: `C-00${id}`, skuName: "Thing", variant: "DEFAULT", variantName: "DEFAULT", qty: 1, qtyFoc: 0 })),
}],
});
render(<ProductSetupPage salesItemId={91} />);
await userEvent.type(await screen.findByLabelText("Product name"), " edited");
await userEvent.click(await screen.findByRole("switch", { name: "Sell this bundle in options" }));
expect(screen.getByLabelText("Option 1 type")).toBeInTheDocument();
await userEvent.click(screen.getByRole("button", { name: "Discard" }));
expect(screen.queryByLabelText("Option 1 type")).not.toBeInTheDocument();
}, 90_000);
it("removing every option and switching off leaves a plain bundle to fill", async () => {
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
const toggle = screen.getByRole("switch", { name: "Sell this bundle in options" });
await userEvent.click(toggle);
await userEvent.click(screen.getByRole("button", { name: "Remove option 1" }));
await userEvent.click(toggle);
expect(screen.getByLabelText(/^Selling price for /)).toBeInTheDocument();
}, 90_000);
it("a saved free qty is sent back unchanged on save", async () => {
catalog.saveCatalog.mockResolvedValue({ salesItem: { id: 91, code: "B-OLD", name: "Old bundle", status: "N", productType: "BUNDLE", variations: [], images: [], groupComponentProductIds: [] } });
catalog.getSalesItem.mockResolvedValue({
id: 91, code: "B-OLD", name: "Old bundle", brand: "NIKE", category: "FW", status: "N", productType: "BUNDLE",
groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false,
variations: [{
id: 911, variant: "DEFAULT", variantName: "DEFAULT", removable: true, pricing: [],
components: [1, 2].map((id) => ({ skuVariationId: id, skuVariationCode: `C-00${id}`, skuId: 50 + id, skuCode: `C-00${id}`, skuName: "Thing", variant: "DEFAULT", variantName: "DEFAULT", qty: 1, qtyFoc: id === 2 ? 1 : 0 })),
}],
});
render(<ProductSetupPage salesItemId={91} />);
await userEvent.type(await screen.findByLabelText("Product name"), " edited");
expect(screen.queryByLabelText(/free qty/i)).not.toBeInTheDocument();
await userEvent.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(catalog.saveCatalog).toHaveBeenCalledTimes(1));
const comps = catalog.saveCatalog.mock.calls[0][0].salesItem.variations[0].components;
expect(comps).toEqual([
expect.objectContaining({ skuVariationCode: "C-001", qtyFoc: 0 }),
expect.objectContaining({ skuVariationCode: "C-002", qtyFoc: 1 }),
]);
}, 90_000);
it("a freshly added item row shows Qty and no Free qty field", async () => {
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
await addItem(screen, "YELLOW", "YELLOW-001");
expect(screen.getByLabelText("Item 1 qty")).toBeInTheDocument();
expect(screen.queryByLabelText(/free qty/i)).not.toBeInTheDocument();
}, 90_000);
it("a one-item bundle with options off shows the 2-item error under What's in the box and sends nothing", async () => {
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
await addItem(screen, "YELLOW", "YELLOW-001");
await userEvent.type(screen.getByLabelText("Selling price for New bundle"), "199.00");
await userEvent.click(screen.getByRole("button", { name: "Save" }));
expect(await screen.findByText("A bundle needs at least 2 items in total")).toBeInTheDocument();
expect(catalog.saveCatalog).not.toHaveBeenCalled();
}, 90_000);
it("a one-item option is flagged while a two-item option is not", async () => {
render(<ProductSetupPage newType="bundle" />);
await fillBasics();
await userEvent.click(screen.getByRole("switch", { name: "Sell this bundle in options" }));
await userEvent.type(screen.getByLabelText("Option 1 type"), "Kit");
await userEvent.type(screen.getByLabelText("Option 1 name"), "Travel Kit");
await addItem(within(screen.getByRole("group", { name: "Option 1" })), "YELLOW", "YELLOW-001");
await userEvent.click(screen.getByRole("button", { name: "Add an option" }));
await userEvent.type(screen.getByLabelText("Option 2 type"), "Kit");
await userEvent.type(screen.getByLabelText("Option 2 name"), "Camping Kit");
await addItem(within(screen.getByRole("group", { name: "Option 2" })), "YELLOW", "YELLOW-001");
await addItem(within(screen.getByRole("group", { name: "Option 2" })), "MOM", "MOM-001");
await userEvent.click(screen.getByRole("button", { name: "Save" }));
const msg = "A bundle needs at least 2 items in total";
expect(await within(screen.getByRole("group", { name: "Option 1" })).findByText(msg)).toBeInTheDocument();
expect(within(screen.getByRole("group", { name: "Option 2" })).queryByText(msg)).not.toBeInTheDocument();
expect(catalog.saveCatalog).not.toHaveBeenCalled();
}, 90_000);
});