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

146 lines
6.4 KiB
TypeScript

import { test, expect, vi, beforeEach } from "vitest";
import { render, screen, within, waitFor } from "@testing-library/react";
import type { DashboardData } from "@/app/lib/services/adminDashboard";
import DashboardPage from "./page";
const auth = vi.hoisted(() => ({ modules: [] as string[] }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({
name: "Aiman",
roles: [{ name: "ROLE_OPS" }],
loading: false,
hasModule: (m: string) => auth.modules.includes(m),
}),
}));
vi.mock("@/app/components/dashboard/EventDashboard", () => ({ EventDashboard: () => <div /> }));
vi.mock("@/app/lib/useAclSidebar", () => ({ useAclSidebar: () => [] }));
vi.mock("next/navigation", () => ({
usePathname: () => "/",
useSearchParams: () => new URLSearchParams(""),
useRouter: () => ({ replace: vi.fn() }),
}));
vi.mock("@/app/components/ui/date-range-picker", () => ({ DateRangePicker: () => <div /> }));
const m = vi.hoisted(() => ({
getKycCounts: vi.fn(),
listRefunds: vi.fn(),
listWarrantyClaims: vi.fn(),
listReviews: vi.fn(),
getPendingWithdrawals: vi.fn(),
listWebOrders: vi.fn(),
}));
vi.mock("@/app/lib/services/adminKyc", () => ({ getKycCounts: m.getKycCounts }));
vi.mock("@/app/lib/services/adminRefunds", () => ({ listRefunds: m.listRefunds }));
vi.mock("@/app/lib/services/adminWarranty", () => ({ listWarrantyClaims: m.listWarrantyClaims }));
vi.mock("@/app/lib/services/adminProductReviews", () => ({ listReviews: m.listReviews }));
vi.mock("@/app/lib/services/adminWithdrawals", () => ({ getPendingWithdrawals: m.getPendingWithdrawals }));
vi.mock("@/app/lib/services/adminWebOrders", async importOriginal => ({
...(await importOriginal<typeof import("@/app/lib/services/adminWebOrders")>()),
listWebOrders: m.listWebOrders,
}));
const data: DashboardData = {
from: "2026-09-01",
to: "2026-09-30",
members: { total: 38, active: 37, newThisMonth: 36, byRank: {}, byStatus: {}, newPerDay: [] },
sales: { daily: [], byChannel: [], periodRevenue: 155.5, periodOrders: 3 },
rewardTotals: [],
topEarners: [],
previous: {
from: "2026-08-01",
to: "2026-08-31",
members: { total: 30, active: 30, newThisMonth: 40, byRank: {}, byStatus: {}, newPerDay: [] },
sales: { daily: [], byChannel: [], periodRevenue: 100, periodOrders: 4 },
rewardTotals: [],
},
};
const getDashboard = vi.fn();
vi.mock("@/app/lib/services/adminDashboard", async () => {
const actual = await vi.importActual<typeof import("@/app/lib/services/adminDashboard")>("@/app/lib/services/adminDashboard");
return { ...actual, getDashboard: (p?: unknown) => getDashboard(p) };
});
const ALL = ["BO_DASHBOARD", "BO_MEMBERS", "BO_REPORTS", "BO_KYC", "BO_REFUND_REQUESTS", "BO_WARRANTY_CLAIMS", "BO_PRODUCT_REVIEWS", "BO_WALLET", "BO_WEB_ORDERS"];
beforeEach(() => {
Object.values(m).forEach(f => f.mockReset());
getDashboard.mockReset();
getDashboard.mockResolvedValue(data);
m.getKycCounts.mockResolvedValue({ P: 3 });
m.listRefunds.mockResolvedValue({ pendingCount: 0 });
m.listWarrantyClaims.mockResolvedValue({ totalElements: 0 });
m.listReviews.mockResolvedValue({ totalElements: 0 });
m.getPendingWithdrawals.mockResolvedValue({ totalElements: 0 });
m.listWebOrders.mockResolvedValue({ totalElements: 4 });
auth.modules = ALL;
});
const strip = async () => await screen.findByRole("region", { name: "Needs attention" });
test("KYC pending 3 shows a count linking to the KYC queue", async () => {
render(<DashboardPage />);
const link = within(await strip()).getByRole("link", { name: /KYC to review.*View records$/ });
expect(link).toHaveAttribute("href", "/members/knowYourCustomer");
expect(link).toHaveTextContent("3");
});
test("stale orders item links to the stale-filtered order list", async () => {
render(<DashboardPage />);
const link = within(await strip()).getByRole("link", { name: /Stale pending orders.*View records$/ });
expect(link).toHaveAttribute("href", "/orders/web?status=STALE");
});
test("zero counts are not listed", async () => {
render(<DashboardPage />);
const s = await strip();
await within(s).findByText("KYC to review");
expect(within(s).queryByText("Refund requests")).not.toBeInTheDocument();
});
test("the strip sits above the KPI hero, not waiting on the dashboard", async () => {
render(<DashboardPage />);
const s = await strip();
const kpi = await screen.findByRole("region", { name: "Key performance indicators" });
expect(s.compareDocumentPosition(kpi) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
});
test("all counts zero shows the all-clear line and no item links", async () => {
m.getKycCounts.mockResolvedValue({ P: 0 });
m.listWebOrders.mockResolvedValue({ totalElements: 0 });
render(<DashboardPage />);
const s = await strip();
expect(await within(s).findByText("Nothing needs your attention")).toBeInTheDocument();
expect(s.querySelectorAll("a")).toHaveLength(0);
});
test("no access to any action route renders no strip and calls nothing", async () => {
auth.modules = ["BO_DASHBOARD", "BO_MEMBERS", "BO_REPORTS"];
render(<DashboardPage />);
await screen.findByRole("region", { name: "Key performance indicators" });
expect(screen.queryByRole("region", { name: "Needs attention" })).not.toBeInTheDocument();
Object.values(m).forEach(f => expect(f).not.toHaveBeenCalled());
});
test("only allowed routes are requested", async () => {
auth.modules = ["BO_DASHBOARD", "BO_REFUND_REQUESTS"];
m.listRefunds.mockResolvedValue({ pendingCount: 1 });
render(<DashboardPage />);
await within(await strip()).findByText("Refund requests");
expect(m.getKycCounts).not.toHaveBeenCalled();
expect(m.listWebOrders).not.toHaveBeenCalled();
});
test("a rejected source is dropped, the rest and the dashboard figures still render", async () => {
m.getKycCounts.mockResolvedValue({ P: 0 });
m.listRefunds.mockRejectedValue(new Error("boom"));
m.listWarrantyClaims.mockResolvedValue({ totalElements: 2 });
m.listWebOrders.mockResolvedValue({ totalElements: 0 });
render(<DashboardPage />);
const s = await strip();
const warranty = await within(s).findByRole("link", { name: /Warranty claims.*View records$/ });
expect(warranty).toHaveTextContent("2");
expect(within(s).queryByText("Refund requests")).not.toBeInTheDocument();
await waitFor(() => expect(screen.getByRole("region", { name: "Key performance indicators" })).toBeInTheDocument());
expect(screen.getByText("36")).toBeInTheDocument();
});