Files
co-lite/solvway-admin-portal/app/events/page.export.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

88 lines
4.6 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { memoryRouter } from "@/test/memoryRouter";
import { formatDateTime } from "@/app/lib/format";
// BABYBOO-64 listing-exports: /events exports the filtered, sorted rows already in memory.
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
const auth = vi.hoisted(() => ({ hasModule: vi.fn() }));
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ hasModule: auth.hasModule, loading: false }) }));
const svc = vi.hoisted(() => ({ listEvents: vi.fn() }));
vi.mock("@/app/lib/services/adminEvents", () => ({
listEvents: svc.listEvents, getEvent: vi.fn(), createEvent: vi.fn(), updateEvent: vi.fn(), listPickupOutlets: vi.fn().mockResolvedValue([]),
}));
vi.mock("@/app/lib/services/adminStock", () => ({
getStockOnHand: vi.fn().mockResolvedValue({ content: [{ skuVariationId: "1", availableQty: 5 }], totalElements: 1, totalPages: 1, number: 0, size: 200 }),
}));
vi.mock("@/app/components/events/EventCloseOutDialog", () => ({ EventCloseOutDialog: () => null }));
const xlsx = vi.hoisted(() => ({ toXlsx: vi.fn(), downloadXlsx: vi.fn() }));
vi.mock("@/app/lib/xlsx", () => xlsx);
import EventsPage from "./page";
const ev = (id: string, code: string, name: string, venue: string, from: string, to: string, status: "P" | "Y" | "N" | "C") =>
({ id, code, name, venue, salesFrom: from, salesTo: to, warehouseId: "9", status });
// Listed out of name order, so a name sort visibly reorders them.
const PJ = ev("2", "PJ-EVENT-01", "PJ Pickup Event", "Sunway Convention Centre", "2026-10-01T09:00", "2026-10-07T23:59", "C");
const KL = ev("1", "KL-EVENT-01", "KL Pickup Event", "KL Convention Centre", "2026-09-01T09:00", "2026-09-07T23:59", "Y");
const GALA = ev("3", "AA-GALA-01", "Aaa Gala", "Penang Hall", "2026-11-01T09:00", "2026-11-02T23:59", "Y");
beforeEach(() => {
memoryRouter.reset("/events");
auth.hasModule.mockReset().mockReturnValue(true);
xlsx.toXlsx.mockReset().mockResolvedValue(new Uint8Array([1]));
xlsx.downloadXlsx.mockReset();
svc.listEvents.mockReset().mockResolvedValue([PJ, KL, GALA]);
});
describe("EventsPage export", () => {
it("exports only the filtered events, in sort order, with the table's columns", async () => {
const user = userEvent.setup();
render(<EventsPage />);
await screen.findByText("KL Pickup Event");
await user.type(screen.getByLabelText("Search events"), "pickup");
await vi.waitFor(() => expect(screen.queryByText("Aaa Gala")).not.toBeInTheDocument());
await user.click(screen.getByRole("button", { name: "Name" })); // ascending
await user.click(screen.getByRole("button", { name: "Export .xlsx" }));
await vi.waitFor(() => expect(xlsx.toXlsx).toHaveBeenCalledTimes(1));
const [rows, columns] = xlsx.toXlsx.mock.calls[0];
expect(columns).toEqual([
{ key: "name", title: "Name" },
{ key: "code", title: "Code" },
{ key: "venue", title: "Venue" },
{ key: "salesFrom", title: "Sales from" },
{ key: "salesTo", title: "Sales to" },
{ key: "status", title: "Status" },
]);
expect(rows).toEqual([
{ name: "KL Pickup Event", code: "KL-EVENT-01", venue: "KL Convention Centre",
salesFrom: formatDateTime(KL.salesFrom), salesTo: formatDateTime(KL.salesTo), status: "Active" },
{ name: "PJ Pickup Event", code: "PJ-EVENT-01", venue: "Sunway Convention Centre",
salesFrom: formatDateTime(PJ.salesFrom), salesTo: formatDateTime(PJ.salesTo), status: "Closed" },
]);
await vi.waitFor(() => expect(xlsx.downloadXlsx).toHaveBeenCalledTimes(1));
expect(xlsx.downloadXlsx.mock.calls[0][0]).toMatch(/^events-\d{4}-\d{2}-\d{2}\.xlsx$/);
});
it("disables the button when the filter matches nothing", async () => {
const user = userEvent.setup();
render(<EventsPage />);
await screen.findByText("KL Pickup Event");
await user.type(screen.getByLabelText("Search events"), "zzz-no-such");
await vi.waitFor(() => expect(screen.getByRole("button", { name: "Export .xlsx" })).toBeDisabled());
});
it("shows ExportFailed and downloads nothing when building the workbook fails", async () => {
xlsx.toXlsx.mockRejectedValue(new Error("boom"));
const user = userEvent.setup();
render(<EventsPage />);
await screen.findByText("KL Pickup Event");
await user.click(screen.getByRole("button", { name: "Export .xlsx" }));
expect(await screen.findByText("Export failed. Please try again.")).toBeInTheDocument();
expect(xlsx.downloadXlsx).not.toHaveBeenCalled();
});
});