import { act, renderHook } from "@testing-library/react"; import { describe, it, expect } from "vitest"; import { useClientPagination } from "./line-pagination-footer"; describe("useClientPagination", () => { it("defaults to page 0 with the given size and derives slice bounds", () => { const { result } = renderHook(() => useClientPagination(250, 100)); expect(result.current.page).toBe(0); expect(result.current.pageSize).toBe(100); expect(result.current.pageCount).toBe(3); expect(result.current.start).toBe(0); expect(result.current.end).toBe(100); }); it("moves between pages and exposes the right slice bounds", () => { const { result } = renderHook(() => useClientPagination(250, 100)); act(() => result.current.setPage(2)); expect(result.current.page).toBe(2); expect(result.current.start).toBe(200); expect(result.current.end).toBe(300); }); it("clamps an out-of-range page to the last page during render", () => { const { result } = renderHook(() => useClientPagination(250, 100)); act(() => result.current.setPage(99)); expect(result.current.page).toBe(2); // pageCount - 1 }); it("resets to the first page when the page size changes", () => { const { result } = renderHook(() => useClientPagination(250, 100)); act(() => result.current.setPage(2)); act(() => result.current.setPageSize(50)); expect(result.current.pageSize).toBe(50); expect(result.current.page).toBe(0); expect(result.current.pageCount).toBe(5); }); it("clamps to a single page when there are no rows", () => { const { result } = renderHook(() => useClientPagination(0, 100)); expect(result.current.pageCount).toBe(1); expect(result.current.page).toBe(0); expect(result.current.start).toBe(0); }); it("re-clamps the page when the row total shrinks below it", () => { const { result, rerender } = renderHook(({ total }) => useClientPagination(total, 100), { initialProps: { total: 250 }, }); act(() => result.current.setPage(2)); expect(result.current.page).toBe(2); // The set dropped to one page worth of rows — the stored page is now out of // range and must clamp on the next render without any effect. rerender({ total: 100 }); expect(result.current.pageCount).toBe(1); expect(result.current.page).toBe(0); }); });