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
192 lines
8.1 KiB
TypeScript
192 lines
8.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import type { AdminRefundDetail, AdminRefundRequest } from "@/app/lib/types";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
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: "Bought L, baby needs M",
|
|
refundAmount: 89,
|
|
decisionReason: null,
|
|
decidedBy: null,
|
|
decidedAt: null,
|
|
walletTrxnRef: null,
|
|
stockDocumentNos: null,
|
|
lastError: null,
|
|
emailSentAt: null,
|
|
createDate: "2026-09-21T10:12:00",
|
|
lines: [
|
|
{ orderDetailsId: 11, productName: "Diaper L 40s", qty: 2, netUnitPrice: 34.5, amount: 69 },
|
|
{ orderDetailsId: 12, productName: "Wipes 80s", qty: 1, netUnitPrice: 20, amount: 20 },
|
|
],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function detail(req: AdminRefundRequest = request()): AdminRefundDetail {
|
|
return {
|
|
request: req,
|
|
buyerName: "Tan Mei Ling",
|
|
orderDate: "2026-09-14T09:00:00",
|
|
grandTotal: 97,
|
|
shippingCost: 8,
|
|
tenders: [
|
|
{ paymentType: "PC", amount: 50, reference: null },
|
|
{ paymentType: "IPAY88", amount: 39, reference: "T123" },
|
|
],
|
|
stockEffects: [
|
|
{ deliveryOrderNo: "DO0001", deliveryOrderType: "HDO", fulfilmentStatus: "DE", warehouseId: "2", effect: "RETURN_TO_SHELF" },
|
|
],
|
|
};
|
|
}
|
|
|
|
function renderDrawer(onChanged = vi.fn()) {
|
|
render(<RefundDrawer uuid="U-31" onClose={() => {}} onChanged={onChanged} />);
|
|
return onChanged;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
Object.values(svc).forEach(fn => fn.mockReset());
|
|
});
|
|
|
|
describe("RefundDrawer — pending", () => {
|
|
it("names the variant after the product, and just the name when the line has none", async () => {
|
|
const lines = [
|
|
{ orderDetailsId: 11, productName: "YELLOW LABEL ITEM 003", variantLabel: "grey / M", qty: 1, netUnitPrice: 20, amount: 20 },
|
|
{ orderDetailsId: 12, productName: "Wipes 80s", variantLabel: null, qty: 1, netUnitPrice: 20, amount: 20 },
|
|
];
|
|
svc.getRefund.mockResolvedValue(detail(request({ lines })));
|
|
renderDrawer();
|
|
expect(await screen.findByText("YELLOW LABEL ITEM 003 · grey / M")).toBeInTheDocument();
|
|
expect(screen.getByText("Wipes 80s")).toBeInTheDocument();
|
|
});
|
|
|
|
it("previews what approval will do before any action", async () => {
|
|
svc.getRefund.mockResolvedValue(detail());
|
|
renderDrawer();
|
|
|
|
expect(await screen.findByRole("dialog", { name: /RR00000031/ })).toBeInTheDocument();
|
|
expect(screen.getByText("Diaper L 40s")).toBeInTheDocument();
|
|
expect(screen.getByText(/89\.00 to Acorns/)).toBeInTheDocument();
|
|
expect(screen.getByText(/shipping 8\.00 not refunded/i)).toBeInTheDocument();
|
|
expect(screen.getByText(/DO0001/)).toBeInTheDocument();
|
|
expect(screen.getByText(/Cash \/ Manual 50\.00/)).toBeInTheDocument();
|
|
expect(screen.getByText(/restock at warehouse 2/i)).toBeInTheDocument();
|
|
expect(screen.getByText(/goods are received/i)).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: /approve & refund/i })).toBeEnabled();
|
|
expect(screen.getByRole("button", { name: /reject/i })).toBeEnabled();
|
|
});
|
|
|
|
it("approves only after confirmation, then reports the change", async () => {
|
|
svc.getRefund.mockResolvedValue(detail());
|
|
svc.approveRefund.mockResolvedValue(request({ status: "A" }));
|
|
const onChanged = renderDrawer();
|
|
|
|
await userEvent.click(await screen.findByRole("button", { name: /approve & refund/i }));
|
|
expect(svc.approveRefund).not.toHaveBeenCalled();
|
|
expect(screen.getByText(/cannot be undone/i)).toBeInTheDocument();
|
|
await userEvent.type(screen.getByLabelText(/note/i), "goods inspected");
|
|
await userEvent.click(screen.getByRole("button", { name: /confirm approve/i }));
|
|
|
|
await waitFor(() => expect(svc.approveRefund).toHaveBeenCalledWith("U-31", { note: "goods inspected" }));
|
|
expect(onChanged).toHaveBeenCalled();
|
|
});
|
|
|
|
it("blocks a reject without a reason in the dialog", async () => {
|
|
svc.getRefund.mockResolvedValue(detail());
|
|
renderDrawer();
|
|
|
|
await userEvent.click(await screen.findByRole("button", { name: /^reject/i }));
|
|
await userEvent.click(screen.getByRole("button", { name: /confirm reject/i }));
|
|
|
|
expect(screen.getByText("Reason is required")).toBeInTheDocument();
|
|
expect(svc.rejectRefund).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects with the typed reason", async () => {
|
|
svc.getRefund.mockResolvedValue(detail());
|
|
svc.rejectRefund.mockResolvedValue(request({ status: "R" }));
|
|
const onChanged = renderDrawer();
|
|
|
|
await userEvent.click(await screen.findByRole("button", { name: /^reject/i }));
|
|
await userEvent.type(screen.getByLabelText(/reason/i), "Item opened");
|
|
await userEvent.click(screen.getByRole("button", { name: /confirm reject/i }));
|
|
|
|
await waitFor(() => expect(svc.rejectRefund).toHaveBeenCalledWith("U-31", "Item opened"));
|
|
expect(onChanged).toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows an approve failure inline and keeps approve available to retry", async () => {
|
|
svc.getRefund
|
|
.mockResolvedValueOnce(detail())
|
|
.mockResolvedValue(detail(request({ status: "I", walletTrxnRef: "T-9", lastError: "Stock reversal failed: inventory down" })));
|
|
svc.approveRefund.mockRejectedValue(new ApiError("Stock reversal failed: inventory down. Press Approve again to retry.", 400));
|
|
renderDrawer();
|
|
|
|
await userEvent.click(await screen.findByRole("button", { name: /approve & refund/i }));
|
|
await userEvent.click(screen.getByRole("button", { name: /confirm approve/i }));
|
|
|
|
expect(await screen.findByRole("alert")).toHaveTextContent(/inventory down/);
|
|
await waitFor(() => expect(screen.getByRole("button", { name: /approve & refund/i })).toBeEnabled());
|
|
expect(screen.queryByRole("button", { name: /^reject/i })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("reports a conflict and reloads the request", async () => {
|
|
svc.getRefund
|
|
.mockResolvedValueOnce(detail())
|
|
.mockResolvedValue(detail(request({ status: "A", decidedBy: "other admin" })));
|
|
svc.approveRefund.mockRejectedValue(new ApiError("Refund request RR00000031 is already approved", 409));
|
|
renderDrawer();
|
|
|
|
await userEvent.click(await screen.findByRole("button", { name: /approve & refund/i }));
|
|
await userEvent.click(screen.getByRole("button", { name: /confirm approve/i }));
|
|
|
|
expect(await screen.findByText(/already approved/)).toBeInTheDocument();
|
|
await waitFor(() => expect(svc.getRefund).toHaveBeenCalledTimes(2));
|
|
});
|
|
});
|
|
|
|
describe("RefundDrawer — decided", () => {
|
|
it("shows the reversal record read-only once approved", async () => {
|
|
svc.getRefund.mockResolvedValue(detail(request({
|
|
status: "A", decidedBy: "admin", decidedAt: "2026-09-22T11:20:00", walletTrxnRef: "T-9",
|
|
stockDocumentNos: "AJI202609221120", emailSentAt: "2026-09-22T11:20:05",
|
|
})));
|
|
renderDrawer();
|
|
|
|
expect(await screen.findByText(/T-9/)).toBeInTheDocument();
|
|
expect(screen.getByText(/AJI202609221120/)).toBeInTheDocument();
|
|
expect(screen.getByText(/email sent/i)).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /approve & refund/i })).not.toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /^reject/i })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("offers Retry email when the decision email was not sent", async () => {
|
|
svc.getRefund.mockResolvedValue(detail(request({ status: "R", decisionReason: "Item opened" })));
|
|
svc.retryRefundEmail.mockResolvedValue(request({ status: "R", emailSentAt: "2026-09-22T12:00:00" }));
|
|
renderDrawer();
|
|
|
|
expect(await screen.findByText("Item opened")).toBeInTheDocument();
|
|
await userEvent.click(screen.getByRole("button", { name: /retry email/i }));
|
|
await waitFor(() => expect(svc.retryRefundEmail).toHaveBeenCalledWith("U-31"));
|
|
});
|
|
});
|