|
| 1 | +import { act, renderHook } from "@testing-library/react"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +import { PAGE_SIZE, usePagedRows } from "./paged-rows"; |
| 5 | + |
| 6 | +describe("usePagedRows (#8306)", () => { |
| 7 | + it("does not paginate an empty list — one page, empty slice, no clamping", () => { |
| 8 | + const { result } = renderHook(() => usePagedRows<number>([], 2)); |
| 9 | + expect(result.current.isPaginated).toBe(false); |
| 10 | + expect(result.current.pageCount).toBe(1); |
| 11 | + expect(result.current.page).toBe(0); |
| 12 | + expect(result.current.visible).toEqual([]); |
| 13 | + }); |
| 14 | + |
| 15 | + it("returns the full list unpaginated when rows fit within a single page (at or below the size)", () => { |
| 16 | + // Exactly `pageSize` rows is still a single page: the `rows.length > pageSize` boundary is exclusive. |
| 17 | + const rows = [0, 1, 2, 3]; |
| 18 | + const { result } = renderHook(() => usePagedRows(rows, 4)); |
| 19 | + expect(result.current.isPaginated).toBe(false); |
| 20 | + expect(result.current.pageCount).toBe(1); |
| 21 | + expect(result.current.visible).toBe(rows); |
| 22 | + }); |
| 23 | + |
| 24 | + it("slices rows across multiple pages and follows setPage", () => { |
| 25 | + const rows = [0, 1, 2, 3, 4]; |
| 26 | + const { result } = renderHook(() => usePagedRows(rows, 2)); |
| 27 | + expect(result.current.isPaginated).toBe(true); |
| 28 | + expect(result.current.pageCount).toBe(3); |
| 29 | + expect(result.current.page).toBe(0); |
| 30 | + expect(result.current.visible).toEqual([0, 1]); |
| 31 | + |
| 32 | + act(() => result.current.setPage(2)); |
| 33 | + expect(result.current.page).toBe(2); |
| 34 | + expect(result.current.visible).toEqual([4]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("clamps the active page when rows shrink below the current page's start index", () => { |
| 38 | + const { result, rerender } = renderHook(({ rows }: { rows: number[] }) => usePagedRows(rows, 2), { |
| 39 | + initialProps: { rows: [0, 1, 2, 3, 4, 5] }, |
| 40 | + }); |
| 41 | + act(() => result.current.setPage(2)); |
| 42 | + expect(result.current.page).toBe(2); |
| 43 | + expect(result.current.visible).toEqual([4, 5]); |
| 44 | + |
| 45 | + // The list shrinks to a single page's worth of rows: the stale page-2 index clamps back to the last page. |
| 46 | + rerender({ rows: [0, 1, 2] }); |
| 47 | + expect(result.current.pageCount).toBe(2); |
| 48 | + expect(result.current.page).toBe(1); |
| 49 | + expect(result.current.visible).toEqual([2]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("defaults the page size to PAGE_SIZE (20) when no size is passed", () => { |
| 53 | + const rows = Array.from({ length: 25 }, (_, index) => index); |
| 54 | + const { result } = renderHook(() => usePagedRows(rows)); |
| 55 | + expect(PAGE_SIZE).toBe(20); |
| 56 | + expect(result.current.isPaginated).toBe(true); |
| 57 | + expect(result.current.pageCount).toBe(2); |
| 58 | + expect(result.current.visible).toHaveLength(20); |
| 59 | + expect(result.current.visible[0]).toBe(0); |
| 60 | + expect(result.current.visible[19]).toBe(19); |
| 61 | + }); |
| 62 | +}); |
0 commit comments