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
216 lines
9.8 KiB
TypeScript
216 lines
9.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, within, fireEvent } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import type { AdminMember } from "@/app/lib/types";
|
|
import MemberDetailPage from "./page";
|
|
|
|
const params = vi.hoisted(() => ({ memberCode: "BB0100" }));
|
|
const nav = vi.hoisted(() => ({ search: new URLSearchParams() }));
|
|
vi.mock("next/navigation", () => ({
|
|
useParams: () => params,
|
|
useSearchParams: () => nav.search,
|
|
}));
|
|
|
|
const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
|
|
}));
|
|
|
|
const getMember = vi.fn();
|
|
const getMemberStatusHistory = vi.fn();
|
|
const getMemberChangeHistory = vi.fn();
|
|
vi.mock("@/app/lib/services/members", () => ({
|
|
getMember: (code: string) => getMember(code),
|
|
getMemberStatusHistory: (code: string) => getMemberStatusHistory(code),
|
|
getMemberChangeHistory: (code: string) => getMemberChangeHistory(code),
|
|
}));
|
|
|
|
const network = vi.hoisted(() => ({ getNetworkTree: vi.fn() }));
|
|
vi.mock("@/app/lib/services/network", async () => {
|
|
const actual = await vi.importActual<typeof import("@/app/lib/services/network")>(
|
|
"@/app/lib/services/network",
|
|
);
|
|
return {
|
|
...actual,
|
|
getNetworkTree: (code: string) => network.getNetworkTree(code),
|
|
};
|
|
});
|
|
|
|
const wallet = vi.hoisted(() => ({
|
|
getMemberWalletBalances: vi.fn(),
|
|
getMemberWalletLedger: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/wallet", () => ({
|
|
getMemberWalletBalances: (code: string) => wallet.getMemberWalletBalances(code),
|
|
getMemberWalletLedger: (code: string, page: number, size: number) =>
|
|
wallet.getMemberWalletLedger(code, page, size),
|
|
}));
|
|
|
|
const member = {
|
|
id: 1, memberCode: "BB0100", name: "AINA TAN", surname: "TAN", email: "aina@example.com",
|
|
phoneNoPrefix: "60", phoneNo: "123456789", dob: "1990-05-20", gender: "F", identityNo: "900520-14-1234",
|
|
status: "Y", rank: "OTTER", dateJoin: "2025-03-01", sponsorId: "BB0001", referrer: "MY200", kycFlag: "Y",
|
|
bank: { id: 1, code: "MBB", name: "Maybank" }, bankAccountNo: "1234567890", payeeName: "AINA TAN",
|
|
addressDetails: "12 Jalan Merdeka", postCode: "50000", town: "KUALA LUMPUR",
|
|
directDownline: 0, totalDownline: 0, downline: [],
|
|
} as AdminMember;
|
|
|
|
// BABYBOO-54 task 1.2: the recent-ledger and Status history tables sort in the browser.
|
|
// API order is deliberately sorted by none of the columns; no extra request on sort.
|
|
const LEDGER = {
|
|
content: [
|
|
{ transactionDateTime: "2026-08-01T10:30:00", refNo: "REF-C", description: "Mango", amountIn: 9, amountOut: 40, trxnType: "SLO" },
|
|
{ transactionDateTime: "2027-01-02T09:00:00", refNo: "REF-A", description: "Zebra", amountIn: 100, amountOut: 5, trxnType: "TPU" },
|
|
{ transactionDateTime: "2026-03-15T08:00:00", refNo: "REF-B", description: "Apple", amountIn: 25, amountOut: 300, trxnType: "REI" },
|
|
],
|
|
number: 0, size: 5, totalElements: 3, totalPages: 1,
|
|
};
|
|
const HISTORY = {
|
|
content: [
|
|
{ id: 1, memberCode: "BB0100", oldStatus: "N", newStatus: "Y", statusRemark: "Mid", doneBy: "mike", createDateTime: "2026-08-01T10:30:00" },
|
|
{ id: 2, memberCode: "BB0100", oldStatus: "Y", newStatus: "S", statusRemark: "Zebra", doneBy: "zed", createDateTime: "2027-01-02T09:00:00" },
|
|
{ id: 3, memberCode: "BB0100", oldStatus: "S", newStatus: "N", statusRemark: "Alpha", doneBy: "amy", createDateTime: "2026-03-15T08:00:00" },
|
|
],
|
|
totalElements: 3,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
params.memberCode = "BB0100";
|
|
nav.search = new URLSearchParams();
|
|
auth.hasModule.mockReset().mockReturnValue(true);
|
|
auth.loading = false;
|
|
getMember.mockReset().mockResolvedValue(member);
|
|
getMemberStatusHistory.mockReset().mockResolvedValue(HISTORY);
|
|
getMemberChangeHistory.mockReset().mockResolvedValue([]);
|
|
network.getNetworkTree.mockReset().mockResolvedValue({ mbid: "BB0100", member: { memberCode: "BB0100", name: "AINA TAN", rank: "OTTER", status: "Y" } });
|
|
wallet.getMemberWalletBalances.mockReset().mockResolvedValue([]);
|
|
wallet.getMemberWalletLedger.mockReset().mockResolvedValue(LEDGER);
|
|
});
|
|
|
|
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 }));
|
|
const sortState = (t: ReturnType<typeof within>, header: string) =>
|
|
t.getByRole("columnheader", { name: header }).getAttribute("aria-sort");
|
|
|
|
async function ledger() {
|
|
render(<MemberDetailPage />);
|
|
return within(await screen.findByRole("table", { name: "Ledger" }));
|
|
}
|
|
async function history() {
|
|
render(<MemberDetailPage />);
|
|
await screen.findAllByText("AINA TAN");
|
|
await userEvent.setup().click(screen.getByRole("tab", { name: "Status history" }));
|
|
return within(await screen.findByRole("table", { name: "Status history" }));
|
|
}
|
|
|
|
describe("member detail Ledger - client-side header sorting", () => {
|
|
it("Date sorts chronologically, asc then desc", async () => {
|
|
const t = await ledger();
|
|
expect(column(t, "Reference")).toEqual(["REF-C", "REF-A", "REF-B"]);
|
|
click(t, "Date");
|
|
expect(sortState(t, "Date")).toBe("ascending");
|
|
expect(column(t, "Reference")).toEqual(["REF-B", "REF-C", "REF-A"]);
|
|
click(t, "Date");
|
|
expect(sortState(t, "Date")).toBe("descending");
|
|
expect(column(t, "Reference")).toEqual(["REF-A", "REF-C", "REF-B"]);
|
|
});
|
|
|
|
it.each([
|
|
["Reference", ["REF-A", "REF-B", "REF-C"]],
|
|
["Description", ["REF-B", "REF-C", "REF-A"]],
|
|
["Type", ["REF-B", "REF-C", "REF-A"]], // Earning in, Sales out, Top-Up
|
|
["In", ["REF-C", "REF-B", "REF-A"]], // 9, 25, 100 numeric (not "+100" first)
|
|
["Out", ["REF-A", "REF-C", "REF-B"]], // 5, 40, 300 numeric
|
|
])("%s sorts asc then desc by displayed value", async (header, asc) => {
|
|
const t = await ledger();
|
|
click(t, header);
|
|
expect(sortState(t, header)).toBe("ascending");
|
|
expect(column(t, "Reference")).toEqual(asc);
|
|
click(t, header);
|
|
expect(sortState(t, header)).toBe("descending");
|
|
expect(column(t, "Reference")).toEqual([...asc].reverse());
|
|
});
|
|
|
|
it("sorting makes no new request and keeps header labels", async () => {
|
|
const t = await ledger();
|
|
click(t, "Date");
|
|
click(t, "In");
|
|
expect(wallet.getMemberWalletLedger).toHaveBeenCalledTimes(1);
|
|
expect(t.getAllByRole("columnheader").map((h: HTMLElement) => h.textContent?.trim()))
|
|
.toEqual(["Date", "Reference", "Description", "In", "Out", "Type"]);
|
|
});
|
|
});
|
|
|
|
describe("member detail Status history - client-side header sorting", () => {
|
|
it("Date sorts chronologically, asc then desc", async () => {
|
|
const t = await history();
|
|
expect(column(t, "Remark")).toEqual(["Mid", "Zebra", "Alpha"]);
|
|
click(t, "Date");
|
|
expect(sortState(t, "Date")).toBe("ascending");
|
|
expect(column(t, "Remark")).toEqual(["Alpha", "Mid", "Zebra"]);
|
|
click(t, "Date");
|
|
expect(sortState(t, "Date")).toBe("descending");
|
|
expect(column(t, "Remark")).toEqual(["Zebra", "Mid", "Alpha"]);
|
|
});
|
|
|
|
it.each([
|
|
["From", ["Zebra", "Mid", "Alpha"]], // Active, Inactive, Suspended
|
|
["To", ["Mid", "Alpha", "Zebra"]], // Active, Inactive, Suspended
|
|
["Remark", ["Alpha", "Mid", "Zebra"]],
|
|
["By", ["Alpha", "Mid", "Zebra"]], // amy, mike, zed
|
|
])("%s sorts asc then desc by displayed value", async (header, asc) => {
|
|
const t = await history();
|
|
click(t, header);
|
|
expect(sortState(t, header)).toBe("ascending");
|
|
expect(column(t, "Remark")).toEqual(asc);
|
|
click(t, header);
|
|
expect(sortState(t, header)).toBe("descending");
|
|
expect(column(t, "Remark")).toEqual([...asc].reverse());
|
|
});
|
|
|
|
it("sorting makes no new request and keeps header labels", async () => {
|
|
const t = await history();
|
|
click(t, "Date");
|
|
click(t, "By");
|
|
expect(getMemberStatusHistory).toHaveBeenCalledTimes(1);
|
|
expect(t.getAllByRole("columnheader").map((h: HTMLElement) => h.textContent?.trim()))
|
|
.toEqual(["Date", "From", "To", "Remark", "By"]);
|
|
});
|
|
});
|
|
|
|
// Reproduction: one checkout writes two ledger rows sharing a refNo. A refNo-only React key
|
|
// made React keep stale DOM for the duplicates, so the table grew on every sort click.
|
|
describe("member detail Ledger - rows sharing a refNo", () => {
|
|
const DUP = {
|
|
...LEDGER,
|
|
content: [
|
|
{ transactionDateTime: "2026-08-01T10:30:00", refNo: "3301964017554284544", description: "Checkout", amountIn: 0, amountOut: 40, trxnType: "SLO" },
|
|
{ transactionDateTime: "2027-01-02T09:00:00", refNo: "REF-A", description: "Zebra", amountIn: 100, amountOut: 5, trxnType: "TPU" },
|
|
{ transactionDateTime: "2026-08-01T10:30:00", refNo: "3301964017554284544", description: "Checkout", amountIn: 0, amountOut: 12, trxnType: "SLO" },
|
|
{ transactionDateTime: "2026-03-15T08:00:00", refNo: "REF-B", description: "Apple", amountIn: 25, amountOut: 300, trxnType: "REI" },
|
|
{ transactionDateTime: "2026-05-05T08:00:00", refNo: "REF-D", description: "Pear", amountIn: 7, amountOut: 0, trxnType: "REI" },
|
|
],
|
|
totalElements: 5,
|
|
};
|
|
const rowTexts = (t: ReturnType<typeof within>) =>
|
|
t.getAllByRole("row").slice(1).map((r: HTMLElement) => r.textContent);
|
|
|
|
it("keeps every row exactly once across repeated sorts", async () => {
|
|
wallet.getMemberWalletLedger.mockReset().mockResolvedValue(DUP);
|
|
const t = await ledger();
|
|
const original = rowTexts(t);
|
|
expect(original).toHaveLength(5);
|
|
const expected = [...original].sort();
|
|
|
|
for (const header of ["Date", "Date", "Reference", "Out"]) {
|
|
click(t, header);
|
|
const now = rowTexts(t);
|
|
expect(now, `after clicking ${header}`).toHaveLength(5);
|
|
expect([...now].sort()).toEqual(expected);
|
|
}
|
|
});
|
|
});
|