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
146 lines
6.6 KiB
TypeScript
146 lines
6.6 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 row = (id: string, availability: PosEventProduct["availability"], productCode: string | null, productName: string | undefined, variantName: string | undefined, label: string): PosEventProduct =>
|
|
({ pPricingId: id, variationId: id, label, productCode, productName, variantName, unitPrice: 10, sku: "", availability }) as PosEventProduct;
|
|
|
|
const Y = "YELLOW LABEL ITEM 003";
|
|
const yellow = (id: string, v: string, a: PosEventProduct["availability"] = "SOLD_OUT") =>
|
|
row(id, a, "YELLOW-003", Y, v, `${Y} - ${v}`);
|
|
|
|
beforeEach(() => {
|
|
auth.modules = [...FULL];
|
|
api.events.mockReset().mockResolvedValue([EXPO]);
|
|
api.products.mockReset().mockResolvedValue([
|
|
yellow("1", "black / S"),
|
|
row("9", "SOLD_OUT", "OTHER-1", "Other Item", "red", "Other Item - red"),
|
|
yellow("2", "grey / S"),
|
|
yellow("3", "white / M"),
|
|
]);
|
|
api.dashboard.mockReset().mockResolvedValue(data);
|
|
api.summary.mockReset().mockResolvedValue({ byHour: [] });
|
|
localStorage.clear();
|
|
});
|
|
|
|
const section = async (name: string) => {
|
|
const alerts = await screen.findByRole("region", { name: "Stock alerts" });
|
|
return within(await within(alerts).findByRole("region", { name }));
|
|
};
|
|
|
|
test("a product with many sold-out variants is listed once, linking to its product search", async () => {
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
const links = await sold.findAllByRole("link");
|
|
expect(links).toHaveLength(2);
|
|
expect(sold.getAllByText(Y)).toHaveLength(1);
|
|
expect(sold.getByRole("link", { name: new RegExp(Y) })).toHaveAttribute("href", "/products?si.q=YELLOW-003");
|
|
});
|
|
|
|
test("the product row lists its variant names, full list in the title", async () => {
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
const line = await sold.findByText("black / S, grey / S, white / M");
|
|
expect(line).toHaveAttribute("title", "black / S, grey / S, white / M");
|
|
});
|
|
|
|
test("the count badge counts products, not variants", async () => {
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
await sold.findByText(Y);
|
|
expect(sold.getByText("2")).toBeInTheDocument();
|
|
expect(sold.queryByText("4")).not.toBeInTheDocument();
|
|
});
|
|
|
|
test("a product whose only variant is DEFAULT shows no variant line and no DEFAULT", async () => {
|
|
api.products.mockResolvedValue([row("1", "SOLD_OUT", "SOLO-1", "Solo Item", "DEFAULT", "Solo Item - DEFAULT")]);
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
expect(await sold.findByText("Solo Item")).toBeInTheDocument();
|
|
expect(sold.queryByText(/DEFAULT/)).not.toBeInTheDocument();
|
|
expect(sold.getByRole("link").querySelector("[title]")).toBeNull();
|
|
});
|
|
|
|
test("blank and DEFAULT variant names are dropped from the variant line", async () => {
|
|
api.products.mockResolvedValue([
|
|
row("1", "SOLD_OUT", "P-1", "Mixed", "DEFAULT", "Mixed - DEFAULT"),
|
|
row("2", "SOLD_OUT", "P-1", "Mixed", " ", "Mixed"),
|
|
row("3", "SOLD_OUT", "P-1", "Mixed", "blue", "Mixed - blue"),
|
|
]);
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
expect(await sold.findByText("blue")).toBeInTheDocument();
|
|
expect(sold.queryByText(/DEFAULT/)).not.toBeInTheDocument();
|
|
});
|
|
|
|
test("pre-order products are grouped the same way", async () => {
|
|
api.products.mockResolvedValue([yellow("1", "black / S", "PRE_ORDER"), yellow("2", "grey / S", "PRE_ORDER")]);
|
|
render(<EventDashboard />);
|
|
const pre = await section("Pre-order");
|
|
expect(await pre.findAllByRole("link")).toHaveLength(1);
|
|
expect(pre.getByText("black / S, grey / S")).toBeInTheDocument();
|
|
expect(pre.getByText("1")).toBeInTheDocument();
|
|
});
|
|
|
|
test("rows without a productName fall back to the label", async () => {
|
|
api.products.mockResolvedValue([row("1", "SOLD_OUT", "L-1", undefined, undefined, "Legacy Label")]);
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
expect(await sold.findByRole("link", { name: /Legacy Label/ })).toHaveAttribute("href", "/products?si.q=L-1");
|
|
});
|
|
|
|
test("rows without a productCode are not merged together", async () => {
|
|
api.products.mockResolvedValue([row("1", "SOLD_OUT", null, "Loose A", "x", "Loose A"), row("2", "SOLD_OUT", null, "Loose B", "y", "Loose B")]);
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
expect(await sold.findByText("Loose A")).toBeInTheDocument();
|
|
expect(sold.getByText("Loose B")).toBeInTheDocument();
|
|
expect(sold.getByText("2")).toBeInTheDocument();
|
|
});
|
|
|
|
test("the alert list does not scroll inside a fixed-height box", async () => {
|
|
render(<EventDashboard />);
|
|
const sold = await section("Sold out");
|
|
const ul = (await sold.findByText(Y)).closest("ul");
|
|
expect(ul).not.toBeNull();
|
|
expect(ul!.className).not.toMatch(/max-h-48|overflow-y-auto/);
|
|
});
|