Skip to content

Commit

Permalink
Nada/feq 2396/unit test (#299)
Browse files Browse the repository at this point in the history
* chore: add unit test for usefloating rate hook

* chore: add unit test for usefetch more hook

* fix: revert changes for hook
  • Loading branch information
nada-deriv authored Aug 20, 2024
1 parent f93dc89 commit 128e220
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/hooks/custom-hooks/__tests__/useFetchMore.spec.ts
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();
});
});
27 changes: 27 additions & 0 deletions src/hooks/custom-hooks/__tests__/useFloatingRate.spec.ts
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,
});
});
});
2 changes: 2 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,5 @@ export type TInitialData = {
paymentMethod: number[] | string[];
selectedCountries: string[];
};

export type TRateType = 'fixed' | 'float';

0 comments on commit 128e220

Please sign in to comment.