-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add unit test for usefloating rate hook * chore: add unit test for usefetch more hook * fix: revert changes for hook
- Loading branch information
1 parent
f93dc89
commit 128e220
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import debounce from 'lodash/debounce'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import useFetchMore from '../useFetchMore'; | ||
|
||
jest.mock('lodash/debounce'); | ||
|
||
describe('useFetchMore', () => { | ||
let mockLoadMore: jest.Mock, | ||
containerRef: { | ||
current: { | ||
addEventListener: jest.Mock; | ||
clientHeight: number; | ||
removeEventListener: jest.Mock; | ||
scrollHeight: number; | ||
scrollTop: number; | ||
}; | ||
}; | ||
|
||
beforeEach(() => { | ||
mockLoadMore = jest.fn(); | ||
containerRef = { | ||
current: { | ||
addEventListener: jest.fn(), | ||
clientHeight: 0, | ||
removeEventListener: jest.fn(), | ||
scrollHeight: 0, | ||
scrollTop: 0, | ||
}, | ||
}; | ||
(debounce as jest.Mock).mockImplementation(fn => fn); | ||
}); | ||
|
||
it('should add scroll event listener on mount', () => { | ||
// @ts-expect-error - we don't need to mock all properties | ||
renderHook(() => useFetchMore({ loadMore: mockLoadMore, ref: containerRef })); | ||
|
||
expect(containerRef.current.addEventListener).toHaveBeenCalledWith('scroll', expect.any(Function)); | ||
}); | ||
|
||
it('should remove scroll event listener on unmount', () => { | ||
// @ts-expect-error - we don't need to mock all properties | ||
const { unmount } = renderHook(() => useFetchMore({ loadMore: mockLoadMore, ref: containerRef })); | ||
|
||
unmount(); | ||
|
||
expect(containerRef.current.removeEventListener).toHaveBeenCalledWith('scroll', expect.any(Function)); | ||
}); | ||
|
||
it('should call loadMore when scrolled near the bottom', async () => { | ||
containerRef.current.scrollTop = 100; | ||
containerRef.current.clientHeight = 50; | ||
containerRef.current.scrollHeight = 300; | ||
|
||
// @ts-expect-error - we don't need to mock all properties | ||
renderHook(() => useFetchMore({ loadMore: mockLoadMore, ref: containerRef })); | ||
|
||
await waitFor(() => { | ||
containerRef.current.addEventListener.mock.calls[0][1](); // Simulate scroll event | ||
}); | ||
|
||
expect(mockLoadMore).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import useFloatingRate from '../useFloatingRate'; | ||
|
||
const mockSettings = { | ||
fixed_rate_adverts_end_date: '2021-07-01', | ||
floatRateOffsetLimitString: '0.5', | ||
rateType: 'float', | ||
reachedTargetDate: true, | ||
}; | ||
|
||
jest.mock('../../api', () => ({ | ||
settings: { | ||
useSettings: jest.fn(() => ({ data: mockSettings })), | ||
}, | ||
})); | ||
|
||
describe('useFloatingRate', () => { | ||
it('should return the correct values', () => { | ||
const { result } = renderHook(() => useFloatingRate()); | ||
expect(result.current).toEqual({ | ||
fixedRateAdvertsEndDate: '2021-07-01', | ||
floatRateOffsetLimitString: '0.5', | ||
rateType: 'float', | ||
reachedTargetDate: true, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters