import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn() })); vi.mock("@/app/lib/clientApi", () => ({ apiGet: (...a: unknown[]) => mocks.apiGet(...a), apiPost: vi.fn(), apiPut: vi.fn(), })); import { listCampaigns, listRedemptions, listVouchers } from "./adminPromotions"; const lastUrl = () => new URL(String(mocks.apiGet.mock.calls.at(-1)?.[0]), "http://x"); describe("BABYBOO-59 service filter params", () => { beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiGet.mockResolvedValue({ content: [] }); }); it("listCampaigns sends q, benefitType, claimType, channel, from, to when set", async () => { await listCampaigns(0, 20, { q: "summer sale", benefitType: "GIFT", claimType: "UNIQUE_CODE", channel: "EVENT", from: "2026-01-01", to: "2026-02-01", } as never); const u = lastUrl(); expect(u.pathname).toBe("/api/promotions/campaigns"); expect(Object.fromEntries(u.searchParams)).toMatchObject({ q: "summer sale", benefitType: "GIFT", claimType: "UNIQUE_CODE", channel: "EVENT", from: "2026-01-01", to: "2026-02-01", }); }); it("listCampaigns omits the new params when unset or empty", async () => { await listCampaigns(0, 20, { status: "ACTIVE", q: "", benefitType: undefined } as never); const u = lastUrl(); for (const k of ["q", "benefitType", "claimType", "channel", "from", "to"]) expect(u.searchParams.has(k)).toBe(false); expect(u.searchParams.get("status")).toBe("ACTIVE"); }); it("listRedemptions sends invoiceNo, member and channel when set", async () => { await listRedemptions(0, 20, { invoiceNo: "IN0001", member: "MY1", channel: "OUTLET" } as never); expect(Object.fromEntries(lastUrl().searchParams)).toMatchObject({ invoiceNo: "IN0001", member: "MY1", channel: "OUTLET" }); }); it("listRedemptions omits the new params when unset", async () => { await listRedemptions(0, 20, { status: "COMMITTED" }); const u = lastUrl(); for (const k of ["invoiceNo", "member", "channel"]) expect(u.searchParams.has(k)).toBe(false); }); it("listVouchers sends member, code and invoiceNo when set", async () => { await listVouchers("C-1", 0, 20, { member: "Irene", code: "AB12", invoiceNo: "IN2" } as never); const u = lastUrl(); expect(u.pathname).toBe("/api/promotions/campaigns/C-1/vouchers"); expect(Object.fromEntries(u.searchParams)).toMatchObject({ member: "Irene", code: "AB12", invoiceNo: "IN2" }); }); it("listVouchers omits the new params when unset", async () => { await listVouchers("C-1", 0, 20, { status: "USED" }); const u = lastUrl(); for (const k of ["member", "code", "invoiceNo"]) expect(u.searchParams.has(k)).toBe(false); }); });