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
304 lines
15 KiB
TypeScript
304 lines
15 KiB
TypeScript
import { test, expect, vi, beforeEach } from "vitest";
|
||
import { render, screen, within } from "@testing-library/react";
|
||
import userEvent from "@testing-library/user-event";
|
||
import type { EventDashboardData } from "@/app/lib/services/adminDashboard";
|
||
import type { PosEventProduct, PosSellableEvent } from "@/app/lib/types";
|
||
import { EventDashboard } from "./EventDashboard";
|
||
|
||
const auth = vi.hoisted(() => ({ modules: ["BO_DASHBOARD_EVENT", "BO_POS"] 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) };
|
||
});
|
||
|
||
// add-event-summary: the POS feed carries the sales window and a sellable flag.
|
||
const LIVE = { salesFrom: "2020-01-01T00:00", salesTo: "2099-12-31T23:59", sellable: true };
|
||
const EXPO: PosSellableEvent = { id: "7", name: "KL Baby Expo", warehouseId: "31", ...LIVE };
|
||
const FAIR: PosSellableEvent = { id: "8", name: "Penang Fair", warehouseId: "32", ...LIVE };
|
||
const ENDED: PosSellableEvent = { id: "9", name: "Old Fair", warehouseId: "33", salesFrom: "2020-09-12T10:00", salesTo: "2020-09-14T22:00", sellable: false };
|
||
|
||
const data: EventDashboardData = {
|
||
operator: "Aisyah",
|
||
mine: {
|
||
orders: 2,
|
||
revenue: 55,
|
||
byTender: [
|
||
{ paymentMethodId: 9, paymentType: "PC", amount: 35 },
|
||
{ paymentMethodId: null, paymentType: "DWP", amount: 20 },
|
||
],
|
||
},
|
||
today: { orders: 3, revenue: 105, byTender: [{ paymentMethodId: 9, paymentType: "PC", amount: 85 }] },
|
||
toDate: {
|
||
orders: 4,
|
||
revenue: 115,
|
||
byTender: [
|
||
{ paymentMethodId: 42, paymentType: "PCC", amount: 100 },
|
||
{ paymentMethodId: null, paymentType: "DWP", amount: 15 },
|
||
],
|
||
},
|
||
pendingOrders: 1,
|
||
tenderLabels: { "9": "Cash" },
|
||
};
|
||
|
||
const product = (label: string, availability: PosEventProduct["availability"]): PosEventProduct =>
|
||
({ pPricingId: label, variationId: label, label, unitPrice: 10, productCode: label, sku: "", availability }) as PosEventProduct;
|
||
|
||
beforeEach(() => {
|
||
auth.modules = ["BO_DASHBOARD_EVENT", "BO_POS"];
|
||
api.events.mockReset().mockResolvedValue([EXPO]);
|
||
api.products.mockReset().mockResolvedValue([product("Bottle", "IN_STOCK"), product("Stroller", "SOLD_OUT"), product("Crib", "PRE_ORDER")]);
|
||
api.dashboard.mockReset().mockResolvedValue(data);
|
||
api.summary.mockReset().mockResolvedValue({ byHour: [{ hour: 11, orders: 1, revenue: 40 }, { hour: 13, orders: 2, revenue: 65 }] });
|
||
localStorage.clear();
|
||
});
|
||
|
||
const card = (name: string) => within(screen.getByRole("region", { name }));
|
||
|
||
test("with one ongoing event it shows my sales, the event totals and the pending count", async () => {
|
||
render(<EventDashboard />);
|
||
|
||
expect(await screen.findByRole("heading", { name: "Event dashboard" })).toBeInTheDocument();
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
expect(api.dashboard).toHaveBeenCalledWith("7");
|
||
expect(card("My sales today").getByText("RM 55.00")).toBeInTheDocument();
|
||
expect(card("My sales today").getByText("2 orders")).toBeInTheDocument();
|
||
expect(card("My sales today").getByText("Cash")).toBeInTheDocument();
|
||
expect(card("My sales today").getByText("RM 35.00")).toBeInTheDocument();
|
||
expect(card("My sales today").getByText("Wallet")).toBeInTheDocument();
|
||
expect(card("My sales today").getByText("RM 20.00")).toBeInTheDocument();
|
||
expect(card("Event today").getByText("RM 105.00")).toBeInTheDocument();
|
||
expect(card("Event today").getByText("3 orders")).toBeInTheDocument();
|
||
expect(card("Since the event began").getByText("RM 115.00")).toBeInTheDocument();
|
||
expect(card("Pending payments").getByText("1")).toBeInTheDocument();
|
||
expect(screen.getByText(/signed in as aisyah/i)).toBeInTheDocument();
|
||
expect(screen.getByText(/before refunds/i)).toBeInTheDocument();
|
||
});
|
||
|
||
test("a payment method without a label is named by its id, never left blank", async () => {
|
||
render(<EventDashboard />);
|
||
await screen.findByRole("region", { name: "Since the event began" });
|
||
expect(card("Since the event began").getByText("Method 42")).toBeInTheDocument();
|
||
});
|
||
|
||
test("lists only sold-out and pre-order products as stock alerts", async () => {
|
||
render(<EventDashboard />);
|
||
const alerts = await screen.findByRole("region", { name: "Stock alerts" });
|
||
await within(alerts).findByText("Stroller");
|
||
expect(within(alerts).getByText("Sold out")).toBeInTheDocument();
|
||
expect(within(alerts).getByText("Crib")).toBeInTheDocument();
|
||
expect(within(alerts).getByText("Pre-order")).toBeInTheDocument();
|
||
expect(within(alerts).queryByText("Bottle")).not.toBeInTheDocument();
|
||
expect(api.products).toHaveBeenCalledWith("7");
|
||
});
|
||
|
||
test("says so when no product needs attention", async () => {
|
||
api.products.mockResolvedValue([product("Bottle", "IN_STOCK")]);
|
||
render(<EventDashboard />);
|
||
const alerts = await screen.findByRole("region", { name: "Stock alerts" });
|
||
expect(await within(alerts).findByText("Every product is in stock.")).toBeInTheDocument();
|
||
});
|
||
|
||
test("Open Event POS goes to the POS and remembers the event for it", async () => {
|
||
api.events.mockResolvedValue([EXPO, FAIR]);
|
||
localStorage.setItem("pos.lastEventId", "8");
|
||
render(<EventDashboard />);
|
||
|
||
const open = await screen.findByRole("link", { name: "Open Event POS" });
|
||
expect(open).toHaveAttribute("href", "/pos");
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
expect(api.dashboard).toHaveBeenCalledWith("8"); // restored the last event used
|
||
});
|
||
|
||
test("without BO_POS there is no Open Event POS button", async () => {
|
||
auth.modules = ["BO_DASHBOARD_EVENT"];
|
||
render(<EventDashboard />);
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
expect(screen.queryByRole("link", { name: "Open Event POS" })).not.toBeInTheDocument();
|
||
});
|
||
|
||
test("choosing another event reloads its figures and is remembered for the POS", async () => {
|
||
api.events.mockResolvedValue([EXPO, FAIR]);
|
||
localStorage.setItem("pos.lastEventId", "7");
|
||
render(<EventDashboard />);
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
|
||
await userEvent.click(screen.getByRole("combobox", { name: "Event" }));
|
||
await userEvent.click(await screen.findByRole("option", { name: "Penang Fair" }));
|
||
|
||
await vi.waitFor(() => expect(api.dashboard).toHaveBeenLastCalledWith("8"));
|
||
expect(localStorage.getItem("pos.lastEventId")).toBe("8");
|
||
});
|
||
|
||
test("with several events and none remembered it asks for an event and loads no figures", async () => {
|
||
api.events.mockResolvedValue([EXPO, FAIR]);
|
||
render(<EventDashboard />);
|
||
expect(await screen.findByText("Choose an event to see its figures.")).toBeInTheDocument();
|
||
expect(api.dashboard).not.toHaveBeenCalled();
|
||
});
|
||
|
||
test("with no ongoing event it says so and shows no figures and no error", async () => {
|
||
api.events.mockResolvedValue([]);
|
||
render(<EventDashboard />);
|
||
expect(await screen.findByText("There is no event open for sales right now.")).toBeInTheDocument();
|
||
expect(api.dashboard).not.toHaveBeenCalled();
|
||
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
||
});
|
||
|
||
test("a failed load shows the error with a retry, and Refresh reloads", async () => {
|
||
api.dashboard.mockRejectedValueOnce(new Error("The BO_DASHBOARD_EVENT module is required."));
|
||
render(<EventDashboard />);
|
||
expect(await screen.findByRole("alert")).toHaveTextContent("The BO_DASHBOARD_EVENT module is required.");
|
||
|
||
await userEvent.click(screen.getByRole("button", { name: "Retry" }));
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
|
||
const calls = api.dashboard.mock.calls.length;
|
||
await userEvent.click(screen.getByRole("button", { name: "Refresh" }));
|
||
await vi.waitFor(() => expect(api.dashboard.mock.calls.length).toBe(calls + 1));
|
||
});
|
||
|
||
test("renders the switcher it is given in the header", async () => {
|
||
render(<EventDashboard switcher={<button type="button">switch dashboards</button>} />);
|
||
expect(await screen.findByRole("button", { name: "switch dashboards" })).toBeInTheDocument();
|
||
});
|
||
|
||
test("split-payment legs without a method id are named, and tenders with the same name are added together", async () => {
|
||
api.dashboard.mockResolvedValue({
|
||
...data,
|
||
mine: {
|
||
orders: 2,
|
||
revenue: 45,
|
||
byTender: [
|
||
{ paymentMethodId: 9, paymentType: "PC", amount: 30 }, // recorded cash (method "Cash")
|
||
{ paymentMethodId: null, paymentType: "PC", amount: 10 }, // split-tender cash leg: no method id
|
||
{ paymentMethodId: null, paymentType: "POL", amount: 5 }, // split-tender online leg
|
||
],
|
||
},
|
||
});
|
||
render(<EventDashboard />);
|
||
const mine = await screen.findByRole("region", { name: "My sales today" });
|
||
|
||
expect(within(mine).getAllByText("Cash")).toHaveLength(1);
|
||
expect(within(mine).getByText("RM 40.00")).toBeInTheDocument();
|
||
expect(within(mine).getByText("Online payment")).toBeInTheDocument();
|
||
expect(within(mine).getByText("RM 5.00")).toBeInTheDocument();
|
||
expect(within(mine).queryByText("PC")).not.toBeInTheDocument();
|
||
});
|
||
|
||
test("Open Event POS is a large button on a card that names the chosen event", async () => {
|
||
render(<EventDashboard />);
|
||
const pos = await screen.findByRole("region", { name: "Event POS" });
|
||
expect(within(pos).getByText("KL Baby Expo")).toBeInTheDocument();
|
||
const open = within(pos).getByRole("link", { name: "Open Event POS" });
|
||
expect(open).toHaveAttribute("href", "/pos");
|
||
expect(open).toHaveAttribute("data-size", "lg");
|
||
// The header no longer carries its own small copy of the button.
|
||
expect(screen.getAllByRole("link", { name: "Open Event POS" })).toHaveLength(1);
|
||
});
|
||
|
||
test("without BO_POS there is no Event POS card", async () => {
|
||
auth.modules = ["BO_DASHBOARD_EVENT"];
|
||
render(<EventDashboard />);
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
expect(screen.queryByRole("region", { name: "Event POS" })).not.toBeInTheDocument();
|
||
});
|
||
|
||
test("a long event name is truncated in the event select and shown in full on hover", async () => {
|
||
const long = "BABYBOO MV EXPO 16 - 19 SEP 2026 AT MID VALLEY MEGAMALL EXHIBITION CENTRE HALL 2";
|
||
api.events.mockResolvedValue([{ ...EXPO, name: long }]);
|
||
render(<EventDashboard />);
|
||
const trigger = await screen.findByRole("combobox", { name: "Event" });
|
||
const name = within(trigger).getByText(long);
|
||
expect(name).toHaveClass("truncate");
|
||
expect(name).toHaveAttribute("title", long);
|
||
});
|
||
|
||
test("names a Gift card wallet tender instead of showing its code (add-gift-card-wallet)", async () => {
|
||
api.dashboard.mockResolvedValue({
|
||
...data,
|
||
mine: { orders: 1, revenue: 30, byTender: [{ paymentMethodId: null, paymentType: "DWG", amount: 30 }] },
|
||
});
|
||
render(<EventDashboard />);
|
||
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
expect(card("My sales today").getByText("Gift card wallet")).toBeInTheDocument();
|
||
expect(card("My sales today").queryByText("DWG")).not.toBeInTheDocument();
|
||
});
|
||
|
||
test("an ended event in the POS feed is not offered and does not stop the only live event being chosen (add-event-summary)", async () => {
|
||
api.events.mockResolvedValue([EXPO, ENDED]);
|
||
render(<EventDashboard />);
|
||
|
||
await screen.findByRole("region", { name: "My sales today" });
|
||
expect(api.dashboard).toHaveBeenCalledWith("7");
|
||
expect(api.dashboard).not.toHaveBeenCalledWith("9");
|
||
});
|
||
|
||
// Feedback 2026-10-26 (owner, 28 Oct): event managers will sign in to this dashboard, so it must only
|
||
// fetch event data. Today by hour comes from the Event summary endpoint, and only for a user who also
|
||
// holds BO_EVENT_SUMMARY.
|
||
test("without BO_EVENT_SUMMARY it never calls the summary endpoint and shows no hourly chart", async () => {
|
||
render(<EventDashboard />);
|
||
await screen.findByRole("region", { name: "Event today" });
|
||
expect(api.summary).not.toHaveBeenCalled();
|
||
expect(screen.queryByText("Today by hour")).not.toBeInTheDocument();
|
||
});
|
||
|
||
test("with BO_EVENT_SUMMARY a live event shows today by hour, from today's summary", async () => {
|
||
auth.modules = ["BO_DASHBOARD_EVENT", "BO_POS", "BO_EVENT_SUMMARY"];
|
||
render(<EventDashboard />);
|
||
const today = await screen.findByRole("region", { name: "Event today" });
|
||
expect(await within(today).findByText("Today by hour")).toBeInTheDocument();
|
||
const [id, range] = api.summary.mock.calls[0];
|
||
expect(id).toBe("7");
|
||
expect(range.from).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||
expect(range.to).toBe(range.from);
|
||
expect(within(today).getByText("Busiest hour: 1pm–2pm")).toBeInTheDocument();
|
||
});
|
||
|
||
test("a refused summary only hides the hourly chart", async () => {
|
||
auth.modules = ["BO_DASHBOARD_EVENT", "BO_EVENT_SUMMARY"];
|
||
api.summary.mockRejectedValue(new Error("The BO_EVENT_SUMMARY module is required."));
|
||
render(<EventDashboard />);
|
||
const today = await screen.findByRole("region", { name: "Event today" });
|
||
await vi.waitFor(() => expect(api.summary).toHaveBeenCalled());
|
||
expect(within(today).queryByText("Today by hour")).not.toBeInTheDocument();
|
||
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
||
expect(within(today).getByText("RM 105.00")).toBeInTheDocument();
|
||
});
|
||
|
||
test("pending payments above zero say they need attention, not by colour alone", async () => {
|
||
render(<EventDashboard />);
|
||
const pending = await screen.findByRole("region", { name: "Pending payments" });
|
||
expect(within(pending).getByText("Needs attention")).toBeInTheDocument();
|
||
});
|
||
|
||
test("stock alerts are grouped into sold out and pre-order, each with its count", async () => {
|
||
render(<EventDashboard />);
|
||
const alerts = await screen.findByRole("region", { name: "Stock alerts" });
|
||
const sold = await within(alerts).findByRole("region", { name: "Sold out" });
|
||
expect(within(sold).getByText("Stroller")).toBeInTheDocument();
|
||
expect(within(sold).getByText("1")).toBeInTheDocument();
|
||
expect(within(within(alerts).getByRole("region", { name: "Pre-order" })).getByText("Crib")).toBeInTheDocument();
|
||
});
|
||
|
||
test("my sales show my share of today's event sales", async () => {
|
||
render(<EventDashboard />);
|
||
const mine = await screen.findByRole("region", { name: "My sales today" });
|
||
// 55 of 105 = 52%; the rest of the team 48%.
|
||
expect(within(mine).getByText("52%")).toBeInTheDocument();
|
||
expect(within(mine).getByText("48%")).toBeInTheDocument();
|
||
});
|