|
| 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 | +}); |
0 commit comments