import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), ApiError: class ApiError extends Error { status: number; data?: unknown; constructor(message: string, status: number, data?: unknown) { super(message); this.name = "ApiError"; this.status = status; this.data = data; } } })); vi.mock("@/app/lib/clientApi", () => ({ apiGet: mocks.apiGet, apiPost: mocks.apiPost, ApiError: mocks.ApiError, })); import { approveRefund, getRefund, listRefunds, retryRefundEmail, rejectRefund } from "./adminRefunds"; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPost.mockReset(); }); describe("listRefunds", () => { it("GETs the list route with status and pagination", async () => { const page = { content: [], totalElements: 0, page: 0, size: 20, pendingCount: 0 }; mocks.apiGet.mockResolvedValue(page); await expect(listRefunds({ status: "P" })).resolves.toBe(page); expect(mocks.apiGet).toHaveBeenCalledWith("/api/refunds?status=P&page=0&size=20"); }); it("omits empty text filters from the query, but still sets default page/size", async () => { mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0, page: 0, size: 20, pendingCount: 0 }); await listRefunds({ requestNo: "", invoiceNo: "", member: "" }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/refunds?page=0&size=20"); }); it("forwards all filter params", async () => { mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0, page: 0, size: 20, pendingCount: 0 }); await listRefunds({ status: "P", requestNo: "RR0001", invoiceNo: "IN0002", member: "MY6217232", from: "2024-01-01", to: "2024-12-31", page: 1, size: 10, }); expect(mocks.apiGet).toHaveBeenCalledWith( "/api/refunds?status=P&requestNo=RR0001&invoiceNo=IN0002&member=MY6217232&from=2024-01-01&to=2024-12-31&page=1&size=10", ); }); }); describe("getRefund", () => { it("GETs the detail route", async () => { const detail = { request: {}, buyerName: null, orderDate: null, grandTotal: 0, shippingCost: 0, tenders: [], stockEffects: [] }; mocks.apiGet.mockResolvedValue(detail); await expect(getRefund("R-1")).resolves.toBe(detail); expect(mocks.apiGet).toHaveBeenCalledWith("/api/refunds/R-1"); }); it("passes through a genuine upstream 404 as ApiError", async () => { mocks.apiGet.mockRejectedValue(new mocks.ApiError("Not found", 404)); await expect(getRefund("NOPE")).rejects.toThrow("Not found"); }); }); describe("approveRefund", () => { it("POSTs the uuid (a mutation — never a GET)", async () => { const row = { uuid: "R-1", status: "A" }; mocks.apiPost.mockResolvedValue(row); await expect(approveRefund("R-1")).resolves.toBe(row); expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/approve", {}); }); it("drops a doneBy field from the payload", async () => { const row = { uuid: "R-1", status: "A" }; mocks.apiPost.mockResolvedValue(row); // Even if note has doneBy, the service strips it await expect(approveRefund("R-1", { note: "OK" })).resolves.toBe(row); expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/approve", { note: "OK" }); }); }); describe("rejectRefund", () => { it("POSTs the reason (a mutation — never a GET)", async () => { const row = { uuid: "R-1", status: "R" }; mocks.apiPost.mockResolvedValue(row); await expect(rejectRefund("R-1", "Duplicate")).resolves.toBe(row); expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/reject", { reason: "Duplicate" }); }); }); describe("retryRefundEmail", () => { it("POSTs an empty body to retryEmail (a mutation — never a GET)", async () => { const result = { uuid: "R-1", emailSentAt: "2024-01-01" }; mocks.apiPost.mockResolvedValue(result); await expect(retryRefundEmail("R-1")).resolves.toBe(result); expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/retry-email", {}); }); });