Files
co-lite/solvway-admin-portal/app/payment-methods/page.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

110 lines
4.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 { PaymentMethodView } from "@/app/lib/types";
// RequireModule gate — /payment-methods maps to BO_PAYMENT_METHODS in ROUTE_MODULES.
const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
}));
const svc = vi.hoisted(() => ({
listPaymentMethods: vi.fn(),
createPaymentMethod: vi.fn(),
updatePaymentMethod: vi.fn(),
}));
vi.mock("@/app/lib/services/adminPaymentMethods", () => ({
listPaymentMethods: () => svc.listPaymentMethods(),
createPaymentMethod: (p: unknown) => svc.createPaymentMethod(p),
updatePaymentMethod: (id: string, p: unknown) => svc.updatePaymentMethod(id, p),
}));
import PaymentMethodsPage from "./page";
const CASH: PaymentMethodView = {
id: "1",
messageTag: "Cash",
description: null,
paymentType: "PC",
status: "Y",
availablePos: true,
availableOnline: false,
};
beforeEach(() => {
vi.clearAllMocks();
auth.hasModule.mockReturnValue(true);
svc.listPaymentMethods.mockResolvedValue([CASH]);
svc.createPaymentMethod.mockResolvedValue({ ...CASH, id: "2" });
svc.updatePaymentMethod.mockResolvedValue(CASH);
});
describe("Payment methods page", () => {
it("lists methods with status and channel availability", async () => {
render(<PaymentMethodsPage />);
expect(await screen.findByText("Cash")).toBeInTheDocument();
expect(screen.getByText("Cash / Manual")).toBeInTheDocument();
expect(screen.queryByText("PC")).not.toBeInTheDocument();
expect(screen.getByText("Active")).toHaveAttribute("data-variant", "positive");
expect(screen.getByRole("heading", { level: 1, name: "Payment methods" })).toBeInTheDocument();
// availablePos true -> "Yes"; availableOnline false -> "—".
expect(screen.getByTestId("pos-1")).toHaveTextContent("Yes");
expect(screen.getByTestId("online-1")).toHaveTextContent("—");
});
it("creates a method with a payment type and channel flags", async () => {
const user = userEvent.setup();
render(<PaymentMethodsPage />);
await screen.findByText("Cash");
await user.click(screen.getByRole("button", { name: /add payment method/i }));
await user.type(screen.getByLabelText("Label"), "QR Pay");
await user.click(screen.getByLabelText("Payment type"));
await user.click(await screen.findByRole("option", { name: "Online payment (gateway)" }));
await user.click(screen.getByRole("switch", { name: "Available online" }));
await user.click(screen.getByRole("button", { name: /^create$/i }));
await waitFor(() => expect(svc.createPaymentMethod).toHaveBeenCalledTimes(1));
expect(svc.createPaymentMethod).toHaveBeenCalledWith(
expect.objectContaining({
messageTag: "QR Pay",
paymentType: "POL",
status: "Y",
availableOnline: true,
}),
);
});
it("edits a method and toggles a channel", async () => {
const user = userEvent.setup();
render(<PaymentMethodsPage />);
await screen.findByText("Cash");
await user.click(screen.getByRole("button", { name: "Edit Cash" }));
// Dialog is prefilled from the row.
expect(screen.getByLabelText("Label")).toHaveValue("Cash");
// Turn Online availability on and save.
await user.click(screen.getByRole("switch", { name: "Available online" }));
await user.click(screen.getByRole("button", { name: /save changes/i }));
await waitFor(() => expect(svc.updatePaymentMethod).toHaveBeenCalledTimes(1));
expect(svc.updatePaymentMethod).toHaveBeenCalledWith(
"1",
expect.objectContaining({ availableOnline: true, availablePos: true }),
);
});
it("shows the not-authorised state without the module grant", async () => {
auth.hasModule.mockReturnValue(false);
render(<PaymentMethodsPage />);
expect(await screen.findByText(/not authori/i)).toBeInTheDocument();
expect(svc.listPaymentMethods).not.toHaveBeenCalled();
});
});