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
811 lines
44 KiB
TypeScript
811 lines
44 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { tEn } from "@/test/i18nCatalog";
|
|
import {
|
|
emptyPrice,
|
|
emptySalesItem,
|
|
emptySku,
|
|
emptySkuVariant,
|
|
type PriceDraft,
|
|
type SalesItemDraft,
|
|
type SalesItemView,
|
|
type SkuDraft,
|
|
type SkuVariantDraft,
|
|
type SkuView,
|
|
} from "./catalogSetup";
|
|
import { validatePrices } from "./catalogSetup";
|
|
import { addPrice, buildSave, compareAtError, isLinked, missingShipping, ownedSku, replaceSku, syncOwnedVariations, syncPrices, typeOfView } from "./productPage";
|
|
|
|
const TODAY = "2026-10-21";
|
|
const image = { url: "https://img/1.png", sortOrder: 0 };
|
|
const item = (over: Partial<SalesItemDraft> = {}): SalesItemDraft => ({
|
|
...emptySalesItem(),
|
|
code: "YELLOW-002",
|
|
name: "Nursing Bra",
|
|
brand: "BABYLON",
|
|
category: "Baby Apparels",
|
|
images: [image] as SalesItemDraft["images"],
|
|
...over,
|
|
});
|
|
const shipped = (over: Partial<SkuVariantDraft> = {}): SkuVariantDraft => ({
|
|
...emptySkuVariant(),
|
|
weight: "0.3",
|
|
lengthCm: "30",
|
|
widthCm: "20",
|
|
heightCm: "5",
|
|
...over,
|
|
});
|
|
const sizes = (variants: SkuVariantDraft[]): SkuDraft => ({
|
|
...emptySku(),
|
|
code: "YELLOW-002",
|
|
name: "Nursing Bra",
|
|
optionNames: ["Size"],
|
|
optionValues: ["M, L"],
|
|
variants,
|
|
});
|
|
const sized = (size: string, over: Partial<SkuVariantDraft> = {}) =>
|
|
shipped({ options: [size], variant: "Size", variantName: size, code: `YELLOW-002-${size}`, ...over });
|
|
const price = (key: string, value = "129.00", over: Partial<PriceDraft> = {}): PriceDraft => ({
|
|
...emptyPrice(key, { id: "1", code: "MY", currency: "MYR", isDefault: true }),
|
|
priceType: "MEMBER",
|
|
price: value,
|
|
effectiveFrom: TODAY,
|
|
...over,
|
|
});
|
|
|
|
function owned(sku: SkuDraft, over: Partial<SalesItemDraft> = {}) {
|
|
const synced = syncOwnedVariations(item(over), sku);
|
|
return { item: synced.item, sku: synced.sku };
|
|
}
|
|
|
|
describe("syncOwnedVariations", () => {
|
|
it("makes one variation per variant, mirroring its labels with one component at qty 1", () => {
|
|
const { item: d } = owned(sizes([sized("M"), sized("L")]));
|
|
expect(d.variations).toHaveLength(2);
|
|
expect(d.variations.map((v) => v.variantName)).toEqual(["M", "L"]);
|
|
for (const v of d.variations) {
|
|
expect(v.components).toHaveLength(1);
|
|
expect(v.components[0]).toMatchObject({ qty: "1", qtyFoc: "0" });
|
|
}
|
|
expect(d.variations.map((v) => v.components[0].skuVariationCode)).toEqual(["YELLOW-002-M", "YELLOW-002-L"]);
|
|
});
|
|
|
|
it("keeps an unsaved row's variation key when its option value is renamed", () => {
|
|
const first = owned(sizes([sized("M")]));
|
|
const key = first.item.variations[0].key;
|
|
const renamed = { ...first.sku, variants: [{ ...first.sku.variants[0], options: ["Medium"], variantName: "Medium" }] };
|
|
const again = syncOwnedVariations(first.item, renamed);
|
|
expect(again.item.variations[0].key).toBe(key);
|
|
expect(again.item.variations[0].variantName).toBe("Medium");
|
|
});
|
|
|
|
it("keeps a saved variation's id and key by its SKU variant id", () => {
|
|
const savedVariation = {
|
|
id: 40, key: "v40", variant: "Size", variantName: "M", removable: true,
|
|
components: [{ skuVariationId: 7, skuVariationCode: "YELLOW-002-M", label: "", qty: "1", qtyFoc: "0", qyTouched: false }],
|
|
};
|
|
const sku = sizes([sized("M", { id: 7 }), sized("L")]);
|
|
const { item: d } = syncOwnedVariations(item({ id: 52, variations: [savedVariation] }), sku);
|
|
expect(d.variations[0]).toMatchObject({ id: 40, key: "v40" });
|
|
expect(d.variations[1].id).toBeUndefined();
|
|
});
|
|
|
|
it("a default variant's component code is the SKU code the sku part sends", () => {
|
|
const { item: d } = owned({ ...emptySku(), code: "YELLOW-002", name: "Nursing Bra", variants: [shipped()] });
|
|
expect(d.variations[0].components[0].skuVariationCode).toBe("YELLOW-002");
|
|
});
|
|
});
|
|
|
|
describe("missingShipping", () => {
|
|
it("lists rows lacking weight, length, width or height above 0", () => {
|
|
const sku = sizes([sized("M"), sized("L", { heightCm: "" }), sized("XL", { weight: "0" })]);
|
|
expect(missingShipping(sku)).toEqual([1, 2]);
|
|
});
|
|
});
|
|
|
|
describe("buildSave", () => {
|
|
it("a new owned product with complete variants sends sku, salesItem and pricing", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M"), sized("L")]));
|
|
const prices = d.variations.map((v) => price(v.key));
|
|
const out = buildSave({ type: "product", item: d, saved: emptySalesItem(), prices, sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.errors).toEqual({});
|
|
expect(out.deferred).toEqual([]);
|
|
expect(out.body?.sku).toMatchObject({ code: "YELLOW-002", name: "Nursing Bra", status: "Y" });
|
|
expect(out.body?.salesItem?.variations).toHaveLength(2);
|
|
expect(out.body?.pricing?.rows?.map((r) => r.variationRef)).toEqual(d.variations.map((v) => v.key));
|
|
});
|
|
|
|
it("an Active product whose only Selling price starts later is sent; an ended one is not", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M")]));
|
|
const key = d.variations[0].key;
|
|
const send = (over: Partial<PriceDraft>) =>
|
|
buildSave({ type: "product", item: { ...d, status: "Y" }, saved: emptySalesItem(), prices: [price(key, "129.00", { country: "2", ...over })], sku, linked: false, today: TODAY, t: tEn, country: "2" });
|
|
const upcoming = send({ effectiveFrom: "2026-11-30" });
|
|
expect(upcoming.errors).toEqual({});
|
|
expect(upcoming.body?.salesItem?.status).toBe("Y");
|
|
expect(send({ effectiveFrom: "2026-01-01", effectiveTo: "2026-02-01" }).errors[`pricing.quick.${key}`]).toBe("Selling price is required");
|
|
expect(send({ effectiveFrom: "2026-11-30", status: "N" }).errors[`pricing.quick.${key}`]).toBe("Selling price is required");
|
|
});
|
|
|
|
it("A later-date price needs a later date: a scheduled row from today sends nothing", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M")]));
|
|
const prices = [price(d.variations[0].key), { ...price(d.variations[0].key), later: true, country: "1", effectiveFrom: TODAY }];
|
|
const out = buildSave({ type: "product", item: d, saved: emptySalesItem(), prices, sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["pricing.rows[1].effectiveFrom"]).toBe("Pick a date after today");
|
|
});
|
|
|
|
describe("a later price takes over from the current one at save", () => {
|
|
const send = (laterFrom?: string) => {
|
|
const { item: d, sku } = owned(sizes([sized("M")]));
|
|
const key = d.variations[0].key;
|
|
const prices = [price(key, "129.00", { id: 5, country: "", effectiveFrom: "2026-01-01" })];
|
|
if (laterFrom) prices.push(price(key, "99.00", { country: "2", effectiveFrom: laterFrom, later: true }));
|
|
const out = buildSave({ type: "product", item: d, saved: emptySalesItem(), prices, sku, linked: false, today: TODAY, t: tEn, country: "2" });
|
|
return { out, prices };
|
|
};
|
|
it("a later Selling price from 1 Nov sends the no-country current row ending 31 Oct", () => {
|
|
const { out, prices } = send("2026-11-01");
|
|
expect(out.errors).toEqual({});
|
|
expect(out.body?.pricing?.rows?.map((r) => [r.price, r.effectiveTo])).toEqual([[129, "2026-10-31T23:59:59"], [99, undefined]]);
|
|
expect(prices[0].effectiveTo).toBe("");
|
|
});
|
|
it("moving the later row's From to 15 Nov sends an end of 14 Nov", () => {
|
|
expect(send("2026-11-15").out.body?.pricing?.rows?.[0].effectiveTo).toBe("2026-11-14T23:59:59");
|
|
});
|
|
it("removing the later row sends the current row with its original end (none)", () => {
|
|
const out = send().out;
|
|
expect(out.errors).toEqual({});
|
|
expect(out.body?.pricing?.rows?.map((r) => r.effectiveTo)).toEqual([undefined]);
|
|
});
|
|
it("two later rows of the same variant and type that overlap each other are still a duplicate", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M")]));
|
|
const key = d.variations[0].key;
|
|
const prices = [price(key, "129.00", { id: 5, country: "", effectiveFrom: "2026-01-01" }), price(key, "99.00", { country: "2", effectiveFrom: "2026-11-01", later: true }), price(key, "89.00", { country: "2", effectiveFrom: "2026-11-10", later: true })];
|
|
const out = buildSave({ type: "product", item: d, saved: emptySalesItem(), prices, sku, linked: false, today: TODAY, t: tEn, country: "2" });
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["pricing.rows[2]"]).toMatch(/^Duplicate active price/);
|
|
});
|
|
});
|
|
|
|
it("removing the row of a sold variation is refused with a field error", () => {
|
|
const sold = {
|
|
id: 41, key: "v41", variant: "Size", variantName: "XL", removable: false,
|
|
components: [{ skuVariationId: 9, skuVariationCode: "YELLOW-002-XL", label: "", qty: "1", qtyFoc: "0", qyTouched: false }],
|
|
};
|
|
const saved = item({ id: 52, variations: [sold] });
|
|
const sku = { ...sizes([sized("M", { id: 7 })]), id: 3 };
|
|
const out = buildSave({ type: "product", item: saved, saved, prices: [], sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["salesItem.variations"]).toBe(tEn("SetupHasOrdersCantRemove"));
|
|
});
|
|
|
|
it("a saved variant without a variation yet gets a stable key", () => {
|
|
const sku = { ...sizes([sized("M", { id: 7 })]), id: 3 };
|
|
const first = syncOwnedVariations(item({ id: 52 }), sku);
|
|
const key = first.item.variations[0].key;
|
|
expect(key).toEqual(expect.any(String));
|
|
expect(syncOwnedVariations(first.item, first.sku).item.variations[0].key).toBe(key);
|
|
});
|
|
|
|
it("the owned SKU follows the product name and stays Active on edit", () => {
|
|
const { item: d, sku } = owned({ ...sizes([sized("M", { id: 7 })]), id: 3, status: "N" }, { id: 52, name: "New name", status: "N" });
|
|
const out = buildSave({ type: "product", item: d, saved: d, prices: [], sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body?.sku).toMatchObject({ id: 3, name: "New name", status: "Y" });
|
|
expect(out.body?.salesItem).toMatchObject({ id: 52, status: "N" });
|
|
});
|
|
|
|
it("an Inactive product with a variant missing its height saves the salesItem part only", () => {
|
|
const saved = item({ id: 52, variations: [] });
|
|
const { item: d, sku } = owned(sizes([sized("M"), sized("L", { heightCm: "" })]), { id: 52 });
|
|
const out = buildSave({ type: "product", item: d, saved, prices: [], sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.deferred).toEqual([1]);
|
|
expect(out.errors).toEqual({});
|
|
expect(out.body?.sku).toBeUndefined();
|
|
expect(out.body?.pricing).toBeUndefined();
|
|
expect(out.body?.salesItem).toMatchObject({ id: 52, variations: [] });
|
|
});
|
|
|
|
it("a draft still saves its details when an unfinished new row also lacks a name", () => {
|
|
const saved = item({ id: 52, variations: [] });
|
|
const unnamed = shipped({ options: [""], variant: "Size", variantName: "", heightCm: "" });
|
|
const { item: d, sku } = owned(sizes([sized("M"), unnamed]), { id: 52 });
|
|
const out = buildSave({ type: "product", item: d, saved, prices: [], sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.deferred).toEqual([1]);
|
|
expect(out.errors).toEqual({});
|
|
expect(out.body?.salesItem).toMatchObject({ id: 52, variations: [] });
|
|
});
|
|
|
|
it("a non-number dimension counts as missing", () => {
|
|
expect(missingShipping(sizes([sized("M", { widthCm: "abc" })]))).toEqual([0]);
|
|
});
|
|
|
|
it("an Active product with a variant missing its height sends nothing", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M"), sized("L", { heightCm: "" })]), { status: "Y" });
|
|
const prices = d.variations.map((v) => price(v.key));
|
|
const out = buildSave({ type: "product", item: d, saved: d, prices, sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body).toBeNull();
|
|
expect(out.deferred).toEqual([1]);
|
|
expect(out.errors["sku.variations[1].heightCm"]).toBeTruthy();
|
|
});
|
|
|
|
it("activating with an unpriced variation sends nothing and names it", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M"), sized("L")]), { status: "Y" });
|
|
const prices = [price(d.variations[0].key)];
|
|
const out = buildSave({ type: "product", item: d, saved: d, prices, sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors[`pricing.quick.${d.variations[1].key}`]).toBe(tEn("SetupSellingPriceRequired"));
|
|
});
|
|
|
|
it("a linked product never sends a sku part", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M", { id: 7 })]));
|
|
const out = buildSave({ type: "product", item: d, saved: d, prices: [], sku, linked: true, today: TODAY, t: tEn });
|
|
expect(out.body?.sku).toBeUndefined();
|
|
expect(out.body?.salesItem).toBeDefined();
|
|
expect(out.body?.pricing).toBeDefined();
|
|
});
|
|
|
|
it("a group sends the salesItem part only", () => {
|
|
const d = item({ isGroup: true, groupPickCount: 1, groupComponents: [{ componentProductId: "44" }] });
|
|
const out = buildSave({ type: "group", item: d, saved: d, prices: [], sku: null, linked: false, today: TODAY, t: tEn });
|
|
expect(Object.keys(out.body ?? {})).toEqual(["salesItem"]);
|
|
});
|
|
|
|
it("a bundle sends salesItem and pricing", () => {
|
|
const variation = {
|
|
key: "b1", variant: "", variantName: "", removable: true,
|
|
components: [
|
|
{ skuVariationId: 1, skuVariationCode: "YELLOW-001", label: "", qty: "1", qtyFoc: "0", qyTouched: false },
|
|
{ skuVariationId: 2, skuVariationCode: "MOM-001", label: "", qty: "1", qtyFoc: "1", qyTouched: false },
|
|
],
|
|
};
|
|
const d = item({ variations: [variation] });
|
|
const out = buildSave({ type: "bundle", item: d, saved: d, prices: [price("b1", "199.00")], sku: null, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body?.sku).toBeUndefined();
|
|
expect(out.body?.salesItem?.variations?.[0].components).toHaveLength(2);
|
|
expect(out.body?.pricing?.rows).toHaveLength(1);
|
|
});
|
|
|
|
it("a bundle without contents is refused", () => {
|
|
const d = item({ variations: [] });
|
|
const out = buildSave({ type: "bundle", item: d, saved: d, prices: [], sku: null, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["salesItem.variations"]).toBe(tEn("BundleErrComponentRequired"));
|
|
});
|
|
|
|
describe("a bundle holds at least 2 units", () => {
|
|
const comp = (id: number, code: string, qty: string, qtyFoc = "0") =>
|
|
({ skuVariationId: id, skuVariationCode: code, label: "", qty, qtyFoc, qyTouched: false });
|
|
const opt = (key: string, name: string, components: ReturnType<typeof comp>[]) =>
|
|
({ key, variant: name ? "Kit" : "", variantName: name, removable: true, components });
|
|
const run = (type: "bundle" | "product", variations: ReturnType<typeof opt>[]) => {
|
|
const d = item({ variations });
|
|
return buildSave({ type, item: d, saved: d, prices: variations.map((v) => price(v.key, "99.00")), sku: null, linked: false, today: TODAY, t: tEn });
|
|
};
|
|
const MSG = "A bundle needs at least 2 items in total";
|
|
|
|
it("one item at qty 1 is refused with the 2-item error on the option's components", () => {
|
|
const out = run("bundle", [opt("b1", "", [comp(1, "YELLOW-001", "1")])]);
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["salesItem.variations[0].components"]).toBe(tEn("BundleErrMinTwoUnits"));
|
|
expect(tEn("BundleErrMinTwoUnits")).toBe(MSG);
|
|
});
|
|
|
|
it("only the one-unit option is flagged", () => {
|
|
const out = run("bundle", [
|
|
opt("b1", "Travel Kit", [comp(1, "HAM-001-BLUE", "1")]),
|
|
opt("b2", "Camping Kit", [comp(2, "HAM-002", "1"), comp(3, "HAM-003", "1")]),
|
|
]);
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["salesItem.variations[0].components"]).toBe(MSG);
|
|
expect(out.errors["salesItem.variations[1].components"]).toBeUndefined();
|
|
});
|
|
|
|
it("a multipack of qty 3 saves", () => {
|
|
const out = run("bundle", [opt("b1", "", [comp(2, "MOM-001", "3")])]);
|
|
expect(out.errors["salesItem.variations[0].components"]).toBeUndefined();
|
|
expect(out.body).not.toBeNull();
|
|
});
|
|
|
|
it("a free unit counts: qty 1 + free qty 1 saves", () => {
|
|
const out = run("bundle", [opt("b1", "", [comp(2, "MOM-001", "1", "1")])]);
|
|
expect(out.errors["salesItem.variations[0].components"]).toBeUndefined();
|
|
expect(out.body).not.toBeNull();
|
|
});
|
|
|
|
it("an empty bundle keeps only its own message", () => {
|
|
const out = run("bundle", [opt("b1", "", [])]);
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["salesItem.variations[0].components"]).toBe(tEn("BundleErrComponentRequired"));
|
|
expect(out.errors["salesItem.variations[0].components"]).not.toBe(MSG);
|
|
});
|
|
|
|
it("an empty option beside a filled one gets no 2-item message", () => {
|
|
const out = run("bundle", [
|
|
opt("b1", "A", []),
|
|
opt("b2", "B", [comp(2, "MOM-001", "2")]),
|
|
]);
|
|
expect(out.errors["salesItem.variations[0].components"]).toBe(tEn("BundleErrComponentRequired"));
|
|
expect(out.errors["salesItem.variations[0].components"]).not.toBe(MSG);
|
|
expect(out.errors["salesItem.variations[1].components"]).toBeUndefined();
|
|
});
|
|
|
|
it("a Product with one 1x1 variation gets no 2-item error", () => {
|
|
const sku = sizes([sized("M")]);
|
|
const { item: d, sku: s } = owned(sku);
|
|
const out = buildSave({ type: "product", item: d, saved: d, prices: d.variations.map((v) => price(v.key)), sku: s, linked: false, today: TODAY, t: tEn });
|
|
expect(Object.keys(out.errors).filter((k) => k.endsWith(".components"))).toEqual([]);
|
|
});
|
|
});
|
|
|
|
it("missing brand and category are reported and nothing is sent", () => {
|
|
const { item: d, sku } = owned(sizes([sized("M")]), { brand: "", category: "" });
|
|
const out = buildSave({ type: "product", item: d, saved: emptySalesItem(), prices: [], sku, linked: false, today: TODAY, t: tEn });
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["salesItem.brand"]).toBe(tEn("SalesItemErrBrandRequired"));
|
|
expect(out.errors["salesItem.category"]).toBe(tEn("SalesItemErrCategoryRequired"));
|
|
});
|
|
});
|
|
|
|
const skuView = (over: Partial<SkuView> = {}): SkuView => ({
|
|
id: 9, code: "BA-002", name: "Romper", status: "Y",
|
|
variations: [
|
|
{ id: 1, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: 0.4, usedBy: ["P-001"], onHand: 0 },
|
|
{ id: 2, code: "BA-002-BLUE", variant: "Colour", variantName: "Blue", weight: 0.4, usedBy: ["P-001"], onHand: 0 },
|
|
],
|
|
...over,
|
|
});
|
|
|
|
describe("replaceSku", () => {
|
|
it("drops the old variation ids", () => {
|
|
const old = {
|
|
id: 40, key: "v40", variant: "Size", variantName: "M", removable: true,
|
|
components: [{ skuVariationId: 7, skuVariationCode: "YELLOW-002-M", label: "", qty: "1", qtyFoc: "0", qyTouched: false }],
|
|
};
|
|
const out = replaceSku(item({ id: 52, variations: [old] }), skuView());
|
|
expect(out.variations).toHaveLength(2);
|
|
expect(out.variations.every((v) => v.id == null && v.key !== "v40")).toBe(true);
|
|
expect(out.variations.map((v) => v.components[0].skuVariationId)).toEqual([1, 2]);
|
|
});
|
|
});
|
|
|
|
const view = (variations: SalesItemView["variations"], over: Partial<SalesItemView> = {}): SalesItemView => ({
|
|
id: 52, code: "P-001", name: "Romper", brand: "B", category: "C", status: "N", productType: "STANDARD",
|
|
groupComponentProductIds: [], images: [], yellowLabel: false, allowOversell: false, variations, ...over,
|
|
});
|
|
const component = (skuVariationId: number, over: Record<string, unknown> = {}) => ({
|
|
skuVariationId, skuVariationCode: `V${skuVariationId}`, skuId: 9, skuCode: "BA-002", skuName: "Romper",
|
|
variant: "", variantName: "", qty: 1, qtyFoc: 0, ...over,
|
|
});
|
|
const variationView = (id: number, components: ReturnType<typeof component>[]) =>
|
|
({ id, variant: "", variantName: "", components, pricing: [] }) as SalesItemView["variations"][number];
|
|
|
|
describe("typeOfView", () => {
|
|
it("reads the type from the saved shape", () => {
|
|
expect(typeOfView(view([], { productType: "GROUP" }))).toBe("group");
|
|
expect(typeOfView(view([variationView(1, [component(1)])]))).toBe("product");
|
|
expect(typeOfView(view([variationView(1, [component(1), component(2)])]))).toBe("bundle");
|
|
expect(typeOfView(view([variationView(1, [component(1, { qtyFoc: 1 })])]))).toBe("bundle");
|
|
expect(typeOfView(view([variationView(1, [component(1, { qty: 2 })])]))).toBe("bundle");
|
|
expect(typeOfView(view([variationView(1, [component(1)]), variationView(2, [component(1)])]))).toBe("bundle");
|
|
});
|
|
|
|
it("variations from two SKUs, one component each, stay a product", () => {
|
|
const other = component(5, { skuId: 10, skuCode: "BA-001" });
|
|
expect(typeOfView(view([variationView(1, [component(1)]), variationView(2, [other])]))).toBe("product");
|
|
});
|
|
});
|
|
|
|
describe("isLinked", () => {
|
|
it("HAM-002 with its own unshared SKU ham-002 is not linked", () => {
|
|
const sku = skuView({ code: "ham-002", variations: [{ id: 1, code: "ham-002", variant: "", variantName: "", weight: 1, usedBy: ["HAM-002"], onHand: 3 }] });
|
|
expect(isLinked(item({ code: "HAM-002" }), [sku])).toBe(false);
|
|
});
|
|
|
|
it("a SKU used by another Sales Item is linked", () => {
|
|
const sku = skuView({ code: "P-001", variations: [{ id: 1, code: "P-001", variant: "", variantName: "", weight: 1, usedBy: ["P-001", "P-SET-1"], onHand: 0 }] });
|
|
expect(isLinked(item({ code: "P-001" }), [sku])).toBe(true);
|
|
});
|
|
|
|
it("a SKU with a different code is linked", () => {
|
|
expect(isLinked(item({ code: "P-001" }), [skuView()])).toBe(true);
|
|
});
|
|
|
|
it("two SKUs are linked", () => {
|
|
const a = skuView({ code: "P-001" });
|
|
const b = skuView({ id: 10, code: "BA-001" });
|
|
expect(isLinked(item({ code: "P-001" }), [a, b])).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("buildSave compare-at price", () => {
|
|
const run = (compareAt: string, extra: PriceDraft[]) => {
|
|
const { item: d, sku } = owned(sizes([sized("M")]), { compareAtPrice: compareAt });
|
|
const prices = [price(d.variations[0].key, "100"), ...extra.map((p) => ({ ...p, variationKey: d.variations[0].key }))];
|
|
return buildSave({ type: "product", item: d, saved: emptySalesItem(), prices, sku, linked: false, today: TODAY, t: tEn });
|
|
};
|
|
|
|
it("an ended higher member price does not block", () => {
|
|
const out = run("120", [price("", "500", { effectiveFrom: "2026-09-01", effectiveTo: "2026-10-01" })]);
|
|
expect(out.errors).toEqual({});
|
|
});
|
|
|
|
it("an active event price above it blocks", () => {
|
|
const out = run("120", [price("", "150", { priceType: "EVENT" })]);
|
|
expect(out.body).toBeNull();
|
|
expect(out.errors["salesItem.compareAtPrice"]).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe("compareAtError per-country prices", () => {
|
|
it("checks the compare-at only against MYR prices, ignoring a higher SGD row", () => {
|
|
const myr = price("k", "80");
|
|
const sgd = { ...price("k", "150"), ...emptyPrice("k", { id: "2", code: "SG", currency: "SGD", isDefault: false }), priceType: "MEMBER", price: "150", effectiveFrom: TODAY };
|
|
expect(compareAtError(item({ compareAtPrice: "100" }), [myr, sgd], TODAY, tEn)).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("buildSave suggested variant codes", () => {
|
|
const build = (sku: SkuDraft, itemCode: string) => {
|
|
const out = buildSave({ type: "product", item: item({ code: itemCode }), saved: emptySalesItem(), prices: [], sku, linked: false, today: TODAY, t: tEn });
|
|
return out.body?.sku?.variations?.map((v) => v.code);
|
|
};
|
|
const allShipped = (codes: string[]) => sizes(codes.map((c, i) => sized(["M", "L"][i], { code: c })));
|
|
|
|
it("suggested codes follow a changed product code", () => {
|
|
expect(build(allShipped(["YELLOW-002-M", "YELLOW-002-L"]), "RED-9")).toEqual(["RED-9-M", "RED-9-L"]);
|
|
});
|
|
|
|
it("a hand-typed code of an unsaved variant is replaced by the suggestion", () => {
|
|
expect(build(allShipped(["YELLOW-002-M", "MINE"]), "RED-9")).toEqual(["RED-9-M", "RED-9-L"]);
|
|
});
|
|
|
|
it("clearing the product code and typing a new one still moves the suggested codes", () => {
|
|
const blank = ownedSku(allShipped(["YELLOW-002-M", "YELLOW-002-L"]), item({ code: "" }));
|
|
expect(ownedSku(blank, item({ code: "RED-9" })).variants.map((v) => v.code)).toEqual(["RED-9-M", "RED-9-L"]);
|
|
});
|
|
|
|
it("options entered before the code get codes once it is typed", () => {
|
|
const sku = { ...allShipped(["", ""]), code: "" };
|
|
expect(build(sku, "RED-9")).toEqual(["RED-9-M", "RED-9-L"]);
|
|
});
|
|
});
|
|
|
|
describe("syncPrices", () => {
|
|
const v = (key: string) => ({ key, variant: "", variantName: "", removable: true, components: [] });
|
|
|
|
it("drops rows of a variation that is gone", () => {
|
|
expect(syncPrices([price("a"), price("b")], [], [v("a"), v("b")], [v("a")])).toEqual([price("a")]);
|
|
});
|
|
|
|
it("gives a variation that comes back its saved rows", () => {
|
|
expect(syncPrices([], [price("a", "10.00")], [], [v("a")])).toEqual([price("a", "10.00")]);
|
|
});
|
|
|
|
it("never brings back saved rows the admin deleted from a variation that stayed", () => {
|
|
expect(syncPrices([], [price("a", "10.00")], [v("a")], [v("a")])).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Moved from the old stepper test module — verbatim except import lines
|
|
// ---------------------------------------------------------------------------
|
|
|
|
import { checklistRows, defaultCountryId, hasOpenSlotRow, sellableMarkets, type PriceMarket, withTakeOver, quickPrice, quickTypes, setComponentQianyi, setQuickPrice, todayKL, unpricedVariationKeys, variationsFromChecklist } from "./productPage";
|
|
|
|
const STEPPER_TODAY = "2026-10-07";
|
|
const MY: PriceMarket = { id: "2", code: "MY", currency: "MYR", isDefault: true };
|
|
const SG: PriceMarket = { id: "9", code: "SG", currency: "SGD", isDefault: false };
|
|
const stepperVariation = (key: string, id?: number, skuVariationId = 1, removable = true) => ({
|
|
id, key, variant: "Colour", variantName: key, removable,
|
|
components: [{ skuVariationId, skuVariationCode: `V${skuVariationId}`, label: "", qty: "1", qtyFoc: "0", qyTouched: false, skuId: 9 }],
|
|
});
|
|
const stepperItem = (over: Partial<SalesItemDraft> = {}): SalesItemDraft => ({ ...emptySalesItem(), ...over });
|
|
const stepperPrice = (key: string, over: Partial<PriceDraft> = {}): PriceDraft =>
|
|
({ ...emptyPrice(key), priceType: "MEMBER", price: "59.90", effectiveFrom: "2026-09-01", ...over });
|
|
const stepperSku: SkuView = { id: 9, code: "BA-002", name: "Romper", status: "Y", variations: [
|
|
{ id: 1, code: "BA-002-RED", variant: "Colour", variantName: "Red", weight: 0.4, usedBy: [], onHand: 0 },
|
|
{ id: 2, code: "BA-002-BLUE", variant: "Colour", variantName: "Blue", weight: 0.4, usedBy: [], onHand: 0 },
|
|
{ id: 3, code: "BA-002-GRN", variant: "Colour", variantName: "Green", weight: 0.4, usedBy: [], onHand: 0 },
|
|
] };
|
|
|
|
describe("todayKL", () => {
|
|
it("uses Kuala Lumpur, not UTC", () => {
|
|
expect(todayKL(new Date("2026-10-06T17:30:00Z"))).toBe("2026-10-07"); // 01:30 MYT
|
|
});
|
|
});
|
|
|
|
describe("checklist", () => {
|
|
it("ticks every variant of a new item", () => {
|
|
expect(checklistRows(stepperSku, stepperItem(), tEn).map((r) => r.ticked)).toEqual([true, true, true]);
|
|
});
|
|
it("on a saved item ticks sold variants, tags unsold ones new, locks used ones", () => {
|
|
const d = stepperItem({ id: 1, variations: [stepperVariation("a", 11, 1, false), stepperVariation("b", 12, 2)] });
|
|
const rows = checklistRows(stepperSku, d, tEn);
|
|
expect(rows.map((r) => [r.ticked, r.locked, r.isNew])).toEqual([[true, true, false], [true, false, false], [false, false, true]]);
|
|
});
|
|
it("re-ticking an unticked saved variant restores its saved variation", () => {
|
|
const saved = stepperItem({ id: 1, variations: [stepperVariation("a", 11, 1), stepperVariation("b", 12, 2)] });
|
|
const unticked = { ...saved, variations: [saved.variations[1]] };
|
|
const out = variationsFromChecklist(stepperSku, unticked, new Set([1, 2]), saved);
|
|
expect(out.map((v) => [v.id, v.key])).toEqual([[11, "a"], [12, "b"]]);
|
|
});
|
|
it("keeps saved variations and adds ticked new ones in SKU order", () => {
|
|
const d = stepperItem({ id: 1, variations: [stepperVariation("b", 12, 2)] });
|
|
const out = variationsFromChecklist(stepperSku, d, new Set([1, 2]));
|
|
expect(out.map((v) => [v.id, v.components[0].skuVariationId, v.components[0].qty])).toEqual([[undefined, 1, "1"], [12, 2, "1"]]);
|
|
expect(out[0].variantName).toBe("Red");
|
|
});
|
|
});
|
|
|
|
describe("quick prices", () => {
|
|
it("reads the open MEMBER MYR row and skips a regional one", () => {
|
|
expect(quickPrice([stepperPrice("a", { region: "5", price: "1" }), stepperPrice("a")], "a", "MEMBER", STEPPER_TODAY, MY)?.price).toBe("59.90");
|
|
});
|
|
it("with several current rows the slot is the empty-country one, then one in the market country; a row of another country is not the slot", () => {
|
|
const nulled = stepperPrice("a", { price: "10" });
|
|
const two = stepperPrice("a", { price: "20", country: "2" });
|
|
const three = stepperPrice("a", { price: "30", country: "3" });
|
|
expect(quickPrice([two, nulled], "a", "MEMBER", STEPPER_TODAY, MY)).toBe(nulled);
|
|
expect(quickPrice([three, two], "a", "MEMBER", STEPPER_TODAY, MY)).toBe(two);
|
|
expect(quickPrice([three, two], "a", "MEMBER", STEPPER_TODAY, { ...MY, id: "9" })).toBeUndefined();
|
|
});
|
|
it("withTakeOver ends the current row the day before the earliest active later row, without mutating", () => {
|
|
const current = stepperPrice("a", { id: 5, country: "" });
|
|
const first = stepperPrice("a", { country: "2", effectiveFrom: "2026-11-15", later: true });
|
|
const second = stepperPrice("a", { country: "2", effectiveFrom: "2026-11-01", later: true });
|
|
const off = stepperPrice("a", { country: "2", effectiveFrom: "2026-10-10", status: "N" });
|
|
const rows = [current, first, second, off];
|
|
const out = withTakeOver(rows, STEPPER_TODAY, MY);
|
|
expect(out[0]).toMatchObject({ id: 5, effectiveTo: "2026-10-31" });
|
|
expect(out.slice(1)).toEqual(rows.slice(1));
|
|
expect(current.effectiveTo).toBe("");
|
|
});
|
|
it("withTakeOver ignores a future row of another region, currency or country", () => {
|
|
const current = stepperPrice("a", { id: 5, country: "" });
|
|
for (const other of [{ region: "5" }, { currency: "USD" }, { country: "3" }]) {
|
|
const later = stepperPrice("a", { country: "2", effectiveFrom: "2026-11-01", later: true, ...other });
|
|
expect(withTakeOver([current, later], STEPPER_TODAY, MY)[0]).toBe(current);
|
|
}
|
|
});
|
|
it("withTakeOver only shortens: an earlier end, another type, or no later row leaves rows unchanged", () => {
|
|
const ends = stepperPrice("a", { effectiveTo: "2026-10-20" });
|
|
const later = stepperPrice("a", { effectiveFrom: "2026-11-01", later: true });
|
|
const rows = [ends, stepperPrice("a", { priceType: "EVENT" }), later];
|
|
const out = withTakeOver(rows, STEPPER_TODAY, MY);
|
|
expect(out[0]).toBe(ends);
|
|
expect(out[1]).toBe(rows[1]);
|
|
const longer = stepperPrice("a", { effectiveTo: "2026-12-31" });
|
|
expect(withTakeOver([longer, later], STEPPER_TODAY, MY)[0].effectiveTo).toBe("2026-10-31");
|
|
const alone = [stepperPrice("a")];
|
|
expect(withTakeOver(alone, STEPPER_TODAY, MY)[0]).toBe(alone[0]);
|
|
});
|
|
it("a later row is never the slot", () => {
|
|
expect(quickPrice([stepperPrice("a", { later: true })], "a", "MEMBER", STEPPER_TODAY, MY)).toBeUndefined();
|
|
});
|
|
it("a changed price ends the old row yesterday and adds one from today", () => {
|
|
const out = setQuickPrice([stepperPrice("a", { id: 5 })], "a", "MEMBER", "49.90", STEPPER_TODAY, MY);
|
|
expect(out).toHaveLength(2);
|
|
expect(out[0]).toMatchObject({ id: 5, effectiveTo: "2026-10-06", price: "59.90" });
|
|
expect(out[1]).toMatchObject({ variationKey: "a", priceType: "MEMBER", currency: "MYR", price: "49.90", effectiveFrom: STEPPER_TODAY, status: "Y" });
|
|
});
|
|
it("a row that starts today is updated in place, never ended before it starts", () => {
|
|
const out = setQuickPrice([stepperPrice("a", { id: 5, effectiveFrom: STEPPER_TODAY })], "a", "MEMBER", "49.90", STEPPER_TODAY, MY);
|
|
expect(out).toEqual([expect.objectContaining({ id: 5, price: "49.90", effectiveTo: "" })]);
|
|
});
|
|
it("an unsaved row is updated in place", () => {
|
|
const out = setQuickPrice([stepperPrice("a")], "a", "MEMBER", "10", STEPPER_TODAY, MY);
|
|
expect(out).toHaveLength(1);
|
|
expect(out[0].price).toBe("10");
|
|
});
|
|
it("adds a row when none exists, and ignores a blank or unchanged price", () => {
|
|
expect(setQuickPrice([], "a", "EVENT", "39.90", STEPPER_TODAY, MY)).toEqual([expect.objectContaining({ priceType: "EVENT", effectiveFrom: STEPPER_TODAY })]);
|
|
const rows = [stepperPrice("a", { id: 5 })];
|
|
expect(setQuickPrice(rows, "a", "MEMBER", "", STEPPER_TODAY, MY)).toBe(rows);
|
|
expect(setQuickPrice(rows, "a", "MEMBER", "59.90", STEPPER_TODAY, MY)).toBe(rows);
|
|
});
|
|
it("reads a current price that has an end date, and a change keeps that end", () => {
|
|
const dated = [stepperPrice("a", { id: 5, effectiveTo: "2026-12-31" })];
|
|
expect(quickPrice(dated, "a", "MEMBER", STEPPER_TODAY, MY)?.id).toBe(5);
|
|
const out = setQuickPrice(dated, "a", "MEMBER", "49.90", STEPPER_TODAY, MY);
|
|
expect(out).toEqual([
|
|
expect.objectContaining({ id: 5, effectiveTo: "2026-10-06" }),
|
|
expect.objectContaining({ price: "49.90", effectiveFrom: STEPPER_TODAY, effectiveTo: "2026-12-31" }),
|
|
]);
|
|
});
|
|
it("a new price does not stop before a later price of another country", () => {
|
|
const other = stepperPrice("a", { id: 7, country: "3", effectiveFrom: "2026-11-01", price: "45" });
|
|
const out = setQuickPrice([stepperPrice("a", { id: 5, country: "" }), other], "a", "MEMBER", "50", STEPPER_TODAY, MY);
|
|
expect(out[2]).toMatchObject({ price: "50", effectiveTo: "" });
|
|
const same = stepperPrice("a", { id: 7, country: "2", effectiveFrom: "2026-11-01", price: "45" });
|
|
expect(setQuickPrice([stepperPrice("a", { id: 5, country: "" }), same], "a", "MEMBER", "50", STEPPER_TODAY, MY)[2]).toMatchObject({ effectiveTo: "2026-10-31" });
|
|
});
|
|
it("a new price stops the day before a scheduled later price", () => {
|
|
const out = setQuickPrice([stepperPrice("a", { id: 7, effectiveFrom: "2026-11-01", price: "45" })], "a", "MEMBER", "50", STEPPER_TODAY, MY);
|
|
expect(out[1]).toMatchObject({ price: "50", effectiveFrom: STEPPER_TODAY, effectiveTo: "2026-10-31" });
|
|
});
|
|
});
|
|
|
|
describe("inline QianYi", () => {
|
|
const linked: SkuView = {
|
|
...stepperSku,
|
|
variations: [{ ...stepperSku.variations[0], qySku: "Q1", qyTitle: "Red bra", qyLinkSource: "AUTO" }, ...stepperSku.variations.slice(1)],
|
|
};
|
|
|
|
it("a row shows the SKU variant's link until the draft touches it", () => {
|
|
const d = stepperItem({ id: 1, variations: [stepperVariation("a", 11, 1), stepperVariation("b", 12, 2)] });
|
|
expect(checklistRows(linked, d, tEn)[0]).toMatchObject({ qySku: "Q1", qyTitle: "Red bra", qyLinkSource: "AUTO" });
|
|
expect(checklistRows(linked, setComponentQianyi(d, 1, undefined), tEn)[0]).toMatchObject({ qySku: null, qyLinkSource: null });
|
|
expect(checklistRows(linked, setComponentQianyi(d, 2, "Q9", "Blue bra"), tEn)[1]).toMatchObject({
|
|
qySku: "Q9", qyTitle: "Blue bra", qyStatus: "OK", qyLinkSource: "MANUAL",
|
|
});
|
|
});
|
|
|
|
it("links every component of that SKU variant and nothing else", () => {
|
|
const d = stepperItem({ variations: [stepperVariation("a", 11, 1), stepperVariation("b", 12, 2), stepperVariation("c", 13, 1)] });
|
|
const out = setComponentQianyi(d, 1, "Q1", "Red");
|
|
expect(out.variations.map((v) => [v.components[0].qySku, v.components[0].qyTouched])).toEqual([["Q1", true], [undefined, false], ["Q1", true]]);
|
|
});
|
|
});
|
|
|
|
describe("buildSave draft without an image", () => {
|
|
it("an Inactive draft saves without an image; Active still needs one", () => {
|
|
const sku = sizes([sized("M"), sized("L")]);
|
|
const args = { type: "product" as const, saved: emptySalesItem(), prices: [], sku, linked: false, today: TODAY, t: tEn };
|
|
expect(buildSave({ ...args, item: item({ images: [] }) }).errors["salesItem.images"]).toBeUndefined();
|
|
expect(buildSave({ ...args, item: item({ images: [], status: "Y" }) }).errors["salesItem.images"]).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe("price columns follow the price types", () => {
|
|
const types = [
|
|
{ code: "EVENT", status: "Y" },
|
|
{ code: "ZZ_QA_TEST", status: "N" },
|
|
{ code: "MEMBER", status: "Y" },
|
|
{ code: "WHOLESALE", status: "Y" },
|
|
];
|
|
|
|
it("columns are the active price types, MEMBER first", () => {
|
|
expect(quickTypes(types)).toEqual(["MEMBER", "EVENT", "WHOLESALE"]);
|
|
});
|
|
|
|
it("a third price type gets its own quick row", () => {
|
|
const rows = setQuickPrice([], "a", "WHOLESALE", "20", STEPPER_TODAY, MY);
|
|
expect(rows).toEqual([expect.objectContaining({ priceType: "WHOLESALE", currency: "MYR", effectiveFrom: STEPPER_TODAY })]);
|
|
expect(quickPrice(rows, "a", "WHOLESALE", STEPPER_TODAY, MY)?.price).toBe("20");
|
|
});
|
|
|
|
it("only a selling (MEMBER) price counts towards activation", () => {
|
|
const d = stepperItem({ variations: [stepperVariation("a", 11, 1)] });
|
|
expect(unpricedVariationKeys([stepperPrice("a", { priceType: "EVENT" })], d, STEPPER_TODAY)).toEqual(["a"]);
|
|
expect(unpricedVariationKeys([stepperPrice("a")], d, STEPPER_TODAY)).toEqual([]);
|
|
expect(unpricedVariationKeys([stepperPrice("a", { region: "5" })], d, STEPPER_TODAY)).toEqual(["a"]);
|
|
});
|
|
});
|
|
|
|
describe("price-list-rows rules", () => {
|
|
const node = (over: Record<string, unknown>) => ({ id: 1, code: "MY", name: "Malaysia", type: "C", status: "Y", parent: null, ...over }) as never;
|
|
|
|
it("defaultCountryId picks the active MY country, ignoring case, inactive, regions and other codes", () => {
|
|
expect(defaultCountryId([node({ id: 9, code: "SG" }), node({ id: 3, status: "N" }), node({ id: 4, type: "R" }), node({ id: 2, code: "my" })])).toBe("2");
|
|
expect(defaultCountryId([node({ id: 3, status: "N" }), node({ id: 9, code: "SG" })])).toBe("");
|
|
expect(defaultCountryId([])).toBe("");
|
|
});
|
|
|
|
it("setQuickPrice adds a MYR row with the given country and no region", () => {
|
|
expect(setQuickPrice([], "a", "MEMBER", "10", STEPPER_TODAY, MY)).toEqual([
|
|
expect.objectContaining({ currency: "MYR", country: "2", region: "" }),
|
|
]);
|
|
});
|
|
|
|
it("a current MEMBER row with the default country fills the slot, and so does a NULL-country row", () => {
|
|
expect(quickPrice([stepperPrice("a", { country: "2" })], "a", "MEMBER", STEPPER_TODAY, MY)?.price).toBe("59.90");
|
|
expect(quickPrice([stepperPrice("a", { country: "" })], "a", "MEMBER", STEPPER_TODAY, MY)?.price).toBe("59.90");
|
|
});
|
|
|
|
it("validatePrices blocks an unsaved row with no country, not a saved one", () => {
|
|
const d = stepperItem({ variations: [stepperVariation("a", 11, 1)] });
|
|
const row = stepperPrice("a");
|
|
expect(validatePrices([row], d, tEn, STEPPER_TODAY).pricing).toBe("Malaysia is missing from Locations");
|
|
expect(validatePrices([{ ...row, id: 5 }], d, tEn, STEPPER_TODAY).pricing).toBeUndefined();
|
|
expect(validatePrices([{ ...row, country: "2" }], d, tEn, STEPPER_TODAY).pricing).toBeUndefined();
|
|
});
|
|
|
|
it("a later selling price is enough to activate; an ended or Inactive one is not", () => {
|
|
const d = stepperItem({ variations: [stepperVariation("a", 11, 1)] });
|
|
expect(unpricedVariationKeys([stepperPrice("a", { effectiveFrom: "2026-11-01" })], d, STEPPER_TODAY)).toEqual([]);
|
|
expect(unpricedVariationKeys([stepperPrice("a", { effectiveFrom: "2026-09-01", effectiveTo: "2026-10-01" })], d, STEPPER_TODAY)).toEqual(["a"]);
|
|
expect(unpricedVariationKeys([stepperPrice("a", { effectiveFrom: "2026-11-01", status: "N" })], d, STEPPER_TODAY)).toEqual(["a"]);
|
|
expect(unpricedVariationKeys([stepperPrice("a", { effectiveFrom: "2026-11-01", priceType: "EVENT" })], d, STEPPER_TODAY)).toEqual(["a"]);
|
|
expect(unpricedVariationKeys([stepperPrice("a", { effectiveFrom: "2026-11-01", price: " " })], d, STEPPER_TODAY)).toEqual(["a"]);
|
|
});
|
|
|
|
it("addPrice appends one Active MYR row with the given dates; a blank price adds nothing", () => {
|
|
expect(addPrice([], "a", "MEMBER", " ", "2026-11-01", "", MY)).toEqual([]);
|
|
expect(addPrice([], "a", "MEMBER", "119", "2026-11-01", "2026-12-31", MY)).toEqual([
|
|
{ variationKey: "a", currency: "MYR", priceType: "MEMBER", country: "2", region: "", price: "119", status: "Y", effectiveFrom: "2026-11-01", effectiveTo: "2026-12-31" },
|
|
]);
|
|
});
|
|
|
|
it("an unsaved variant code is always the suggestion; saved codes stay", () => {
|
|
const sku = sizes([sized("M", { code: "MINE" }), sized("L", { id: 5, code: "OLD-L" })]);
|
|
const out = ownedSku(sku, item({ code: "YELLOW-002" }));
|
|
expect(out.variants.map((v) => v.code)).toEqual(["YELLOW-002-M", "OLD-L"]);
|
|
});
|
|
|
|
it("changing the product code renames unsaved variant codes", () => {
|
|
const out = ownedSku(sizes([sized("M")]), item({ code: "YELLOW-003" }));
|
|
expect(out.variants[0].code).toBe("YELLOW-003-M");
|
|
});
|
|
});
|
|
|
|
describe("country price setup: per-market slots", () => {
|
|
const node = (over: Record<string, unknown>) => ({ id: 1, code: "MY", name: "Malaysia", type: "C", status: "Y", parent: null, ...over }) as never;
|
|
const countries = [{ code: "MY", name: "Malaysia", currency: "MYR" }, { code: "SG", name: "Singapore", currency: "SGD" }];
|
|
const sgRow = (over: Partial<PriceDraft> = {}) => stepperPrice("a", { country: "9", currency: "SGD", price: "20.00", ...over });
|
|
|
|
it("sellableMarkets maps each sellable code to its active country node, MY default, dropping codes with no node", () => {
|
|
const locations = [node({ id: 2, code: "my" }), node({ id: 9, code: "SG" }), node({ id: 4, code: "SG", status: "N" }), node({ id: 5, code: "SG", type: "R" })];
|
|
expect(sellableMarkets(locations, countries)).toEqual([
|
|
{ id: "2", code: "MY", currency: "MYR", isDefault: true },
|
|
{ id: "9", code: "SG", currency: "SGD", isDefault: false },
|
|
]);
|
|
expect(sellableMarkets([node({ id: 2 })], countries).map((m) => m.code)).toEqual(["MY"]);
|
|
});
|
|
|
|
it("sellableMarkets with an empty country list falls back to Malaysia only", () => {
|
|
expect(sellableMarkets([node({ id: 2 }), node({ id: 9, code: "SG" })], [])).toEqual([{ id: "2", code: "MY", currency: "MYR", isDefault: true }]);
|
|
expect(sellableMarkets([], [])).toEqual([{ id: "", code: "MY", currency: "MYR", isDefault: true }]);
|
|
});
|
|
|
|
it("MY and SG MEMBER rows are different slots", () => {
|
|
const my = stepperPrice("a", { id: 1, country: "2" });
|
|
const sg = sgRow({ id: 2 });
|
|
expect(quickPrice([my, sg], "a", "MEMBER", STEPPER_TODAY, MY)).toBe(my);
|
|
expect(quickPrice([my, sg], "a", "MEMBER", STEPPER_TODAY, SG)).toBe(sg);
|
|
expect(quickPrice([my], "a", "MEMBER", STEPPER_TODAY, SG)).toBeUndefined();
|
|
expect(quickPrice([sg], "a", "MEMBER", STEPPER_TODAY, MY)).toBeUndefined();
|
|
});
|
|
|
|
it("a blank-country row belongs to the default market only", () => {
|
|
const blank = stepperPrice("a", { country: "" });
|
|
expect(quickPrice([blank], "a", "MEMBER", STEPPER_TODAY, MY)).toBe(blank);
|
|
expect(quickPrice([blank], "a", "MEMBER", STEPPER_TODAY, SG)).toBeUndefined();
|
|
expect(quickPrice([{ ...blank, currency: "SGD" }], "a", "MEMBER", STEPPER_TODAY, SG)).toBeUndefined();
|
|
});
|
|
|
|
it("setQuickPrice for SG adds an SGD row with the SG country and leaves the MY row untouched", () => {
|
|
const my = stepperPrice("a", { id: 1, country: "2" });
|
|
const out = setQuickPrice([my], "a", "MEMBER", "20", STEPPER_TODAY, SG);
|
|
expect(out).toHaveLength(2);
|
|
expect(out[0]).toBe(my);
|
|
expect(out[1]).toMatchObject({ variationKey: "a", priceType: "MEMBER", currency: "SGD", country: "9", region: "", price: "20", status: "Y", effectiveFrom: STEPPER_TODAY });
|
|
});
|
|
|
|
it("setQuickPrice for SG edits the SG row, not the MY one", () => {
|
|
const my = stepperPrice("a", { id: 1, country: "2" });
|
|
const sg = sgRow({ id: 2, effectiveFrom: STEPPER_TODAY });
|
|
const out = setQuickPrice([my, sg], "a", "MEMBER", "25", STEPPER_TODAY, SG);
|
|
expect(out[0]).toBe(my);
|
|
expect(out[1]).toMatchObject({ price: "25", currency: "SGD", country: "9" });
|
|
});
|
|
|
|
it("addPrice for SG appends an SGD row in the SG country", () => {
|
|
expect(addPrice([], "a", "MEMBER", "30", "2026-11-01", "", SG)).toEqual([
|
|
{ variationKey: "a", currency: "SGD", priceType: "MEMBER", country: "9", region: "", price: "30", status: "Y", effectiveFrom: "2026-11-01", effectiveTo: "" },
|
|
]);
|
|
});
|
|
|
|
it("hasOpenSlotRow is per market", () => {
|
|
const my = stepperPrice("a", { country: "2" });
|
|
expect(hasOpenSlotRow([my], "a", "MEMBER", STEPPER_TODAY, MY)).toBe(true);
|
|
expect(hasOpenSlotRow([my], "a", "MEMBER", STEPPER_TODAY, SG)).toBe(false);
|
|
expect(hasOpenSlotRow([sgRow()], "a", "MEMBER", STEPPER_TODAY, SG)).toBe(true);
|
|
});
|
|
|
|
it("withTakeOver of a later SG price does not end the MY row", () => {
|
|
const my = stepperPrice("a", { id: 1, country: "2" });
|
|
const later = sgRow({ effectiveFrom: "2026-11-01", later: true });
|
|
expect(withTakeOver([my, later], STEPPER_TODAY, MY)[0]).toBe(my);
|
|
});
|
|
});
|