import { beforeEach, describe, expect, it, vi } from "vitest"; const api = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn() })); vi.mock("@/app/lib/clientApi", () => api); import { getSalesItem, getSku, listSkus, saveCatalog, searchSkuVariations, findSkuVariationsByCode } from "./adminCatalogSetup"; beforeEach(() => { api.apiGet.mockReset(); api.apiPost.mockReset(); }); describe("adminCatalogSetup", () => { it("saves through POST /api/products", async () => { api.apiPost.mockResolvedValue({ sku: { id: 70 } }); const body = { sku: { code: "BA-010", name: "Romper", status: "Y", variations: [] } }; await saveCatalog(body as never); expect(api.apiPost).toHaveBeenCalledWith("/api/products", body); }); it("reads a Sales Item, a SKU, the SKU list and the variant options", async () => { api.apiGet.mockResolvedValue({ content: [{ id: 1 }], totalElements: 3 }); await getSalesItem(26); await getSku(16); const page = await listSkus({ page: 0, pageSize: 50 }); await searchSkuVariations("a&b"); expect(api.apiGet).toHaveBeenNthCalledWith(1, "/api/products/26"); expect(api.apiGet).toHaveBeenNthCalledWith(2, "/api/products/skus/16"); expect(api.apiGet).toHaveBeenNthCalledWith(3, "/api/products/skus?page=0&size=50"); expect(api.apiGet).toHaveBeenNthCalledWith(4, "/api/products/sku-variation-options?q=a%26b"); expect(page).toEqual({ items: [{ id: 1 }], total: 3 }); }); it("sends sortBy and sortDir to the SKU list only when set", async () => { api.apiGet.mockResolvedValue({ content: [], totalElements: 0 }); await listSkus({ page: 0, pageSize: 20, sortBy: "skuCode", sortDir: "desc" } as never); expect(api.apiGet).toHaveBeenLastCalledWith("/api/products/skus?page=0&size=20&sortBy=skuCode&sortDir=desc"); await listSkus({ page: 0, pageSize: 20 }); expect(api.apiGet).toHaveBeenLastCalledWith("/api/products/skus?page=0&size=20"); }); it("sends the SKU status filter", async () => { api.apiGet.mockResolvedValue({ content: [], totalElements: 0 }); await listSkus({ page: 0, pageSize: 20, q: "ba", status: "N" }); expect(api.apiGet).toHaveBeenLastCalledWith("/api/products/skus?page=0&size=20&q=ba&status=N"); }); it("sends the QianYi link filter only when set", async () => { api.apiGet.mockResolvedValue({ content: [], totalElements: 0 }); await listSkus({ page: 0, pageSize: 20, qyLink: "UNLINKED" }); expect(api.apiGet).toHaveBeenLastCalledWith("/api/products/skus?page=0&size=20&qyLink=UNLINKED"); await listSkus({ page: 0, pageSize: 20, qyLink: "" }); expect(api.apiGet).toHaveBeenLastCalledWith("/api/products/skus?page=0&size=20"); }); it("looks up SKU variants by exact scan code, encoded as one value", async () => { api.apiGet.mockResolvedValue([{ skuVariationId: 9 }]); const found = await findSkuVariationsByCode("TEST-003&BLUE x"); expect(api.apiGet).toHaveBeenCalledWith("/api/products/sku-variations-by-code?code=TEST-003%26BLUE%20x"); expect(found).toEqual([{ skuVariationId: 9 }]); }); });