Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
test: added server time tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niloofar-deriv committed Nov 21, 2023
1 parent bc90d90 commit 5564108
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/components/layout/footer/__tests__/ServerTime.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { render, screen } from '@testing-library/react';
import ServerTime from '../ServerTime';

jest.mock('Hooks/useServerTime', () => () => 1234567890);

describe('ServerTime component', () => {
it('should render the component with correct time format', () => {
render(<ServerTime />);
expect(screen.getByText('2009-02-13 23:31:30 GMT')).toBeInTheDocument();
});
});
25 changes: 25 additions & 0 deletions src/hooks/__tests__/useServerTime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { renderHook } from '@testing-library/react';
import useQuery from 'Api/hooks/useQuery';
import useServerTime from 'Hooks/useServerTime';

jest.mock('Api/hooks/useQuery');

const mockUseQuery = useQuery as jest.MockedFunction<typeof useQuery>;

describe('useServerTime hook', () => {
it('should return initial server time and updates it based on the useQuery result', () => {
// @ts-expect-error need to come up with a way to mock the return type of useQuery
mockUseQuery.mockReturnValue({ data: { time: 1234567890 } });

const { result } = renderHook(() => useServerTime());
expect(result.current).toBe(1234567890);
});

it('should handle useQuery data being null', () => {
// @ts-expect-error need to come up with a way to mock the return type of useQuery
mockUseQuery.mockReturnValue({ data: { time: null } });

const { result } = renderHook(() => useServerTime());
expect(result.current).toBeGreaterThan(0);
});
});

0 comments on commit 5564108

Please sign in to comment.