import { afterEach, describe, expect, it, vi } from "vitest"; import { render, renderHook } from "@testing-library/react"; // vitest.setup.ts pins useFitPageSize to 20 for page tests; this file tests the real hook. vi.unmock("@/app/lib/hooks/useFitPageSize"); import { fitPageSize, useFitPageSize } from "./useFitPageSize"; // fit-admin-listing-pages: a listing requests as many rows as fit between its table's top and the // bottom of the screen, 10 to 50. describe("fitPageSize", () => { it("fits more rows on a taller screen", () => { // Rows are sized as two-line rows (49px, a member code above a name) so they never overflow. expect(fitPageSize(1080, 200)).toBe(15); expect(fitPageSize(1440, 300)).toBe(20); }); it("never goes below 10 or above 50", () => { expect(fitPageSize(400, 300)).toBe(10); expect(fitPageSize(5000, 0)).toBe(50); }); }); describe("useFitPageSize", () => { const original = window.innerHeight; afterEach(() => { Object.defineProperty(window, "innerHeight", { value: original, configurable: true }); }); const setHeight = (h: number) => Object.defineProperty(window, "innerHeight", { value: h, configurable: true }); it("measures from the table's top once the table is attached", () => { setHeight(1440); let size: number | null = null; function Listing() { const { tableRef, pageSize } = useFitPageSize(); size = pageSize; return
; } vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue({ top: 300 } as DOMRect); render(