From 19c03981e2d687779a70e556ba1e1a8f6a456183 Mon Sep 17 00:00:00 2001 From: mheggelund Date: Mon, 18 Mar 2024 09:38:19 +0100 Subject: [PATCH] test: Test useFetchMetadata and useFetchAnalogue hooks, and modelMetadate component initial render. --- .../ModelMetadata/ModelMetadataHooks.test.tsx | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/features/AddModel/ModelMetadata/ModelMetadataHooks.test.tsx diff --git a/src/features/AddModel/ModelMetadata/ModelMetadataHooks.test.tsx b/src/features/AddModel/ModelMetadata/ModelMetadataHooks.test.tsx new file mode 100644 index 0000000..488de39 --- /dev/null +++ b/src/features/AddModel/ModelMetadata/ModelMetadataHooks.test.tsx @@ -0,0 +1,119 @@ +/* eslint-disable testing-library/render-result-naming-convention */ +/* eslint-disable max-lines-per-function */ +import { MsalProvider } from '@azure/msal-react'; +import * as ReactQuery from '@tanstack/react-query'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { cleanup, renderHook, screen, waitFor } from '@testing-library/react'; +import { MsalReactTester } from 'msal-react-tester'; +import { AnalogueModelSourceType } from '../../../api/generated'; +import { useFetchAnalogues } from '../../../hooks/useFetchAnalogues'; +import { useFetchMetadata } from '../../../hooks/useFethcMetadata'; +import { ModelMetadata } from './ModelMetadata'; + +let msalTester: MsalReactTester; +const mockMetadata = { + analogueModelId: '', + name: '', + description: '', + isProcessed: false, + sourceType: AnalogueModelSourceType.DELTARES, + fileUploads: [], + parameters: [], + metadata: [], + analogues: [], + modelAreas: [], +}; +const errors = {}; +function wrapper(props: { children: React.ReactNode }) { + const testQueryClient = new QueryClient(); + return ( + + + {props.children} + + + + ); +} + +const mockMetadataDto = { + metadataId: 'test', + metadataType: 'string', + value: 'string', + success: true, +}; + +const mockAnalogueList = { + analogueId: 'test2', + name: 'string', + description: 'string', + success: true, +}; + +const mockGetData = async (data: any) => { + const mock = jest.spyOn(ReactQuery, 'useQuery'); + + // @ts-ignore because of error + mock.mockImplementation(() => { + return data + ? Promise.resolve({ + data: data, + isLoading: false, + isSuccess: true, + isError: false, + }) + : Promise.reject('error'); + }); + + return mock; +}; + +beforeEach(() => { + // new instance of msal tester for each test + msalTester = new MsalReactTester(); + + // spy all required msal things + msalTester.spyMsal(); +}); + +afterEach(() => { + cleanup(); + msalTester.resetSpyMsal(); + jest.clearAllMocks(); +}); + +test('Calls Mocked metadata api with mock data', async () => { + const mock = await mockGetData(mockMetadataDto); + + const { result } = renderHook(() => useFetchMetadata(), { wrapper }); + + const res = await result.current; + + await waitFor(() => expect(mock).toHaveBeenCalled()); + await waitFor(() => expect(res.data).toBe(mockMetadataDto)); +}); + +test('Calls Mocked analogue api with mock data', async () => { + const mock = await mockGetData(mockAnalogueList); + + const { result } = renderHook(() => useFetchAnalogues(), { wrapper }); + + const res = await result.current; + + await waitFor(() => expect(mock).toHaveBeenCalled()); + await waitFor(() => expect(res.data).toBe(mockAnalogueList)); +}); + +test('renders ModelMetadata component in loading state', async () => { + await mockGetData(mockMetadataDto); + + const { result } = renderHook(() => useFetchMetadata(), { wrapper }); + await result.current; + + const loading = screen.getByText('Loading ...'); + expect(loading).toBeVisible(); +});