File size: 3,896 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { renderHook, act } from "@testing-library/react";
import { vi, describe, it, expect, beforeEach } from "vitest";
import { useProgressiveRender } from "./useProgressiveRender";

// Capture the IntersectionObserver callback so we can fire it manually in tests
let intersectionCallback:
  | ((entries: IntersectionObserverEntry[]) => void)
  | null = null;

beforeEach(() => {
  intersectionCallback = null;

  // Must be a real constructor (function, not arrow) so `new IntersectionObserver(...)` works
  global.IntersectionObserver = vi.fn(function (
    this: unknown,
    cb: (entries: IntersectionObserverEntry[]) => void,
  ) {
    intersectionCallback = cb;
    return {
      observe: vi.fn(),
      unobserve: vi.fn(),
      disconnect: vi.fn(),
    };
  }) as unknown as typeof IntersectionObserver;
});

// Helper: fire the captured IntersectionObserver as if sentinel is intersecting
function triggerIntersection() {
  intersectionCallback?.([
    { isIntersecting: true } as IntersectionObserverEntry,
  ]);
}

describe("useProgressiveRender", () => {
  it("shows all items when total < INITIAL_COUNT (10 items)", () => {
    const items = Array.from({ length: 10 }, (_, i) => i);
    const { result } = renderHook(() => useProgressiveRender(items));

    expect(result.current.visibleItems).toHaveLength(10);
    expect(result.current.visibleItems).toEqual(items);
    expect(result.current.hasMore).toBe(false);
  });

  it("shows only INITIAL_COUNT (20) items when total > INITIAL_COUNT (50 items)", () => {
    const items = Array.from({ length: 50 }, (_, i) => i);
    const { result } = renderHook(() => useProgressiveRender(items));

    expect(result.current.visibleItems).toHaveLength(20);
    expect(result.current.hasMore).toBe(true);
  });

  it("loadMore (via IntersectionObserver) increases visibleItems by BATCH_SIZE", () => {
    const items = Array.from({ length: 100 }, (_, i) => i);
    const { result } = renderHook(() => useProgressiveRender(items));

    // Initial state: 20 visible
    expect(result.current.visibleItems).toHaveLength(20);

    // Attach a sentinel so the observer is created, then fire intersection
    act(() => {
      result.current.sentinelRef(document.createElement("div"));
    });

    act(() => {
      triggerIntersection();
    });

    expect(result.current.visibleItems).toHaveLength(40);
    expect(result.current.hasMore).toBe(true);
  });

  it("loadMore clamps at items.length and does not exceed total", () => {
    // 25 items: initial=20, one loadMore should show all 25, not 40
    const items = Array.from({ length: 25 }, (_, i) => i);
    const { result } = renderHook(() => useProgressiveRender(items));

    expect(result.current.visibleItems).toHaveLength(20);
    expect(result.current.hasMore).toBe(true);

    act(() => {
      result.current.sentinelRef(document.createElement("div"));
    });

    act(() => {
      triggerIntersection();
    });

    expect(result.current.visibleItems).toHaveLength(25);
    expect(result.current.hasMore).toBe(false);
  });

  it("resets visibleCount to INITIAL_COUNT when items prop changes", () => {
    const items50 = Array.from({ length: 50 }, (_, i) => i);
    const items5 = Array.from({ length: 5 }, (_, i) => i + 100);

    const { result, rerender } = renderHook(
      ({ items }: { items: number[] }) => useProgressiveRender(items),
      { initialProps: { items: items50 } },
    );

    // Initially 50 items → 20 visible
    expect(result.current.visibleItems).toHaveLength(20);
    expect(result.current.hasMore).toBe(true);

    // Switch to a new shorter list
    rerender({ items: items5 });

    // visibleCount resets to INITIAL_COUNT (20), but list only has 5 → all shown
    expect(result.current.visibleItems).toHaveLength(5);
    expect(result.current.visibleItems).toEqual(items5);
    expect(result.current.hasMore).toBe(false);
  });
});