Files
co-lite/solvway-admin-portal/app/lib/services/adminProduct.test.ts
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

103 lines
3.2 KiB
TypeScript

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" });
});
});