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

94 lines
3.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within, fireEvent } from "@testing-library/react";
import type { CmsFormListItem } from "@/app/lib/cmsForms";
// BABYBOO-54 task 1.1: the /forms 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(() => ({ listForms: vi.fn() }));
vi.mock("@/app/lib/services/adminForms", () => ({ listForms: () => svc.listForms() }));
import FormsPage from "./page";
// API order is deliberately sorted by none of the columns.
const ROWS: CmsFormListItem[] = [
{ id: 1, name: "Newsletter", status: "Y", fieldCount: 2, submissionCount: 3, updatedAt: "2026-10-05T10:15:00" },
{ id: 2, name: "Contact us", status: "N", fieldCount: 3, submissionCount: 1, updatedAt: "2026-11-02T08:00:00" },
{ id: 3, name: "Distributor", status: "Y", fieldCount: 1, submissionCount: 2, updatedAt: "2026-09-20T09:00:00" },
];
beforeEach(() => {
auth.hasModule.mockReset().mockReturnValue(true);
svc.listForms.mockReset().mockResolvedValue(ROWS);
});
async function table() {
await screen.findByText("Newsletter");
return within(screen.getByRole("table", { name: "Forms" }));
}
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("/forms - client-side header sorting", () => {
it("Name header reorders asc then desc", async () => {
render(<FormsPage />);
const t = await table();
expect(column(t, "Name")).toEqual(["Newsletter", "Contact us", "Distributor"]);
click(t, "Name");
expect(column(t, "Name")).toEqual(["Contact us", "Distributor", "Newsletter"]);
expect(t.getByRole("columnheader", { name: "Name" })).toHaveAttribute("aria-sort", "ascending");
click(t, "Name");
expect(column(t, "Name")).toEqual(["Newsletter", "Distributor", "Contact us"]);
expect(t.getByRole("columnheader", { name: "Name" })).toHaveAttribute("aria-sort", "descending");
});
it("Fields header orders numerically", async () => {
render(<FormsPage />);
const t = await table();
click(t, "Fields");
expect(column(t, "Name")).toEqual(["Distributor", "Newsletter", "Contact us"]);
click(t, "Fields");
expect(column(t, "Name")).toEqual(["Contact us", "Newsletter", "Distributor"]);
});
it("Submissions header orders by count", async () => {
render(<FormsPage />);
const t = await table();
click(t, "Submissions");
expect(column(t, "Name")).toEqual(["Contact us", "Distributor", "Newsletter"]);
click(t, "Submissions");
expect(column(t, "Name")).toEqual(["Newsletter", "Distributor", "Contact us"]);
});
it("Status header groups Hidden before Visible, then reverses", async () => {
render(<FormsPage />);
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", async () => {
render(<FormsPage />);
const t = await table();
click(t, "Updated");
expect(column(t, "Name")).toEqual(["Distributor", "Newsletter", "Contact us"]);
click(t, "Updated");
expect(column(t, "Name")).toEqual(["Contact us", "Newsletter", "Distributor"]);
});
it("sorting makes no new request", async () => {
render(<FormsPage />);
const t = await table();
click(t, "Name");
expect(svc.listForms).toHaveBeenCalledTimes(1);
});
});