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
113 lines
5.4 KiB
TypeScript
113 lines
5.4 KiB
TypeScript
import { test, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, within } from "@testing-library/react";
|
|
import type { EventDashboardData } from "@/app/lib/services/adminDashboard";
|
|
import type { PosEventProduct, PosSellableEvent } from "@/app/lib/types";
|
|
import { EventDashboard } from "./EventDashboard";
|
|
|
|
const TODAY = "2026-11-01";
|
|
vi.mock("@/app/lib/malaysiaPeriod", async () => {
|
|
const actual = await vi.importActual<typeof import("@/app/lib/malaysiaPeriod")>("@/app/lib/malaysiaPeriod");
|
|
return { ...actual, malaysiaToday: () => TODAY };
|
|
});
|
|
|
|
const FULL = ["BO_DASHBOARD_EVENT", "BO_POS", "BO_EVENT_SUMMARY", "BO_WEB_ORDERS", "BO_PRODUCTS"];
|
|
const auth = vi.hoisted(() => ({ modules: [] as string[] }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ loading: false, hasModule: (m: string) => auth.modules.includes(m) }),
|
|
}));
|
|
|
|
const api = vi.hoisted(() => ({ events: vi.fn(), products: vi.fn(), dashboard: vi.fn(), summary: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminEventSummary", () => ({
|
|
getEventSummary: (id: string, range?: { from: string; to: string }) => api.summary(id, range),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminPos", () => ({
|
|
listSellableEvents: () => api.events(),
|
|
listEventProducts: (id?: string) => api.products(id),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminDashboard", async () => {
|
|
const actual = await vi.importActual<typeof import("@/app/lib/services/adminDashboard")>("@/app/lib/services/adminDashboard");
|
|
return { ...actual, getEventDashboard: (id: string) => api.dashboard(id) };
|
|
});
|
|
|
|
const EXPO: PosSellableEvent = { id: "7", name: "KL Baby Expo", warehouseId: "31", salesFrom: "2026-10-12T10:00", salesTo: "2099-12-31T23:59", sellable: true };
|
|
|
|
const data: EventDashboardData = {
|
|
operator: "Aisyah",
|
|
mine: { orders: 2, revenue: 55, byTender: [] },
|
|
today: { orders: 3, revenue: 105, byTender: [] },
|
|
toDate: { orders: 4, revenue: 115, byTender: [] },
|
|
pendingOrders: 1,
|
|
tenderLabels: {},
|
|
};
|
|
|
|
const product = (label: string, availability: PosEventProduct["availability"], productCode: string | null = label): PosEventProduct =>
|
|
({ pPricingId: label, variationId: label, label, unitPrice: 10, productCode, sku: "", availability }) as PosEventProduct;
|
|
|
|
beforeEach(() => {
|
|
auth.modules = [...FULL];
|
|
api.events.mockReset().mockResolvedValue([EXPO]);
|
|
api.products.mockReset().mockResolvedValue([product("Stroller", "SOLD_OUT", "ABC 1"), product("Crib", "PRE_ORDER", "CRB-2")]);
|
|
api.dashboard.mockReset().mockResolvedValue(data);
|
|
api.summary.mockReset().mockResolvedValue({ byHour: [] });
|
|
localStorage.clear();
|
|
});
|
|
|
|
const card = async (name: string) => within(await screen.findByRole("region", { name }));
|
|
|
|
test("Event today figure links to the event summary for today", async () => {
|
|
render(<EventDashboard />);
|
|
const link = (await card("Event today")).getByRole("link", { name: /RM 105\.00/ });
|
|
expect(link).toHaveAttribute("href", `/events/summary?event=7&from=${TODAY}&to=${TODAY}`);
|
|
});
|
|
|
|
test("Since the event began links from the sales start date to today", async () => {
|
|
render(<EventDashboard />);
|
|
const link = (await card("Since the event began")).getByRole("link", { name: /RM 115\.00/ });
|
|
expect(link).toHaveAttribute("href", `/events/summary?event=7&from=2026-10-12&to=${TODAY}`);
|
|
});
|
|
|
|
test("pending count links to the pending event web orders", async () => {
|
|
render(<EventDashboard />);
|
|
const link = (await card("Pending payments")).getByRole("link");
|
|
expect(link).toHaveAttribute("href", "/orders/web?type=EVENT&status=P&event=7");
|
|
});
|
|
|
|
test("My sales today links to the operator's active event orders for today", async () => {
|
|
render(<EventDashboard />);
|
|
const link = (await card("My sales today")).getByRole("link", { name: /RM 55\.00/ });
|
|
expect(link).toHaveAttribute("href", `/orders/web?type=EVENT&status=Y&event=7&mine=Y&from=${TODAY}&to=${TODAY}`);
|
|
});
|
|
|
|
test("sold-out and pre-order items link to the product search, code URL-encoded", async () => {
|
|
render(<EventDashboard />);
|
|
const stock = await card("Stock alerts");
|
|
expect(await stock.findByRole("link", { name: /Stroller/ })).toHaveAttribute("href", "/products?si.q=ABC%201");
|
|
expect(stock.getByRole("link", { name: /Crib/ })).toHaveAttribute("href", "/products?si.q=CRB-2");
|
|
expect(stock.getByRole("link", { name: /Stroller/ })).toHaveClass("py-1.5");
|
|
});
|
|
|
|
test("an event without a sales start leaves Since the event began unlinked but shows the figure", async () => {
|
|
api.events.mockResolvedValue([{ ...EXPO, salesFrom: "" }]);
|
|
render(<EventDashboard />);
|
|
const c = await card("Since the event began");
|
|
expect(c.getByText("RM 115.00")).toBeInTheDocument();
|
|
expect(c.queryByRole("link")).not.toBeInTheDocument();
|
|
});
|
|
|
|
test("without the web orders module the pending count shows but is not a link", async () => {
|
|
auth.modules = FULL.filter(m => m !== "BO_WEB_ORDERS");
|
|
render(<EventDashboard />);
|
|
const c = await card("Pending payments");
|
|
expect(c.getByText("1")).toBeInTheDocument();
|
|
expect(c.queryByRole("link")).not.toBeInTheDocument();
|
|
});
|
|
|
|
test("a stock-alert item without a product code stays plain text", async () => {
|
|
api.products.mockResolvedValue([product("Stroller", "SOLD_OUT", null)]);
|
|
render(<EventDashboard />);
|
|
const stock = await card("Stock alerts");
|
|
expect(await stock.findByText("Stroller")).toBeInTheDocument();
|
|
expect(stock.queryByRole("link")).not.toBeInTheDocument();
|
|
expect(stock.getByText("Stroller").closest(".py-1\\.5")).not.toBeNull();
|
|
});
|