Skip to content

Commit

Permalink
test: added test cases for footer custom hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
niloofar-deriv committed Jun 18, 2024
1 parent 09faa03 commit 2fdca1f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/hooks/__tests__/useNavigatorOnline.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { act, renderHook } from '@testing-library/react';
import { useNavigatorOnline } from '../custom-hooks';

describe('useNavigatorOnline', () => {
it('initializes as true when browser is online', () => {
const { result } = renderHook(() => useNavigatorOnline());
expect(result.current).toBe(true);
});

it('sets status to false when offline event is triggered', () => {
const { result } = renderHook(() => useNavigatorOnline());

act(() => {
window.dispatchEvent(new Event('offline'));
});

expect(result.current).toBe(false);
});

it('sets status to true when online event is triggered', () => {
Object.defineProperty(navigator, 'onLine', {
value: false,
writable: true,
});

const { result } = renderHook(() => useNavigatorOnline());

act(() => {
window.dispatchEvent(new Event('online'));
});

expect(result.current).toBe(true);
});

it('removes event listeners on unmount', () => {
const addSpy = jest.spyOn(window, 'addEventListener');
const removeSpy = jest.spyOn(window, 'removeEventListener');
const { unmount } = renderHook(() => useNavigatorOnline());

expect(addSpy).toHaveBeenCalledTimes(2);
unmount();
expect(removeSpy).toHaveBeenCalledTimes(2);
});
});
34 changes: 34 additions & 0 deletions src/hooks/__tests__/useNetworkStatus.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { renderHook } from '@testing-library/react';
import { useNavigatorOnline, useNetworkStatus } from '../custom-hooks';

jest.mock('../custom-hooks/useNavigatorOnline');

describe('useNetworkStatus', () => {
it('initializes as online', () => {
(useNavigatorOnline as jest.Mock).mockReturnValue(true);
const { result } = renderHook(() => useNetworkStatus());
expect(result.current).toBe('online');
});

it('changes status to offline when network is down', () => {
(useNavigatorOnline as jest.Mock).mockReturnValue(false);
const { result } = renderHook(() => useNetworkStatus());
expect(result.current).toBe('offline');
});

it('reacts to changes in network status', () => {
(useNavigatorOnline as jest.Mock).mockReturnValue(true);
const { rerender, result } = renderHook(() => useNetworkStatus());
expect(result.current).toBe('online');

// Simulating network going offline
(useNavigatorOnline as jest.Mock).mockReturnValue(false);
rerender();
expect(result.current).toBe('offline');

// Simulating network coming back online
(useNavigatorOnline as jest.Mock).mockReturnValue(true);
rerender();
expect(result.current).toBe('online');
});
});
46 changes: 46 additions & 0 deletions src/hooks/__tests__/useSyncedTime.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { act, renderHook } from '@testing-library/react';
import { useSyncedTime } from '../custom-hooks';

const initialTime = Math.floor(Date.now() / 1000);

jest.mock('@deriv-com/api-hooks', () => ({
useTime: jest.fn(() => ({ data: initialTime })),
}));

describe('useSyncedTime', () => {
beforeAll(() => {
jest.useFakeTimers();
});

it('initializes with the current date', async () => {
const { result } = renderHook(() => useSyncedTime());
expect(result.current).toBe(initialTime);
});

it('updates server time every second', () => {
const { result } = renderHook(() => useSyncedTime());

act(() => {
jest.advanceTimersByTime(1000);
});

expect(result.current).toBe(initialTime + 1);
});

it('stops updating after unmount', () => {
const { result, unmount } = renderHook(() => useSyncedTime());

act(() => {
jest.advanceTimersByTime(3000);
});

expect(result.current).toBe(initialTime + 3);
unmount();

act(() => {
jest.advanceTimersByTime(1000);
});

expect(result.current).toBe(initialTime + 3);
});
});

0 comments on commit 2fdca1f

Please sign in to comment.