Skip to content

Commit 399bbb0

Browse files
test: added test cases for useFetchMore hook
1 parent 49b0758 commit 399bbb0

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@tanstack/react-table": "^8.15.0",
5454
"@testing-library/jest-dom": "^6.4.2",
5555
"@testing-library/react": "^14.2.2",
56+
"@testing-library/react-hooks": "^7.0.2",
5657
"@testing-library/user-event": "^14.5.2",
5758
"@types/jest": "^29.5.12",
5859
"@types/node": "^20.11.30",
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { useFetchMore } from '../index';
3+
4+
describe('useFetchMore', () => {
5+
it('should call loadMore when scrolled to the bottom of the container', () => {
6+
const loadMoreMock = jest.fn();
7+
const containerRef = { clientHeight: 500, scrollHeight: 1000, scrollTop: 300 } ;
8+
const { result } = renderHook(() =>
9+
useFetchMore({ isFetching: false, loadMore: loadMoreMock, ref: containerRef })
10+
);
11+
const containerRefMock = { current: { clientHeight: 500, scrollHeight: 1000, scrollTop: 800 } };
12+
result.current.fetchMoreOnBottomReached(containerRefMock.current);
13+
14+
expect(loadMoreMock).toHaveBeenCalled();
15+
});
16+
17+
it('should not call loadMore when isFetching is true', () => {
18+
const loadMoreMock = jest.fn();
19+
const containerRef = { current: { clientHeight: 500, scrollHeight: 1000, scrollTop: 300 } };
20+
const { result } = renderHook(() =>
21+
useFetchMore({ isFetching: true, loadMore: loadMoreMock, ref: containerRef })
22+
);
23+
const containerRefMock = { current: { clientHeight: 500, scrollHeight: 1000, scrollTop: 800 } };
24+
result.current.fetchMoreOnBottomReached(containerRefMock.current);
25+
26+
expect(loadMoreMock).not.toHaveBeenCalled();
27+
});
28+
29+
it('should not call loadMore if scrolled to the bottom but already fetching', () => {
30+
const loadMoreMock = jest.fn();
31+
const containerRef = { current: { clientHeight: 500, scrollHeight: 1000, scrollTop: 300 } };
32+
const { result } = renderHook(() =>
33+
useFetchMore({ isFetching: true, loadMore: loadMoreMock, ref: containerRef })
34+
);
35+
const containerRefMock = { current: { clientHeight: 500, scrollHeight: 1000, scrollTop: 800 } };
36+
result.current.fetchMoreOnBottomReached(containerRefMock.current);
37+
38+
expect(loadMoreMock).not.toHaveBeenCalled();
39+
});
40+
});

src/hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export {useDevice} from './useDevice'
2+
export {useFetchMore} from "./useFetchMore"

0 commit comments

Comments
 (0)