Files
co-lite/solvway-admin-portal/app/collections/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

103 lines
4.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within, fireEvent } from "@testing-library/react";
import type { CollectionListItem } from "@/app/lib/collections";
// BABYBOO-54 task 1.1: the /collections list is unpaged, so every data header sorts in the browser.
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(() => ({ listCollections: vi.fn() }));
vi.mock("@/app/lib/services/adminCollections", () => ({ listCollections: () => svc.listCollections() }));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
import { memoryRouter } from "@/test/memoryRouter";
import CollectionsPage from "./page";
// API order is deliberately sorted by none of title, type, products, status or updated.
const ROWS: CollectionListItem[] = [
{ id: 1, slug: "newborn", title: "Newborn", type: "MANUAL", status: "Y", productCount: 12, updatedAt: "2026-10-05T10:15:00" },
{ id: 2, slug: "our-picks", title: "Our picks", type: "AUTOMATIC", status: "N", productCount: 3, updatedAt: "2026-11-02T08:00:00" },
{ id: 3, slug: "gifts", title: "Gifts", type: "MANUAL", status: "Y", productCount: 100, updatedAt: "2026-09-20T09:00:00" },
];
beforeEach(() => {
memoryRouter.reset("/collections");
auth.hasModule.mockReset().mockReturnValue(true);
svc.listCollections.mockReset().mockResolvedValue(ROWS);
});
async function table() {
await screen.findByText("Newborn");
return within(screen.getByRole("table", { name: "Collections" }));
}
/** Collection titles in on-screen order (the Title cell also carries the slug). */
function titles(t: ReturnType<typeof within>) {
return t.getAllByRole("row").slice(1).map((r: HTMLElement) => within(r).getByRole("link").textContent);
}
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);
}
const click = (t: ReturnType<typeof within>, header: string) => fireEvent.click(t.getByRole("button", { name: header }));
describe("/collections - client-side header sorting", () => {
it("Title header reorders asc then desc", async () => {
render(<CollectionsPage />);
const t = await table();
expect(titles(t)).toEqual(["Newborn", "Our picks", "Gifts"]);
click(t, "Title");
expect(titles(t)).toEqual(["Gifts", "Newborn", "Our picks"]);
expect(t.getByRole("columnheader", { name: "Title" })).toHaveAttribute("aria-sort", "ascending");
click(t, "Title");
expect(titles(t)).toEqual(["Our picks", "Newborn", "Gifts"]);
expect(t.getByRole("columnheader", { name: "Title" })).toHaveAttribute("aria-sort", "descending");
});
it("Type header groups Automatic before Manual, then reverses", async () => {
render(<CollectionsPage />);
const t = await table();
click(t, "Type");
expect(column(t, "Type")).toEqual(["Automatic", "Manual", "Manual"]);
click(t, "Type");
expect(column(t, "Type")).toEqual(["Manual", "Manual", "Automatic"]);
});
it("Products header orders numerically", async () => {
render(<CollectionsPage />);
const t = await table();
click(t, "Products");
expect(column(t, "Products")).toEqual(["3", "12", "100"]);
click(t, "Products");
expect(column(t, "Products")).toEqual(["100", "12", "3"]);
});
it("Status header groups Inactive before Active, then reverses", async () => {
render(<CollectionsPage />);
const t = await table();
click(t, "Status");
expect(column(t, "Status")).toEqual(["Active", "Active", "Inactive"]);
click(t, "Status");
expect(column(t, "Status")).toEqual(["Inactive", "Active", "Active"]);
});
it("Last updated header orders by date", async () => {
render(<CollectionsPage />);
const t = await table();
click(t, "Last updated");
expect(titles(t)).toEqual(["Gifts", "Newborn", "Our picks"]);
click(t, "Last updated");
expect(titles(t)).toEqual(["Our picks", "Newborn", "Gifts"]);
});
it("sorting makes no new request", async () => {
render(<CollectionsPage />);
const t = await table();
click(t, "Title");
expect(svc.listCollections).toHaveBeenCalledTimes(1);
});
});