Files
co-lite/solvway-admin-portal/app/api/dashboard/leads/route.test.ts
T
ngqileandClaude Opus 5.5 5eead327a6 feat(portal): lead dashboard on the home page
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
2026-12-01 10:25:47 +08:00

39 lines
1.7 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
import { GET } from "./route";
type Req = Parameters<typeof GET>[0];
const req = (url: string) => new Request(`http://localhost${url}`) as unknown as Req;
beforeEach(() => {
mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { captures: 1 } });
mocks.getToken.mockReset().mockResolvedValue("jwt-abc");
});
describe("/api/dashboard/leads", () => {
it("returns 401 without a session", async () => {
mocks.getToken.mockResolvedValue(null);
const res = await GET(req("/api/dashboard/leads"));
expect(res.status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("forwards the query string to mobile-api", async () => {
const res = await GET(req("/api/dashboard/leads?from=2026-11-01&to=2026-11-30&companyId=2"));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ captures: 1 });
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/dashboard/leads?from=2026-11-01&to=2026-11-30&companyId=2", { token: "jwt-abc" });
});
it("passes an upstream INVALID_PERIOD with its code", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Start date must be on or before end date.", status: 400, code: "INVALID_PERIOD" });
const res = await GET(req("/api/dashboard/leads?from=2026-11-30&to=2026-11-01"));
expect(res.status).toBe(400);
expect(((await res.json()) as Record<string, unknown>).code).toBe("INVALID_PERIOD");
});
});