-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathuseInfiniteScroll.test.tsx
More file actions
177 lines (143 loc) · 4.95 KB
/
useInfiniteScroll.test.tsx
File metadata and controls
177 lines (143 loc) · 4.95 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { RefObject } from 'react';
import { render, cleanup, act } from '@testing-library/react';
import { useInfiniteScroll, UseInfiniteScrollOptions } from '../index';
import { MockIntersectionObserver } from './setup/intersectionObserverMock';
function HookWrapper(props: UseInfiniteScrollOptions) {
const { sentinelRef, isLoading } = useInfiniteScroll(props);
return (
<div>
<div
ref={sentinelRef as RefObject<HTMLDivElement>}
data-testid="sentinel"
/>
{isLoading && <div data-testid="loader" />}
</div>
);
}
describe('useInfiniteScroll hook', () => {
beforeEach(() => {
MockIntersectionObserver.instances = [];
});
afterEach(cleanup);
it('calls next when sentinel intersects', () => {
const next = jest.fn();
render(<HookWrapper next={next} hasMore={true} dataLength={10} />);
act(() => {
MockIntersectionObserver.instances[0].triggerIntersect();
});
expect(next).toHaveBeenCalledTimes(1);
});
it('does not create an observer when hasMore is false', () => {
render(<HookWrapper next={jest.fn()} hasMore={false} dataLength={10} />);
expect(MockIntersectionObserver.instances).toHaveLength(0);
});
it('does not call next twice before dataLength changes (load guard)', () => {
const next = jest.fn();
render(<HookWrapper next={next} hasMore={true} dataLength={10} />);
const observer = MockIntersectionObserver.instances[0];
act(() => {
observer.triggerIntersect();
observer.triggerIntersect();
});
expect(next).toHaveBeenCalledTimes(1);
});
it('isLoading is true after sentinel fires and false after dataLength changes', () => {
const next = jest.fn();
const { getByTestId, queryByTestId, rerender } = render(
<HookWrapper next={next} hasMore={true} dataLength={10} />
);
expect(queryByTestId('loader')).toBeNull();
act(() => {
MockIntersectionObserver.instances[0].triggerIntersect();
});
expect(getByTestId('loader')).toBeTruthy();
rerender(<HookWrapper next={next} hasMore={true} dataLength={20} />);
expect(queryByTestId('loader')).toBeNull();
});
it('resets load guard after dataLength changes so next can fire again', () => {
const next = jest.fn();
const { rerender } = render(
<HookWrapper next={next} hasMore={true} dataLength={10} />
);
act(() => {
MockIntersectionObserver.instances[0].triggerIntersect();
});
expect(next).toHaveBeenCalledTimes(1);
rerender(<HookWrapper next={next} hasMore={true} dataLength={20} />);
act(() => {
MockIntersectionObserver.instances[0].triggerIntersect();
});
expect(next).toHaveBeenCalledTimes(2);
});
it('uses scrollableTarget string id as the IO root', () => {
const target = document.createElement('div');
target.id = 'hookScroll';
document.body.appendChild(target);
render(
<HookWrapper
next={jest.fn()}
hasMore={true}
dataLength={5}
scrollableTarget="hookScroll"
/>
);
expect(MockIntersectionObserver.instances[0].options.root).toBe(target);
document.body.removeChild(target);
});
it('accepts an HTMLElement directly as scrollableTarget', () => {
const target = document.createElement('div');
document.body.appendChild(target);
render(
<HookWrapper
next={jest.fn()}
hasMore={true}
dataLength={5}
scrollableTarget={target}
/>
);
expect(MockIntersectionObserver.instances[0].options.root).toBe(target);
document.body.removeChild(target);
});
it('applies top rootMargin in inverse mode', () => {
render(
<HookWrapper
next={jest.fn()}
hasMore={true}
dataLength={5}
inverse={true}
scrollThreshold={0.8}
/>
);
const { rootMargin } = MockIntersectionObserver.instances[0].options;
expect(rootMargin).toBe('20% 0px 0px 0px');
});
it('applies bottom rootMargin in normal (non-inverse) mode', () => {
render(
<HookWrapper
next={jest.fn()}
hasMore={true}
dataLength={5}
scrollThreshold={0.8}
/>
);
const { rootMargin } = MockIntersectionObserver.instances[0].options;
expect(rootMargin).toBe('0px 0px 20% 0px');
});
it('does not create an observer when sentinel ref is not attached', () => {
function NoRefWrapper(props: UseInfiniteScrollOptions) {
useInfiniteScroll(props);
return <div />;
}
render(<NoRefWrapper next={jest.fn()} hasMore={true} dataLength={5} />);
expect(MockIntersectionObserver.instances).toHaveLength(0);
});
it('does not throw when IntersectionObserver is unavailable (SSR)', () => {
const g = globalThis as Record<string, unknown>;
const original = g['IntersectionObserver'];
delete g['IntersectionObserver'];
expect(() => {
render(<HookWrapper next={jest.fn()} hasMore={true} dataLength={5} />);
}).not.toThrow();
g['IntersectionObserver'] = original;
});
});