import { describe, it, expect, vi, beforeEach } from "vitest"; const client = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), })); vi.mock("@/app/lib/clientApi", () => ({ apiGet: client.apiGet, apiPost: client.apiPost, })); import { getProductOptions, listProducts, toggleProductStatus, uploadProductImage } from "./adminProduct"; import type { ProductOptions } from "@/app/lib/types"; beforeEach(() => { client.apiGet.mockReset(); client.apiPost.mockReset(); }); const listPage = { content: [ { id: "1", productCode: "SKU-1", productName: "Widget", productBrand: "NIKE", productCategory: "Footwear", productStatus: "Y", sellingPrice: 19.9, }, ], number: 0, size: 20, totalElements: 1, totalPages: 1, }; describe("listProducts", () => { it("GETs /api/products and normalizes the paged result", async () => { client.apiGet.mockResolvedValue(listPage); const result = await listProducts({ page: 0, pageSize: 20 }); expect(client.apiGet).toHaveBeenCalledWith("/api/products?page=0&size=20"); expect(result).toEqual({ items: listPage.content, total: 1 }); }); it("defaults to no query params when none are given", async () => { client.apiGet.mockResolvedValue({ content: [], number: 0, size: 20, totalElements: 0, totalPages: 0 }); await listProducts(); expect(client.apiGet).toHaveBeenCalledWith("/api/products"); }); it("sends q, status, brand and category when set", async () => { client.apiGet.mockResolvedValue({ content: [], totalElements: 0 }); await listProducts({ page: 1, pageSize: 20, q: "a&b", status: "Y", brand: "NIKE", category: "FW" }); expect(client.apiGet).toHaveBeenCalledWith("/api/products?page=1&size=20&q=a%26b&status=Y&brand=NIKE&category=FW"); }); }); describe("toggleProductStatus", () => { it("POSTs to /api/products/{id} with no body and returns the toggled view", async () => { client.apiPost.mockResolvedValue({ id: 1, status: "N" }); const result = await toggleProductStatus("1"); expect(client.apiPost).toHaveBeenCalledWith("/api/products/1"); expect(result).toEqual({ id: 1, status: "N" }); }); }); describe("getProductOptions", () => { it("GETs /api/products/options", async () => { const options: ProductOptions = { brands: [{ id: "b1", code: "NIKE", status: "Y" }], categories: [{ id: "c1", code: "Footwear", status: "Y" }], priceTypes: [{ id: "p1", code: "Retail", status: "Y" }], suppliers: [{ id: "s1", code: "SUP01", status: "Y" }], }; client.apiGet.mockResolvedValue(options); const result = await getProductOptions(); expect(client.apiGet).toHaveBeenCalledWith("/api/products/options"); expect(result).toEqual(options); }); }); describe("uploadProductImage", () => { it("POSTs { fileName, fileType, base } to /api/products/image", async () => { client.apiPost.mockResolvedValue({ url: "https://cdn/pub/bucket/a.png" }); const result = await uploadProductImage({ fileName: "a.png", fileType: "image/png", base: "QUJD" }); expect(client.apiPost).toHaveBeenCalledWith("/api/products/image", { fileName: "a.png", fileType: "image/png", base: "QUJD", }); expect(result).toEqual({ url: "https://cdn/pub/bucket/a.png" }); }); });