Files
co-lite/solvway-admin-portal/app/components/catalog-setup/SkuEditor.barcodes.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

110 lines
5.0 KiB
TypeScript

import { render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
vi.mock("@/app/lib/services/adminStock", () => ({ getStockOnHand: vi.fn() }));
vi.mock("@/app/lib/services/adminCatalogSetup", () => ({ searchSkuVariations: vi.fn().mockResolvedValue([]) }));
vi.mock("@/app/components/products/QianyiSkuPicker", () => ({ QianyiSkuPicker: () => null }));
vi.mock("./SupplierSelect", () => ({ SupplierSelect: () => null }));
import { SkuEditor } from "./SkuEditor";
import { emptySku, emptySkuVariant, type BarcodeDraft, type SkuDraft } from "@/app/lib/catalogSetup";
// add-sku-barcodes: each variant row lists its barcodes (batch, expiry, Active, remove) and adds new ones.
const SAVED: BarcodeDraft = { id: 1, barcode: "9555123400011", batchNumber: "B2501", expiryDate: "2027-06-30", status: "Y", source: "MANUAL" };
const FROM_QIANYI: BarcodeDraft = { id: 2, barcode: "6971234567890", batchNumber: "", expiryDate: "", status: "Y", source: "QIANYI" };
function sku(barcodes: BarcodeDraft[]): SkuDraft {
return {
...emptySku(),
id: 16,
code: "BA-002",
name: "Romper",
variants: [{ ...emptySkuVariant(), id: 70, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: "0.2", barcodes }],
};
}
function renderEditor(draft: SkuDraft, errors: Record<string, string> = {}) {
const onChange = vi.fn();
render(<SkuEditor sku={draft} onChange={onChange} onUploadImage={vi.fn()} errors={errors} saving={false} />);
return onChange;
}
const barcodesOf = (onChange: ReturnType<typeof vi.fn>) => {
const next = onChange.mock.calls.at(-1)![0] as SkuDraft;
return { list: next.variants[0].barcodes, touched: next.variants[0].barcodesTouched };
};
describe("SkuEditor barcodes", () => {
it("lists a variant's barcodes with batch, expiry, Active and a QianYi badge", () => {
renderEditor(sku([SAVED, FROM_QIANYI]));
const group = screen.getByRole("group", { name: "Variant 1 barcodes" });
expect(within(group).getByText("9555123400011")).toBeInTheDocument();
expect(within(group).getByLabelText("Batch for 9555123400011")).toHaveValue("B2501");
// The repo date picker shows DD/MM/YYYY (no native date inputs).
expect(within(group).getByLabelText("Expiry for 9555123400011")).toHaveValue("30/06/2027");
expect(within(group).getByRole("switch", { name: "Active 9555123400011" })).toBeChecked();
expect(within(group).getByText("QianYi")).toBeInTheDocument();
// A saved barcode's text is not editable.
expect(within(group).queryByDisplayValue("9555123400011")).not.toBeInTheDocument();
});
it("adds a typed barcode on Enter, trimmed, and marks the list touched", async () => {
const user = userEvent.setup();
const onChange = renderEditor(sku([SAVED]));
await user.type(screen.getByLabelText("New barcode for variant 1"), " 9555123400028 {Enter}");
const { list, touched } = barcodesOf(onChange);
expect(list).toEqual([SAVED, { barcode: "9555123400028", batchNumber: "", expiryDate: "", status: "Y", source: "MANUAL" }]);
expect(touched).toBe(true);
});
it("adds with the Add barcode button and ignores a blank entry", async () => {
const user = userEvent.setup();
const onChange = renderEditor(sku([]));
await user.click(screen.getByRole("button", { name: "Add barcode for variant 1" }));
expect(onChange).not.toHaveBeenCalled();
await user.type(screen.getByLabelText("New barcode for variant 1"), "AB-12/x");
await user.click(screen.getByRole("button", { name: "Add barcode for variant 1" }));
expect(barcodesOf(onChange).list).toEqual([{ barcode: "AB-12/x", batchNumber: "", expiryDate: "", status: "Y", source: "MANUAL" }]);
});
it("removes a barcode", async () => {
const user = userEvent.setup();
const onChange = renderEditor(sku([SAVED, FROM_QIANYI]));
await user.click(screen.getByRole("button", { name: "Remove barcode 9555123400011" }));
expect(barcodesOf(onChange)).toEqual({ list: [FROM_QIANYI], touched: true });
});
it("switches a barcode off", async () => {
const user = userEvent.setup();
const onChange = renderEditor(sku([SAVED]));
await user.click(screen.getByRole("switch", { name: "Active 9555123400011" }));
expect(barcodesOf(onChange)).toEqual({ list: [{ ...SAVED, status: "N" }], touched: true });
});
it("edits the batch number", async () => {
const user = userEvent.setup();
const onChange = renderEditor(sku([{ ...SAVED, batchNumber: "" }]));
await user.type(screen.getByLabelText("Batch for 9555123400011"), "B");
expect(barcodesOf(onChange)).toEqual({ list: [{ ...SAVED, batchNumber: "B" }], touched: true });
});
it("shows a barcode's server error on its row", () => {
renderEditor(sku([SAVED]), { "sku.variations[0].barcodes[0].barcode": "Barcode 9555123400011 is already used by BA-003-BLU" });
expect(within(screen.getByRole("group", { name: "Variant 1 barcodes" })).getByText("Barcode 9555123400011 is already used by BA-003-BLU")).toBeInTheDocument();
});
});