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
103 lines
4.3 KiB
TypeScript
103 lines
4.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, within, fireEvent } from "@testing-library/react";
|
|
import type { PaymentMethodView } from "@/app/lib/types";
|
|
|
|
// BABYBOO-54 task 1.1: the /payment-methods list sorts in the browser; the Edit column does not sort.
|
|
|
|
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 pm = (id: string, messageTag: string, paymentType: string, status: string, availablePos: boolean, availableOnline: boolean): PaymentMethodView =>
|
|
({ id, messageTag, description: null, paymentType, status, availablePos, availableOnline }) as PaymentMethodView;
|
|
|
|
// API order is deliberately sorted by none of the columns.
|
|
const ROWS = [
|
|
pm("1", "Cash", "PC", "Y", true, false),
|
|
pm("2", "Wallet", "DWP", "N", false, true),
|
|
pm("3", "Coupon", "GC", "Y", true, true),
|
|
];
|
|
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReset().mockReturnValue(true);
|
|
svc.listPaymentMethods.mockReset().mockResolvedValue(ROWS);
|
|
});
|
|
|
|
async function table() {
|
|
await screen.findByText("Cash");
|
|
return within(screen.getByRole("table"));
|
|
}
|
|
function column(t: ReturnType<typeof within>, header: string) {
|
|
const idx = t.getAllByRole("columnheader").findIndex((h: HTMLElement) => h.textContent?.trim() === header);
|
|
return t.getAllByRole("row").slice(1).map((r: HTMLElement) => within(r).getAllByRole("cell")[idx].textContent);
|
|
}
|
|
const click = (t: ReturnType<typeof within>, header: string) => fireEvent.click(t.getByRole("button", { name: header }));
|
|
|
|
describe("/payment-methods - client-side header sorting", () => {
|
|
it("Label header reorders asc then desc", async () => {
|
|
render(<PaymentMethodsPage />);
|
|
const t = await table();
|
|
expect(column(t, "Label")).toEqual(["Cash", "Wallet", "Coupon"]);
|
|
click(t, "Label");
|
|
expect(column(t, "Label")).toEqual(["Cash", "Coupon", "Wallet"]);
|
|
expect(t.getByRole("columnheader", { name: "Label" })).toHaveAttribute("aria-sort", "ascending");
|
|
click(t, "Label");
|
|
expect(column(t, "Label")).toEqual(["Wallet", "Coupon", "Cash"]);
|
|
expect(t.getByRole("columnheader", { name: "Label" })).toHaveAttribute("aria-sort", "descending");
|
|
});
|
|
|
|
it("Type header sorts by the displayed type label", async () => {
|
|
render(<PaymentMethodsPage />);
|
|
const t = await table();
|
|
click(t, "Type");
|
|
expect(column(t, "Type")).toEqual(["Cash / Manual", "Gift card", "Product wallet"]);
|
|
click(t, "Type");
|
|
expect(column(t, "Type")).toEqual(["Product wallet", "Gift card", "Cash / Manual"]);
|
|
});
|
|
|
|
it("Status header orders by label, Active first asc then Inactive first desc", async () => {
|
|
render(<PaymentMethodsPage />);
|
|
const t = await table();
|
|
click(t, "Status");
|
|
expect(column(t, "Status")).toEqual(["Active", "Active", "Inactive"]);
|
|
click(t, "Status");
|
|
expect(column(t, "Status")).toEqual(["Inactive", "Active", "Active"]);
|
|
});
|
|
|
|
it("POS header groups the unavailable method first, then reverses", async () => {
|
|
render(<PaymentMethodsPage />);
|
|
const t = await table();
|
|
click(t, "POS");
|
|
expect(column(t, "Label")[0]).toBe("Wallet");
|
|
click(t, "POS");
|
|
expect(column(t, "Label")[2]).toBe("Wallet");
|
|
});
|
|
|
|
it("Online header groups the unavailable methods first, then reverses", async () => {
|
|
render(<PaymentMethodsPage />);
|
|
const t = await table();
|
|
click(t, "Online");
|
|
expect(column(t, "Label")[0]).toBe("Cash");
|
|
click(t, "Online");
|
|
expect(column(t, "Label")[2]).toBe("Cash");
|
|
});
|
|
|
|
it("Edit column has no sort button; sorting makes no new request", async () => {
|
|
render(<PaymentMethodsPage />);
|
|
const t = await table();
|
|
const heads = t.getAllByRole("columnheader");
|
|
expect(within(heads[heads.length - 1]).queryByRole("button")).toBeNull();
|
|
click(t, "Label");
|
|
expect(svc.listPaymentMethods).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|