import { describe, it, expect, vi, beforeEach } from "vitest"; import { render, screen } from "@testing-library/react"; import type { AdminRefundDetail, AdminRefundRequest } from "@/app/lib/types"; import { DEFAULT_MESSAGES } from "@/app/lib/i18n/defaultMessages"; // add-admin-initiated-refund: the queue shows who raised a refund and how it was paid out. const svc = vi.hoisted(() => ({ getRefund: vi.fn(), approveRefund: vi.fn(), rejectRefund: vi.fn(), retryRefundEmail: vi.fn(), })); vi.mock("@/app/lib/services/adminRefunds", () => svc); import { RefundDrawer } from "./RefundDrawer"; import { payoutLabel } from "./refundLabels"; const t = (code: string, params?: Record) => { let s = DEFAULT_MESSAGES[code] ?? code; for (const [k, v] of Object.entries(params ?? {})) s = s.replace(`{${k}}`, String(v)); return s; }; function request(overrides: Partial = {}): AdminRefundRequest { return { uuid: "U-41", documentNo: "RR00000041", orderUuid: "O-9", orderDocumentNo: "IN00000080", buyerCode: "", status: "A", reasonCode: "DAMAGED", remark: null, refundAmount: 35, decisionReason: null, decidedBy: "Ops Admin", decidedAt: "2026-10-05T11:00:00", walletTrxnRef: null, stockDocumentNos: "AJI9", lastError: null, emailSentAt: null, createDate: "2026-10-05T11:00:00", lines: [{ orderDetailsId: 21, productName: "BA-001", qty: 1, netUnitPrice: 35, amount: 35 }], source: "ADMIN", payoutMethod: "MANUAL", payoutChannel: "CASH", payoutReference: null, windowOverrideReason: null, submittedBy: "Ops Admin", ...overrides, }; } function detail(req: AdminRefundRequest): AdminRefundDetail { return { request: req, buyerName: null, orderDate: "2026-10-02T10:00:00", grandTotal: 70, shippingCost: 0, tenders: [{ paymentType: "PC", amount: 70, reference: null }], stockEffects: [], }; } beforeEach(() => { Object.values(svc).forEach(fn => fn.mockReset()); }); describe("payoutLabel", () => { const base = request({ payoutMethod: "ACORNS", payoutChannel: null }); it("labels an Acorns payout", () => { expect(payoutLabel(t, base)).toBe("Acorns wallet"); }); it("labels a manual bank transfer with its reference", () => { expect(payoutLabel(t, { ...base, payoutMethod: "MANUAL", payoutChannel: "BANK_TRANSFER", payoutReference: "MBB-1" })) .toBe("Manual · Bank transfer · MBB-1"); }); it("treats an old row without payoutMethod as Acorns", () => { expect(payoutLabel(t, { ...base, payoutMethod: undefined })).toBe("Acorns wallet"); }); }); describe("RefundDrawer — admin-raised refunds", () => { it("shows the Admin tag, the raiser and the manual payout record", async () => { svc.getRefund.mockResolvedValue(detail(request())); render( {}} onChanged={() => {}} />); expect(await screen.findByText("Raised by Ops Admin")).toBeInTheDocument(); expect(screen.getByText("Admin")).toBeInTheDocument(); expect(screen.getByText("Returned manually RM 35.00 · Cash")).toBeInTheDocument(); expect(screen.queryByText(/Acorns credited/)).not.toBeInTheDocument(); }); it("shows a window override reason", async () => { svc.getRefund.mockResolvedValue(detail(request({ windowOverrideReason: "Warranty goodwill" }))); render( {}} onChanged={() => {}} />); expect(await screen.findByText("Warranty goodwill")).toBeInTheDocument(); }); it("previews a manual return instead of an Acorns credit while pending", async () => { svc.getRefund.mockResolvedValue(detail(request({ status: "P", decidedBy: null, decidedAt: null, stockDocumentNos: null }))); render( {}} onChanged={() => {}} />); expect(await screen.findByText("Return RM 35.00 to the buyer manually (Cash)")).toBeInTheDocument(); expect(screen.queryByText(/to Acorns/)).not.toBeInTheDocument(); }); it("keeps a member request's Acorns wording", async () => { svc.getRefund.mockResolvedValue(detail(request({ source: undefined, payoutMethod: undefined, payoutChannel: undefined, submittedBy: "MY3619941", buyerCode: "MY3619941", walletTrxnRef: "T-9", }))); render( {}} onChanged={() => {}} />); expect(await screen.findByText(/Acorns credited RM 35\.00/)).toBeInTheDocument(); expect(screen.queryByText("Raised by MY3619941")).not.toBeInTheDocument(); }); });