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

130 lines
4.9 KiB
TypeScript

import { test, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { DashboardData } from "@/app/lib/services/adminDashboard";
import DashboardPage from "./page";
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({
name: "Aiman",
roles: [{ name: "ROLE_OPS" }],
loading: false,
hasModule: (m: string) => m === "BO_DASHBOARD",
}),
}));
vi.mock("@/app/components/dashboard/EventDashboard", () => ({ EventDashboard: () => <div /> }));
vi.mock("@/app/lib/useAclSidebar", () => ({ useAclSidebar: () => [] }));
vi.mock("@/app/components/ui/date-range-picker", () => ({ DateRangePicker: () => <div /> }));
const router = vi.hoisted(() => {
let search = new URLSearchParams();
const listeners = new Set<() => void>();
return {
getSearch: () => search,
subscribe: (l: () => void) => {
listeners.add(l);
return () => listeners.delete(l);
},
replace: vi.fn((url: string) => {
search = new URLSearchParams(url.split("?")[1] ?? "");
listeners.forEach(l => l());
}),
reset: (qs = "") => {
search = new URLSearchParams(qs);
},
};
});
vi.mock("next/navigation", async () => {
const { useSyncExternalStore } = await import("react");
return {
usePathname: () => "/",
useSearchParams: () => useSyncExternalStore(router.subscribe, router.getSearch, router.getSearch),
useRouter: () => ({ replace: router.replace }),
};
});
const empty: DashboardData = {
from: "2026-12-01",
to: "2026-12-01",
members: { total: 38, active: 37, newThisMonth: 0, byRank: {}, byStatus: { Y: 37, N: 1 }, newPerDay: [] },
sales: { daily: [], byChannel: [], periodRevenue: 0, periodOrders: 0 },
rewardTotals: [],
topEarners: [],
previous: {
from: "2026-11-01",
to: "2026-11-30",
members: { total: 30, active: 30, newThisMonth: 4, 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) };
});
beforeEach(() => {
getDashboard.mockReset();
router.reset();
router.replace.mockClear();
vi.useFakeTimers({ now: new Date("2026-12-01T04:00:00Z"), toFake: ["Date"] });
getDashboard.mockResolvedValue(empty);
});
const NOTICE = "Nothing recorded yet this month.";
const loaded = () => screen.findByRole("region", { name: "Key performance indicators" });
test("an empty default month shows the notice and a View last month button", async () => {
render(<DashboardPage />);
await loaded();
expect(screen.getByText(NOTICE)).toBeInTheDocument();
expect(screen.getByRole("button", { name: "View last month" })).toBeInTheDocument();
});
test("View last month sets the URL period to last month, like the Period select", async () => {
render(<DashboardPage />);
await loaded();
await userEvent.click(screen.getByRole("button", { name: "View last month" }));
expect(router.replace).toHaveBeenLastCalledWith("/?from=2026-11-01&to=2026-11-30");
await vi.waitFor(() => expect(getDashboard).toHaveBeenLastCalledWith({ from: "2026-11-01", to: "2026-11-30" }));
});
test("the dashboard never changes the period on its own", async () => {
render(<DashboardPage />);
await loaded();
expect(router.replace).not.toHaveBeenCalled();
});
test("one new member alone suppresses the notice", async () => {
getDashboard.mockResolvedValue({
...empty,
members: { ...empty.members, newPerDay: [{ date: "2026-12-01", byRank: { OTTER: 1, SQUIRREL: 0, GIRAFFE: 0 } }] },
});
render(<DashboardPage />);
await loaded();
expect(screen.queryByText(NOTICE)).not.toBeInTheDocument();
});
test.each([
["revenue", { sales: { ...empty.sales, periodRevenue: 10 } }],
["orders", { sales: { ...empty.sales, periodOrders: 1 } }],
["Apples", { rewardTotals: [{ rewardCode: "SPONSOR_APPLE", walletType: "APPLE", total: 5, count: 1 }] }],
["Acorns", { rewardTotals: [{ rewardCode: "SELF_ACORN", walletType: "ACORN", total: 5, count: 1 }] }],
])("non-zero %s suppresses the notice", async (_label, patch) => {
getDashboard.mockResolvedValue({ ...empty, ...patch });
render(<DashboardPage />);
await loaded();
expect(screen.queryByText(NOTICE)).not.toBeInTheDocument();
});
test("a period in the URL suppresses the notice even when everything is zero", async () => {
router.reset("from=2026-08-01&to=2026-08-31");
getDashboard.mockResolvedValue({ ...empty, from: "2026-08-01", to: "2026-08-31" });
render(<DashboardPage />);
await loaded();
expect(screen.queryByText(NOTICE)).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "View last month" })).not.toBeInTheDocument();
});