Files
co-lite/solvway-admin-portal/app/events/page.sorting.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

108 lines
4.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within, fireEvent } from "@testing-library/react";
import { memoryRouter } from "@/test/memoryRouter";
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false }));
vi.mock("@/app/lib/AdminAuthContext", () => ({
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
}));
const svc = vi.hoisted(() => ({ listEvents: vi.fn(), listPickupOutlets: vi.fn() }));
vi.mock("@/app/lib/services/adminEvents", () => ({
listEvents: svc.listEvents,
getEvent: vi.fn(),
createEvent: vi.fn(),
updateEvent: vi.fn(),
listPickupOutlets: svc.listPickupOutlets,
}));
vi.mock("@/app/lib/services/adminStock", () => ({
getStockOnHand: () => Promise.resolve({ content: [{ skuVariationId: "1", availableQty: 5 }], totalElements: 1, totalPages: 1, number: 0, size: 200 }),
}));
vi.mock("@/app/components/events/EventCloseOutDialog", () => ({ EventCloseOutDialog: () => null }));
import EventsPage from "./page";
const ev = (id: string, code: string, name: string, venue: string, salesFrom = "2026-09-01T09:00") => ({
id, code, name, venue,
salesFrom, salesTo: "2026-09-07T23:59",
warehouseId: "1", status: "Y" as const,
});
// Default (API) order is deliberately not sorted by name, code or venue.
const EVENTS = [
ev("1", "C-03", "Charlie Expo", "Venue Y", "2026-09-20T09:00"),
ev("2", "A-01", "Alpha Expo", "Venue Z", "2026-09-01T09:00"),
ev("3", "B-02", "Bravo Expo", "Venue X", "2026-09-10T09:00"),
];
beforeEach(() => {
memoryRouter.reset("/events");
auth.hasModule.mockReset().mockReturnValue(true);
svc.listEvents.mockReset().mockResolvedValue(EVENTS);
svc.listPickupOutlets.mockReset().mockResolvedValue([]);
});
async function table() {
await screen.findByText("Charlie Expo");
return within(screen.getByRole("table", { name: "Events" }));
}
function column(t: ReturnType<typeof within>, header: string) {
const idx = t.getAllByRole("columnheader").findIndex((h: HTMLElement) => h.textContent?.trim() === header);
return t.getAllByRole("row").slice(1).map((r: HTMLElement) => within(r).getAllByRole("cell")[idx].textContent);
}
describe("Event setup — client-side header sorting", () => {
it.each([
["Name", ["Charlie Expo", "Alpha Expo", "Bravo Expo"], ["Alpha Expo", "Bravo Expo", "Charlie Expo"]],
["Code", ["C-03", "A-01", "B-02"], ["A-01", "B-02", "C-03"]],
["Venue", ["Venue Y", "Venue Z", "Venue X"], ["Venue X", "Venue Y", "Venue Z"]],
])("%s header reorders asc, desc, then back to the original order", async (header, original, asc) => {
render(<EventsPage />);
const t = await table();
const btn = () => t.getByRole("button", { name: header });
expect(column(t, header)).toEqual(original);
fireEvent.click(btn());
expect(column(t, header)).toEqual(asc);
expect(t.getByRole("columnheader", { name: header })).toHaveAttribute("aria-sort", "ascending");
fireEvent.click(btn());
expect(column(t, header)).toEqual([...asc].reverse());
expect(t.getByRole("columnheader", { name: header })).toHaveAttribute("aria-sort", "descending");
fireEvent.click(btn());
expect(column(t, header)).toEqual(original);
});
it("Sales header reorders by the sales start, asc then desc, without a new request", async () => {
render(<EventsPage />);
const t = await table();
const names = () => column(t, "Name");
expect(t.getByRole("columnheader", { name: "Sales" })).toBeInTheDocument();
fireEvent.click(t.getByRole("button", { name: "Sales" }));
expect(names()).toEqual(["Alpha Expo", "Bravo Expo", "Charlie Expo"]);
expect(t.getByRole("columnheader", { name: "Sales" })).toHaveAttribute("aria-sort", "ascending");
fireEvent.click(t.getByRole("button", { name: "Sales" }));
expect(names()).toEqual(["Charlie Expo", "Bravo Expo", "Alpha Expo"]);
expect(svc.listEvents).toHaveBeenCalledTimes(1);
});
it("does not call the list API again when a header is clicked", async () => {
render(<EventsPage />);
const t = await table();
fireEvent.click(t.getByRole("button", { name: "Name" }));
fireEvent.click(t.getByRole("button", { name: "Name" }));
expect(svc.listEvents).toHaveBeenCalledTimes(1);
});
it("the trailing actions column offers no sort", async () => {
render(<EventsPage />);
const t = await table();
const heads = t.getAllByRole("columnheader");
expect(within(heads[heads.length - 1]).queryByRole("button")).toBeNull();
});
});