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
208 lines
9.1 KiB
TypeScript
208 lines
9.1 KiB
TypeScript
import { test, expect, vi, beforeEach } from "vitest";
|
||
import { render, screen, within } 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 /> }));
|
||
|
||
// The computed period differs from anything in the URL: links must use it.
|
||
const data: DashboardData = {
|
||
from: "2026-09-01",
|
||
to: "2026-09-30",
|
||
members: {
|
||
total: 38,
|
||
active: 37,
|
||
newThisMonth: 36,
|
||
byRank: { OTTER: 8, SQUIRREL: 19, GIRAFFE: 10 },
|
||
byStatus: { Y: 35, N: 1, S: 2 },
|
||
newPerDay: [{ date: "2026-09-01", byRank: { OTTER: 2, SQUIRREL: 0, GIRAFFE: 1 } }],
|
||
},
|
||
sales: {
|
||
daily: [{ date: "2026-09-02", revenue: 155.5, orders: 3 }],
|
||
byChannel: [{ channel: "NORMAL", revenue: 100.5, orders: 1 }],
|
||
periodRevenue: 155.5,
|
||
periodOrders: 3,
|
||
},
|
||
rewardTotals: [{ rewardCode: "SELF_ACORN", walletType: "ACORN", total: 5000, count: 25 }],
|
||
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) };
|
||
});
|
||
|
||
beforeEach(() => {
|
||
getDashboard.mockReset();
|
||
getDashboard.mockResolvedValue(data);
|
||
auth.modules = ["BO_DASHBOARD", "BO_MEMBERS", "BO_REPORTS"];
|
||
});
|
||
|
||
const hrefs = (el: HTMLElement) => Array.from(el.querySelectorAll("a")).map(a => a.getAttribute("href"));
|
||
const card = async (title: string) => (await screen.findByText(title)).closest("[data-slot='card']") as HTMLElement;
|
||
const kpiRegion = async () => await screen.findByRole("region", { name: "Key performance indicators" });
|
||
|
||
test("New members tile links to the member list for the computed period", async () => {
|
||
render(<DashboardPage />);
|
||
const region = await kpiRegion();
|
||
expect(within(region).getByRole("link", { name: /New members.*View records$/ })).toHaveAttribute(
|
||
"href",
|
||
"/members?joinDateFrom=2026-09-01&joinDateTo=2026-09-30",
|
||
);
|
||
});
|
||
|
||
test("a hero tile link's accessible name includes its change vs the previous period", async () => {
|
||
render(<DashboardPage />);
|
||
const region = await kpiRegion();
|
||
const rev = within(region).getByRole("link", { name: /Revenue.*View records$/ });
|
||
expect(rev).not.toHaveAttribute("aria-label");
|
||
expect(rev).toHaveAccessibleName(/\+55\.5%/);
|
||
expect(within(region).getByRole("link", { name: /New members.*View records$/ })).toHaveAccessibleName(/−10\.0%/);
|
||
});
|
||
|
||
test("Revenue, Orders, Apples and Acorns tiles link to the reports", async () => {
|
||
render(<DashboardPage />);
|
||
const region = await kpiRegion();
|
||
const sales = "/reports?sales.preset=custom&sales.from=2026-09-01&sales.to=2026-09-30";
|
||
const rewards = "/reports?tab=rewards&rewards.preset=custom&rewards.from=2026-09-01&rewards.to=2026-09-30";
|
||
expect(within(region).getByRole("link", { name: /Revenue.*View records$/ })).toHaveAttribute("href", sales);
|
||
expect(within(region).getByRole("link", { name: /Orders.*View records$/ })).toHaveAttribute("href", sales);
|
||
expect(within(region).getByRole("link", { name: /Apples credited.*View records$/ })).toHaveAttribute("href", rewards);
|
||
expect(within(region).getByRole("link", { name: /Acorns credited.*View records$/ })).toHaveAttribute("href", rewards);
|
||
});
|
||
|
||
test("a rank row links to the member list filtered by that rank", async () => {
|
||
render(<DashboardPage />);
|
||
const rank = within(await card("Rank mix"));
|
||
expect(rank.getByRole("link", { name: /Squirrel.*View records$/ })).toHaveAttribute("href", "/members?rank=SQUIRREL");
|
||
});
|
||
|
||
test("Active and Inactive status rows link; Suspended stays plain text", async () => {
|
||
render(<DashboardPage />);
|
||
const status = await card("Member status");
|
||
expect(hrefs(status)).toEqual(["/members?status=Y", "/members?status=N"]);
|
||
expect(within(status).getByText("Suspended")).toBeInTheDocument();
|
||
});
|
||
|
||
test("a rewards-by-type row links to the rewards report filtered by that reward", async () => {
|
||
render(<DashboardPage />);
|
||
const rewards = await card("Rewards by type");
|
||
expect(hrefs(rewards)).toEqual([
|
||
"/reports?tab=rewards&rewards.preset=custom&rewards.from=2026-09-01&rewards.to=2026-09-30&rewards.reward=SELF_ACORN",
|
||
]);
|
||
});
|
||
|
||
test("the trend charts carry no links", async () => {
|
||
render(<DashboardPage />);
|
||
await kpiRegion();
|
||
for (const title of ["Member growth", "Revenue trend"]) {
|
||
const c = (await screen.findAllByText(title))[0].closest("[data-slot='card']") as HTMLElement;
|
||
expect(c.querySelectorAll("a")).toHaveLength(0);
|
||
}
|
||
});
|
||
|
||
const channelCard = async () =>
|
||
(await screen.findAllByText("Revenue by channel"))[0].closest("[data-slot='card']") as HTMLElement;
|
||
const listing = (type: string) => `/orders/web?type=${type}&status=Y&from=2026-09-01&to=2026-09-30`;
|
||
const withChannels = (byChannel: unknown[]) =>
|
||
getDashboard.mockResolvedValue({ ...data, sales: { ...data.sales, byChannel } });
|
||
|
||
test("Revenue by channel rows are order types linking to the Sales listing", async () => {
|
||
auth.modules = ["BO_DASHBOARD", "BO_REPORTS", "BO_WEB_ORDERS"];
|
||
withChannels([
|
||
{ channel: "NORMAL", platform: "CP", revenue: 100, orders: 2 },
|
||
{ channel: "NORMAL", platform: "BO", revenue: 20, orders: 1 },
|
||
{ channel: "EVENT", platform: "BO", revenue: 40, orders: 1 },
|
||
{ channel: "OUTLET", platform: "BO", revenue: 30, orders: 1 },
|
||
]);
|
||
render(<DashboardPage />);
|
||
const c = within(await channelCard());
|
||
for (const [label, value, type] of [
|
||
["Web", "RM 100.00", "WEB"],
|
||
["Counter", "RM 20.00", "COUNTER"],
|
||
["Event", "RM 40.00", "EVENT"],
|
||
["Outlet", "RM 30.00", "OUTLET"],
|
||
]) {
|
||
const link = c.getByRole("link", { name: new RegExp(`${label}.*${value.replace(".", "\\.")}`) });
|
||
expect(link).toHaveAttribute("href", listing(type));
|
||
}
|
||
});
|
||
|
||
test("OUTLET is labelled Outlet, not Counter, and Counter is absent without BO data", async () => {
|
||
auth.modules = ["BO_DASHBOARD", "BO_REPORTS", "BO_WEB_ORDERS"];
|
||
withChannels([
|
||
{ channel: "NORMAL", platform: "CP", revenue: 100, orders: 2 },
|
||
{ channel: "OUTLET", platform: "BO", revenue: 30, orders: 1 },
|
||
]);
|
||
render(<DashboardPage />);
|
||
const c = within(await channelCard());
|
||
expect(c.getByText("Outlet")).toBeInTheDocument();
|
||
expect(c.queryByText("Counter")).toBeNull();
|
||
});
|
||
|
||
test("the Other row shows its revenue but is not a link", async () => {
|
||
auth.modules = ["BO_DASHBOARD", "BO_REPORTS", "BO_WEB_ORDERS"];
|
||
withChannels([{ channel: "NORMAL", platform: "APP", revenue: 5, orders: 1 }]);
|
||
render(<DashboardPage />);
|
||
const el = await channelCard();
|
||
const c = within(el);
|
||
expect(c.getByText("Other")).toBeInTheDocument();
|
||
expect(c.getByText("RM 5.00")).toBeInTheDocument();
|
||
expect(hrefs(el)).not.toContain(listing("OTHER"));
|
||
expect(hrefs(el)).toEqual([listing("WEB"), listing("EVENT"), listing("OUTLET")]);
|
||
});
|
||
|
||
test("without BO_WEB_ORDERS the channel rows render but are not links", async () => {
|
||
withChannels([{ channel: "NORMAL", platform: "CP", revenue: 100, orders: 2 }]);
|
||
render(<DashboardPage />);
|
||
const el = await channelCard();
|
||
expect(hrefs(el)).toEqual([]);
|
||
expect(within(el).getByText("Web")).toBeInTheDocument();
|
||
expect(within(el).getByText("RM 100.00")).toBeInTheDocument();
|
||
});
|
||
|
||
test("without BO_MEMBERS the member figures render but are not links", async () => {
|
||
auth.modules = ["BO_DASHBOARD", "BO_REPORTS"];
|
||
render(<DashboardPage />);
|
||
const region = await kpiRegion();
|
||
expect(within(region).getByText("36")).toBeInTheDocument();
|
||
expect(hrefs(region).some(h => h?.startsWith("/members"))).toBe(false);
|
||
expect(hrefs(region).some(h => h?.startsWith("/reports"))).toBe(true); // proves links exist at all
|
||
expect(hrefs(await card("Rank mix"))).toEqual([]);
|
||
expect(hrefs(await card("Member status"))).toEqual([]);
|
||
expect(within(await card("Rank mix")).getByText("19")).toBeInTheDocument();
|
||
});
|
||
|
||
test("without BO_MEMBERS the New members tile has no hover styling", async () => {
|
||
auth.modules = ["BO_DASHBOARD", "BO_REPORTS"];
|
||
render(<DashboardPage />);
|
||
const region = await kpiRegion();
|
||
const tile = within(region).getByText("New members").closest("[data-slot='kpi']")!.parentElement as HTMLElement;
|
||
const els = [tile, ...Array.from(tile.querySelectorAll("*"))];
|
||
expect(els.filter(e => (e.getAttribute("class") ?? "").includes("hover:"))).toHaveLength(0);
|
||
});
|