67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import Constants from "~/application/Constants";
|
|
import { getPaginationFromCurrentUrl, getTotalPages } from "./RowPaginationHelper";
|
|
|
|
function paginationFor(query: string) {
|
|
return getPaginationFromCurrentUrl(new URLSearchParams(query));
|
|
}
|
|
|
|
describe("getTotalPages", () => {
|
|
it("never renders NaN when an upstream provider reports no page size", () => {
|
|
// Regression guard for the admin notification tables, which showed "1/NAN"
|
|
// whenever the provider call returned nothing (0 / 0).
|
|
expect(getTotalPages(0, 0)).toBe(1);
|
|
expect(getTotalPages(undefined, undefined)).toBe(1);
|
|
expect(getTotalPages(null, null)).toBe(1);
|
|
expect(getTotalPages(Number.NaN, Number.NaN)).toBe(1);
|
|
});
|
|
|
|
it("keeps at least one page for an empty result set", () => {
|
|
expect(getTotalPages(0, 10)).toBe(1);
|
|
});
|
|
|
|
it("rounds partial pages up", () => {
|
|
expect(getTotalPages(41, 10)).toBe(5);
|
|
expect(getTotalPages(40, 10)).toBe(4);
|
|
});
|
|
|
|
it("falls back to the default page size for non-positive sizes", () => {
|
|
expect(getTotalPages(25, -5)).toBe(Math.ceil(25 / Constants.DEFAULT_PAGE_SIZE));
|
|
});
|
|
});
|
|
|
|
describe("RowPaginationHelper", () => {
|
|
it("defaults invalid pagination params before they reach database queries", () => {
|
|
expect(paginationFor("page=abc&pageSize=abc")).toMatchObject({
|
|
page: 1,
|
|
pageSize: Constants.DEFAULT_PAGE_SIZE,
|
|
});
|
|
expect(paginationFor("page=NaN&pageSize=Infinity")).toMatchObject({
|
|
page: 1,
|
|
pageSize: Constants.DEFAULT_PAGE_SIZE,
|
|
});
|
|
});
|
|
|
|
it("rejects decimal, zero, and negative pagination params", () => {
|
|
expect(paginationFor("page=1.5&pageSize=25.5")).toMatchObject({
|
|
page: 1,
|
|
pageSize: Constants.DEFAULT_PAGE_SIZE,
|
|
});
|
|
expect(paginationFor("page=0&pageSize=0")).toMatchObject({
|
|
page: 1,
|
|
pageSize: Constants.DEFAULT_PAGE_SIZE,
|
|
});
|
|
expect(paginationFor("page=-3&pageSize=-20")).toMatchObject({
|
|
page: 1,
|
|
pageSize: Constants.DEFAULT_PAGE_SIZE,
|
|
});
|
|
});
|
|
|
|
it("caps oversized page sizes while preserving valid page numbers", () => {
|
|
expect(paginationFor(`page=3&pageSize=${Constants.MAX_PAGE_SIZE + 1}`)).toMatchObject({
|
|
page: 3,
|
|
pageSize: Constants.MAX_PAGE_SIZE,
|
|
});
|
|
});
|
|
});
|