From 5564108e2fef3c4590e4e3c4927babbe76420e8f Mon Sep 17 00:00:00 2001 From: Niloofar Date: Tue, 21 Nov 2023 12:43:02 +0800 Subject: [PATCH] test: added server time tests --- .../footer/__tests__/ServerTime.test.tsx | 11 ++++++++ src/hooks/__tests__/useServerTime.test.ts | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/components/layout/footer/__tests__/ServerTime.test.tsx create mode 100644 src/hooks/__tests__/useServerTime.test.ts diff --git a/src/components/layout/footer/__tests__/ServerTime.test.tsx b/src/components/layout/footer/__tests__/ServerTime.test.tsx new file mode 100644 index 0000000000..215acc5c36 --- /dev/null +++ b/src/components/layout/footer/__tests__/ServerTime.test.tsx @@ -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(); + expect(screen.getByText('2009-02-13 23:31:30 GMT')).toBeInTheDocument(); + }); +}); diff --git a/src/hooks/__tests__/useServerTime.test.ts b/src/hooks/__tests__/useServerTime.test.ts new file mode 100644 index 0000000000..57036468bd --- /dev/null +++ b/src/hooks/__tests__/useServerTime.test.ts @@ -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; + +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); + }); +});