Files
co-lite/solvway-admin-portal/app/orders/refunds/RefundDrawer.giftCard.test.tsx
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

184 lines
8.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { AdminRefundDetail, AdminRefundRequest } from "@/app/lib/types";
// add-gift-card-checkout: the drawer shows the gift card part of a refund and pays out only the rest.
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";
function request(overrides: Partial<AdminRefundRequest> = {}): AdminRefundRequest {
return {
uuid: "U-31",
documentNo: "RR00000031",
orderUuid: "O-1",
orderDocumentNo: "IN00000128",
buyerCode: "MY3619941",
status: "P",
reasonCode: "WRONG_ITEM",
remark: null,
refundAmount: 50,
decisionReason: null,
decidedBy: null,
decidedAt: null,
walletTrxnRef: null,
stockDocumentNos: null,
lastError: null,
emailSentAt: null,
createDate: "2026-10-06T10:12:00",
lines: [{ orderDetailsId: 11, productName: "Diaper L 40s", qty: 1, netUnitPrice: 50, amount: 50 }],
...overrides,
} as AdminRefundRequest;
}
function detail(req: AdminRefundRequest, tenders: AdminRefundDetail["tenders"]): AdminRefundDetail {
return {
request: req,
buyerName: "Tan Mei Ling",
orderDate: "2026-10-01T09:00:00",
grandTotal: 100,
shippingCost: 0,
tenders,
stockEffects: [
{ deliveryOrderNo: "DO0001", deliveryOrderType: "HDO", fulfilmentStatus: "DE", warehouseId: "2", effect: "RETURN_TO_SHELF" },
],
};
}
const CARD_AND_WALLET = [
{ paymentType: "GC", amount: 30, reference: "•••• P5R2" },
{ paymentType: "DWP", amount: 70, reference: "T-1" },
];
const APPROVED = { status: "A" as const, decidedBy: "admin", decidedAt: "2026-10-06T11:20:00", stockDocumentNos: "AJI1", emailSentAt: "2026-10-06T11:20:05" };
beforeEach(() => {
Object.values(svc).forEach(fn => fn.mockReset());
});
describe("RefundDrawer — gift cards", () => {
it("records the card part and credits only the rest to Acorns", async () => {
svc.getRefund.mockResolvedValue(detail(request({ ...APPROVED, walletTrxnRef: "T-9", giftCardRefunded: 30 }), CARD_AND_WALLET));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText(/Refilled to gift cards RM 30/)).toBeInTheDocument();
expect(screen.getByText(/Acorns credited RM 20(\.00)? · trxn T-9/)).toBeInTheDocument();
});
it("shows no manual payout when the cards covered the whole refund", async () => {
svc.getRefund.mockResolvedValue(detail(
request({ ...APPROVED, refundAmount: 40, payoutMethod: "MANUAL", payoutChannel: "CASH", giftCardRefunded: 40 }),
[{ paymentType: "GC", amount: 40, reference: "•••• P5R2" }]));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText(/Refilled to gift cards RM 40/)).toBeInTheDocument();
expect(screen.queryByText(/Returned manually/)).not.toBeInTheDocument();
});
it("before approval, labels the card payment and says cards are refilled first", async () => {
svc.getRefund.mockResolvedValue(detail(request(), CARD_AND_WALLET));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText(/Gift card 30/)).toBeInTheDocument();
expect(screen.getByText("Gift card payments are refilled first (to the cards or the Gift card balance); only the rest is paid out.")).toBeInTheDocument();
});
it("the approve confirmation also says cards are refilled first", async () => {
svc.getRefund.mockResolvedValue(detail(request(), CARD_AND_WALLET));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
await screen.findByText(/Gift card 30/);
await userEvent.click(screen.getByRole("button", { name: /approve/i }));
const dialog = await screen.findByRole("dialog");
expect(dialog).toHaveTextContent("Gift card payments are refilled first (to the cards or the Gift card balance); only the rest is paid out.");
});
it("an order with no gift card renders as before", async () => {
svc.getRefund.mockResolvedValue(detail(request({ ...APPROVED, walletTrxnRef: "T-9", giftCardRefunded: null }),
[{ paymentType: "DWP", amount: 100, reference: "T-1" }]));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText(/Acorns credited RM 50(\.00)? · trxn T-9/)).toBeInTheDocument();
expect(screen.queryByText(/Refilled to gift cards/)).not.toBeInTheDocument();
expect(screen.queryByText(/refilled first/)).not.toBeInTheDocument();
});
it("a Gift card wallet payment also says it is refilled first (add-gift-card-wallet)", async () => {
svc.getRefund.mockResolvedValue(detail(request(), [
{ paymentType: "DWG", amount: 40, reference: "SLO-G" },
{ paymentType: "DWP", amount: 60, reference: "T-1" },
]));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText("Gift card payments are refilled first (to the cards or the Gift card balance); only the rest is paid out.")).toBeInTheDocument();
});
// fix-gift-refund-wording
it("a pending gift-card-paid request says the card is refilled first", async () => {
svc.getRefund.mockResolvedValue(detail(request(), CARD_AND_WALLET));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText("Gift card first, then Acorns wallet")).toBeInTheDocument();
expect(
screen.getByText(/Gift card payments are refilled first; the rest of RM 50(\.00)? is credited to Acorns \(Yellow Label spend only, not withdrawable\)/),
).toBeInTheDocument();
expect(screen.queryByText(/^Credit RM 50(\.00)? to Acorns/)).not.toBeInTheDocument();
});
it("the approve confirmation for a gift-card-paid request says the card is refilled first", async () => {
svc.getRefund.mockResolvedValue(detail(request(), CARD_AND_WALLET));
const user = userEvent.setup();
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
await user.click(await screen.findByRole("button", { name: /Approve & refund/ }));
const dialog = await screen.findByRole("dialog", { name: /Approve/ });
expect(dialog).toHaveTextContent(
/Gift card payments are refilled first and the rest of RM 50(\.00)? is credited to the member's Acorns wallet, and the stock reversed\. This cannot be undone\./,
);
});
it("a pending request paid without a gift card keeps the Acorns wording", async () => {
svc.getRefund.mockResolvedValue(detail(request(), [{ paymentType: "DWP", amount: 100, reference: "T-1" }]));
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText("Acorns wallet")).toBeInTheDocument();
expect(screen.getByText(/Credit RM 50(\.00)? to Acorns/)).toBeInTheDocument();
});
// fix-manual-refund-wording
it("a pending manual payout is never described as Acorns", async () => {
svc.getRefund.mockResolvedValue(detail(
request({ payoutMethod: "MANUAL", payoutChannel: "CASH" }),
[{ paymentType: "PC", amount: 100, reference: "" }]));
const user = userEvent.setup();
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
await user.click(await screen.findByRole("button", { name: /Approve & refund/ }));
const dialog = await screen.findByRole("dialog", { name: /Approve/ });
expect(dialog).toHaveTextContent(
/RM 50(\.00)? will be returned to the buyer manually \(Cash\) and the stock reversed\. This cannot be undone\./,
);
expect(dialog).not.toHaveTextContent(/Acorns/);
});
it("a pending gift-card-paid manual payout says the card is refilled first", async () => {
svc.getRefund.mockResolvedValue(detail(request({ payoutMethod: "MANUAL", payoutChannel: "CASH" }), CARD_AND_WALLET));
const user = userEvent.setup();
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={vi.fn()} />);
expect(await screen.findByText("Gift card first, then Manual · Cash")).toBeInTheDocument();
expect(
screen.getByText(/Gift card payments are refilled first; return the rest of RM 50(\.00)? to the buyer manually \(Cash\)/),
).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: /Approve & refund/ }));
const dialog = await screen.findByRole("dialog", { name: /Approve/ });
expect(dialog).toHaveTextContent(
/Gift card payments are refilled first and the rest of RM 50(\.00)? is returned to the buyer manually \(Cash\), and the stock reversed\. This cannot be undone\./,
);
expect(dialog).not.toHaveTextContent(/Acorns/);
});
});