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

85 lines
3.6 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within, fireEvent } from "@testing-library/react";
import type { CmsPageListItem } from "@/app/lib/cmsPages";
// BABYBOO-54 task 1.1: the /pages 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(() => ({ listPages: vi.fn() }));
vi.mock("@/app/lib/services/adminPages", () => ({ listPages: () => svc.listPages() }));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
import { memoryRouter } from "@/test/memoryRouter";
import PagesPage from "./page";
// API order is deliberately sorted by none of title, address, status or updated.
const ROWS: CmsPageListItem[] = [
{ id: 1, slug: "returns", title: "Returns policy", status: "Y", updatedAt: "2026-10-05T10:15:00" },
{ id: 2, slug: "about-us", title: "About us", status: "N", updatedAt: "2026-11-02T08:00:00" },
{ id: 3, slug: "shipping", title: "Shipping info", status: "Y", updatedAt: "2026-09-20T09:00:00" },
];
beforeEach(() => {
memoryRouter.reset("/pages");
auth.hasModule.mockReset().mockReturnValue(true);
svc.listPages.mockReset().mockResolvedValue(ROWS);
});
async function table() {
await screen.findByText("Returns policy");
return within(screen.getByRole("table", { name: "Pages" }));
}
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("/pages - client-side header sorting", () => {
it.each([
["Title", ["Returns policy", "About us", "Shipping info"], ["About us", "Returns policy", "Shipping info"]],
["Address", ["/pages/returns", "/pages/about-us", "/pages/shipping"], ["/pages/about-us", "/pages/returns", "/pages/shipping"]],
])("%s header reorders asc then desc", async (header, original, asc) => {
render(<PagesPage />);
const t = await table();
expect(column(t, header)).toEqual(original);
click(t, header);
expect(column(t, header)).toEqual(asc);
expect(t.getByRole("columnheader", { name: header })).toHaveAttribute("aria-sort", "ascending");
click(t, header);
expect(column(t, header)).toEqual([...asc].reverse());
expect(t.getByRole("columnheader", { name: header })).toHaveAttribute("aria-sort", "descending");
});
it("Status header groups Hidden before Visible, then reverses", async () => {
render(<PagesPage />);
const t = await table();
click(t, "Status");
expect(column(t, "Status")).toEqual(["Hidden", "Visible", "Visible"]);
click(t, "Status");
expect(column(t, "Status")).toEqual(["Visible", "Visible", "Hidden"]);
});
it("Updated header orders by date, not by the displayed text", async () => {
render(<PagesPage />);
const t = await table();
click(t, "Updated");
expect(column(t, "Title")).toEqual(["Shipping info", "Returns policy", "About us"]);
click(t, "Updated");
expect(column(t, "Title")).toEqual(["About us", "Returns policy", "Shipping info"]);
});
it("sorting makes no new request", async () => {
render(<PagesPage />);
const t = await table();
click(t, "Title");
click(t, "Title");
expect(svc.listPages).toHaveBeenCalledTimes(1);
});
});