-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathbottom.test.tsx
More file actions
99 lines (84 loc) · 2.33 KB
/
bottom.test.tsx
File metadata and controls
99 lines (84 loc) · 2.33 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
import { render, cleanup, act } from '@testing-library/react';
import InfiniteScroll from '../index';
import { MockIntersectionObserver } from './setup/intersectionObserverMock';
describe('bottom detection triggers next', () => {
beforeEach(() => {
MockIntersectionObserver.instances = [];
});
afterEach(cleanup);
it('calls next when sentinel intersects (height container)', () => {
const next = jest.fn();
render(
<InfiniteScroll
dataLength={0}
loader={'Loading...'}
hasMore={true}
next={next}
height={100}
scrollThreshold="0px"
>
<div />
</InfiniteScroll>
);
act(() => {
MockIntersectionObserver.instances[0].triggerIntersect();
});
expect(next).toHaveBeenCalled();
});
it('does not call next when hasMore is false', () => {
const next = jest.fn();
render(
<InfiniteScroll
dataLength={0}
loader={'Loading...'}
hasMore={false}
next={next}
height={100}
>
<div />
</InfiniteScroll>
);
// No observer is created when hasMore=false (no sentinel rendered)
expect(MockIntersectionObserver.instances).toHaveLength(0);
expect(next).not.toHaveBeenCalled();
});
it('does not call next twice before dataLength changes', () => {
const next = jest.fn();
render(
<InfiniteScroll
dataLength={0}
loader={'Loading...'}
hasMore={true}
next={next}
height={100}
>
<div />
</InfiniteScroll>
);
const observer = MockIntersectionObserver.instances[0];
act(() => {
observer.triggerIntersect();
observer.triggerIntersect(); // second fire before dataLength changes
});
expect(next).toHaveBeenCalledTimes(1);
});
it('uses null root (viewport) in window scroll mode', () => {
const next = jest.fn();
render(
<InfiniteScroll
dataLength={0}
loader={'Loading...'}
hasMore={true}
next={next}
>
<div />
</InfiniteScroll>
);
// No height, no scrollableTarget → root must be null (viewport IO)
expect(MockIntersectionObserver.instances[0].options.root).toBeNull();
act(() => {
MockIntersectionObserver.instances[0].triggerIntersect();
});
expect(next).toHaveBeenCalled();
});
});