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: () =>
})); vi.mock("@/app/lib/useAclSidebar", () => ({ useAclSidebar: () => [] })); vi.mock("@/app/components/ui/date-range-picker", () => ({ DateRangePicker: () =>
})); 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("@/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(); 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(); 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(); 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(); 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(); 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(); await loaded(); expect(screen.queryByText(NOTICE)).not.toBeInTheDocument(); expect(screen.queryByRole("button", { name: "View last month" })).not.toBeInTheDocument(); });