diff --git a/src/hooks/custom-hooks/__tests__/useFetchMore.spec.ts b/src/hooks/custom-hooks/__tests__/useFetchMore.spec.ts new file mode 100644 index 00000000..14f3b4c6 --- /dev/null +++ b/src/hooks/custom-hooks/__tests__/useFetchMore.spec.ts @@ -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(); + }); +}); diff --git a/src/hooks/custom-hooks/__tests__/useFloatingRate.spec.ts b/src/hooks/custom-hooks/__tests__/useFloatingRate.spec.ts new file mode 100644 index 00000000..66ab4a83 --- /dev/null +++ b/src/hooks/custom-hooks/__tests__/useFloatingRate.spec.ts @@ -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, + }); + }); +}); diff --git a/types.ts b/types.ts index ac9562db..2bc12ab4 100644 --- a/types.ts +++ b/types.ts @@ -232,3 +232,5 @@ export type TInitialData = { paymentMethod: number[] | string[]; selectedCountries: string[]; }; + +export type TRateType = 'fixed' | 'float';