Users with BO_DASHBOARD get period presets, capture and win tiles, the status funnel, a captures/wins trend and breakdowns by event, salesperson and interest; everyone else keeps the welcome. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
174 lines
9.3 KiB
TypeScript
174 lines
9.3 KiB
TypeScript
import { render, screen, waitFor, within, fireEvent } from "@testing-library/react";
|
||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||
import { pickDate } from "@/test/pickDate";
|
||
|
||
const mocks = vi.hoisted(() => ({
|
||
getLeadDashboard: vi.fn(),
|
||
useAdminAuth: vi.fn(),
|
||
useCompanyScope: vi.fn(),
|
||
}));
|
||
vi.mock("@/app/lib/services/adminDashboard", () => ({ getLeadDashboard: mocks.getLeadDashboard }));
|
||
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
||
vi.mock("@/app/lib/CompanyScope", () => ({ useCompanyScope: mocks.useCompanyScope }));
|
||
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
||
|
||
import { ApiError } from "@/app/lib/clientApi";
|
||
import HomePage from "./page";
|
||
|
||
const DATA = {
|
||
from: "2026-12-01", to: "2026-12-01", bucket: "DAY",
|
||
captures: 40, newLeads: 25, wonInPeriod: 8, medianDaysToWin: 4.5, overdueFollowUps: 3,
|
||
funnel: { OPEN: 11, CONTACTED: 12, QUALIFIED: 13, WON: 8, LOST: 6 },
|
||
trend: [{ bucket: "2026-12-01", captures: 5, wins: 1 }],
|
||
events: [{ eventUuid: "ev-1", name: "Expo KL", companyId: 2, captures: 20, newLeads: 10, won: 5 }],
|
||
salespeople: [
|
||
{ userId: null, name: null, assigned: 7, worked: 2, won: 1, lost: 0, overdue: 4 },
|
||
{ userId: 9, name: null, assigned: 0, worked: 0, won: 0, lost: 0, overdue: 0 },
|
||
{ userId: 5, name: "Sam", assigned: 10, worked: 6, won: 3, lost: 1, overdue: 2 },
|
||
],
|
||
interests: [{ interestId: 1, name: "Catering", captures: 15, won: 4 }],
|
||
};
|
||
const scope = (companyId?: number, showCompanyColumn = false) => ({
|
||
companyId, companies: [], showCompanyColumn, codeOf: (id?: number | null) => (id === 2 ? "CV" : ""), setCompanyId: vi.fn(),
|
||
});
|
||
const tile = (name: string) => within(screen.getByRole("group", { name }));
|
||
|
||
beforeEach(() => {
|
||
mocks.getLeadDashboard.mockReset().mockResolvedValue(DATA);
|
||
mocks.useAdminAuth.mockReset().mockReturnValue({ name: "Alice", loading: false, isSuper: false, hasModule: (m: string) => m === "BO_DASHBOARD" });
|
||
mocks.useCompanyScope.mockReset().mockReturnValue(scope());
|
||
});
|
||
afterEach(() => vi.useRealTimers());
|
||
|
||
describe("home page lead dashboard", () => {
|
||
it("shows only the welcome and never calls the API without the module", () => {
|
||
mocks.useAdminAuth.mockReturnValue({ name: "Alice", loading: false, hasModule: () => false });
|
||
render(<HomePage />);
|
||
expect(screen.getByText("Welcome, Alice")).toBeInTheDocument();
|
||
expect(screen.queryByRole("group", { name: "Captures" })).not.toBeInTheDocument();
|
||
expect(mocks.getLeadDashboard).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("shows the welcome plus tiles with the This month default (no from/to)", async () => {
|
||
render(<HomePage />);
|
||
expect(screen.getByText("Welcome, Alice")).toBeInTheDocument();
|
||
await screen.findByRole("group", { name: "Captures" });
|
||
expect(tile("Captures").getByText("40")).toBeInTheDocument();
|
||
expect(tile("New leads").getByText("25")).toBeInTheDocument();
|
||
expect(tile("Won in period").getByText("8")).toBeInTheDocument();
|
||
expect(tile("Conversion rate").getByText("20%")).toBeInTheDocument();
|
||
expect(tile("Median days to win").getByText("4.5")).toBeInTheDocument();
|
||
expect(tile("Overdue follow-ups").getByText("3")).toBeInTheDocument();
|
||
expect(mocks.getLeadDashboard).toHaveBeenCalledWith({ from: undefined, to: undefined, companyId: undefined });
|
||
});
|
||
|
||
it("shows a dash for a null median and for zero captures", async () => {
|
||
mocks.getLeadDashboard.mockResolvedValue({ ...DATA, captures: 0, medianDaysToWin: null });
|
||
render(<HomePage />);
|
||
await waitFor(() => expect(tile("Conversion rate").getByText("–")).toBeInTheDocument());
|
||
expect(tile("Median days to win").getByText("–")).toBeInTheDocument();
|
||
});
|
||
|
||
it("shows the funnel counts and the trend chart", async () => {
|
||
const { container } = render(<HomePage />);
|
||
const funnel = await screen.findByRole("list", { name: "Lead funnel" });
|
||
const items = within(funnel).getAllByRole("listitem").map((li) => li.textContent);
|
||
expect(items).toEqual(["Open11", "Contacted12", "Qualified13", "Won8", "Lost6"]);
|
||
expect(container.querySelector('[data-slot="chart"]')).toBeInTheDocument();
|
||
});
|
||
|
||
it("shows the by-event, by-salesperson and by-interest tables", async () => {
|
||
render(<HomePage />);
|
||
const ev = within(await screen.findByRole("table", { name: "By event" }));
|
||
expect(ev.getByRole("link", { name: "Expo KL" })).toHaveAttribute("href", "/event-leads/ev-1");
|
||
expect(ev.getByText("25%")).toBeInTheDocument();
|
||
expect(ev.queryByText("Company")).not.toBeInTheDocument();
|
||
const sp = within(screen.getByRole("table", { name: "By salesperson" }));
|
||
expect(sp.getByText("Unassigned")).toBeInTheDocument();
|
||
expect(sp.getByText("Unknown user")).toBeInTheDocument();
|
||
expect(sp.getByText("Sam")).toBeInTheDocument();
|
||
expect(sp.getByText("30%")).toBeInTheDocument();
|
||
expect(sp.getAllByText("–").length).toBe(1);
|
||
const rowText = (name: string) => Array.from(sp.getByText(name).closest("tr")!.querySelectorAll("td")).map((td) => td.textContent);
|
||
expect(rowText("Unassigned")).toEqual(["Unassigned", "7", "2", "1", "0", "14%", "4"]);
|
||
expect(rowText("Sam")).toEqual(["Sam", "10", "6", "3", "1", "30%", "2"]);
|
||
const it = within(screen.getByRole("table", { name: "By interest" }));
|
||
expect(it.getByText("Catering")).toBeInTheDocument();
|
||
});
|
||
|
||
it("shows empty-state lines for empty lists", async () => {
|
||
mocks.getLeadDashboard.mockResolvedValue({ ...DATA, events: [], salespeople: [], interests: [] });
|
||
render(<HomePage />);
|
||
await waitFor(() => expect(screen.getAllByText("No data for this period")).toHaveLength(3));
|
||
});
|
||
|
||
it("sends the previous Malaysian month for Last month", async () => {
|
||
vi.useFakeTimers({ toFake: ["Date"], now: new Date("2026-12-01T10:00:00+08:00") });
|
||
render(<HomePage />);
|
||
fireEvent.click(screen.getByRole("button", { name: "Last month" }));
|
||
await waitFor(() => expect(mocks.getLeadDashboard).toHaveBeenLastCalledWith({ from: "2026-11-01", to: "2026-11-30", companyId: undefined }));
|
||
});
|
||
|
||
it("uses Malaysian time near midnight UTC and across a year boundary", async () => {
|
||
vi.useFakeTimers({ toFake: ["Date"], now: new Date("2026-12-31T20:00:00Z") }); // 2027-01-01 04:00 in KL
|
||
render(<HomePage />);
|
||
fireEvent.click(screen.getByRole("button", { name: "Last month" }));
|
||
await waitFor(() => expect(mocks.getLeadDashboard).toHaveBeenLastCalledWith({ from: "2026-12-01", to: "2026-12-31", companyId: undefined }));
|
||
});
|
||
|
||
it("rejects a reversed custom range without calling the API", async () => {
|
||
render(<HomePage />);
|
||
await screen.findByRole("group", { name: "Captures" });
|
||
fireEvent.click(screen.getByRole("button", { name: "Custom" }));
|
||
expect(screen.queryByRole("group", { name: "Captures" })).not.toBeInTheDocument();
|
||
mocks.getLeadDashboard.mockClear();
|
||
pickDate("From", "2026-11-20");
|
||
pickDate("To", "2026-11-10");
|
||
expect(await screen.findByText("The start date must not be after the end date.")).toBeInTheDocument();
|
||
expect(screen.queryByRole("group", { name: "Captures" })).not.toBeInTheDocument();
|
||
expect(mocks.getLeadDashboard).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("sends a valid custom range", async () => {
|
||
render(<HomePage />);
|
||
await screen.findByRole("group", { name: "Captures" });
|
||
fireEvent.click(screen.getByRole("button", { name: "Custom" }));
|
||
pickDate("From", "2026-11-10");
|
||
pickDate("To", "2026-11-20");
|
||
await waitFor(() => expect(mocks.getLeadDashboard).toHaveBeenLastCalledWith({ from: "2026-11-10", to: "2026-11-20", companyId: undefined }));
|
||
});
|
||
|
||
it("sends the picked company and shows no Company column", async () => {
|
||
mocks.useCompanyScope.mockReturnValue(scope(2, false));
|
||
render(<HomePage />);
|
||
const ev = within(await screen.findByRole("table", { name: "By event" }));
|
||
expect(mocks.getLeadDashboard).toHaveBeenCalledWith({ from: undefined, to: undefined, companyId: 2 });
|
||
expect(ev.queryByText("Company")).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("on All companies sends no companyId and shows the Company column with the code", async () => {
|
||
mocks.useCompanyScope.mockReturnValue(scope(undefined, true));
|
||
render(<HomePage />);
|
||
const ev = within(await screen.findByRole("table", { name: "By event" }));
|
||
expect(mocks.getLeadDashboard).toHaveBeenCalledWith({ from: undefined, to: undefined, companyId: undefined });
|
||
expect(ev.getByText("Company")).toBeInTheDocument();
|
||
expect(ev.getByText("CV")).toBeInTheDocument();
|
||
});
|
||
|
||
it("shows the API message for INVALID_PERIOD", async () => {
|
||
mocks.getLeadDashboard.mockRejectedValue(new ApiError("Start date must be on or before end date.", 400, { error: "Start date must be on or before end date.", code: "INVALID_PERIOD" }));
|
||
render(<HomePage />);
|
||
expect(await screen.findByText("Start date must be on or before end date.")).toBeInTheDocument();
|
||
expect(screen.queryByRole("button", { name: "Retry" })).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("shows a retryable error for other failures", async () => {
|
||
mocks.getLeadDashboard.mockRejectedValueOnce(new Error("boom"));
|
||
render(<HomePage />);
|
||
expect(await screen.findByText("Could not load the dashboard.")).toBeInTheDocument();
|
||
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
|
||
await screen.findByRole("group", { name: "Captures" });
|
||
expect(tile("Captures").getByText("40")).toBeInTheDocument();
|
||
});
|
||
});
|