From 128e220a9b9bc43e94aef9599bce6a3a1a239cd5 Mon Sep 17 00:00:00 2001 From: nada-deriv <122768621+nada-deriv@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:16:19 +0400 Subject: [PATCH] Nada/feq 2396/unit test (#299) * chore: add unit test for usefloating rate hook * chore: add unit test for usefetch more hook * fix: revert changes for hook --- .../__tests__/useFetchMore.spec.ts | 63 +++++++++++++++++++ .../__tests__/useFloatingRate.spec.ts | 27 ++++++++ types.ts | 2 + 3 files changed, 92 insertions(+) create mode 100644 src/hooks/custom-hooks/__tests__/useFetchMore.spec.ts create mode 100644 src/hooks/custom-hooks/__tests__/useFloatingRate.spec.ts 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';