-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Test modelMetadata component. Renders inputs and dropdowns.
- Loading branch information
1 parent
19c0398
commit 4c861f1
Showing
1 changed file
with
181 additions
and
0 deletions.
There are no files selected for viewing
181 changes: 181 additions & 0 deletions
181
src/features/AddModel/ModelMetadata/ModelMetadataComponent.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/* eslint-disable max-lines-per-function */ | ||
import { MsalProvider } from '@azure/msal-react'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { cleanup, render, screen } 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 = {}; | ||
|
||
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(); | ||
}); | ||
|
||
jest.mock('../../../hooks/useFethcMetadata'); | ||
jest.mock('../../../hooks/useFetchAnalogues'); | ||
|
||
const Render = () => { | ||
const testQueryClient = new QueryClient(); | ||
|
||
// @ts-ignore because of error | ||
useFetchMetadata.mockReturnValue({ | ||
data: { | ||
data: [ | ||
{ | ||
metadataId: 'test1', | ||
metadataType: 'Field', | ||
value: 'string11', | ||
}, | ||
{ | ||
metadataId: 'test2', | ||
metadataType: 'Field', | ||
value: 'string12', | ||
}, | ||
{ | ||
metadataId: 'test5', | ||
metadataType: 'Formation', | ||
value: 'string55', | ||
}, | ||
{ | ||
metadataId: 'test6', | ||
metadataType: 'Zone', | ||
value: 'string66', | ||
}, | ||
], | ||
success: true, | ||
}, | ||
isLoading: false, | ||
isSuccess: true, | ||
isError: false, | ||
}); | ||
|
||
// @ts-ignore because of error | ||
useFetchAnalogues.mockReturnValue({ | ||
data: { | ||
data: [ | ||
{ | ||
analogueId: 'test12', | ||
description: 'string122', | ||
name: 'stringA1', | ||
}, | ||
{ | ||
analogueId: 'test13', | ||
description: 'string123', | ||
name: 'stringA2', | ||
}, | ||
], | ||
success: true, | ||
}, | ||
isLoading: false, | ||
isSuccess: true, | ||
isError: false, | ||
}); | ||
|
||
return ( | ||
<MsalProvider instance={msalTester.client}> | ||
<QueryClientProvider client={testQueryClient}> | ||
<ModelMetadata | ||
errors={errors} | ||
metadata={mockMetadata} | ||
setMetadata={jest.fn()} | ||
/> | ||
</QueryClientProvider> | ||
</MsalProvider> | ||
); | ||
}; | ||
|
||
test('renders modelMetadata after loading', () => { | ||
render(<Render />); | ||
|
||
const nameLable = screen.getByLabelText('Model Name'); | ||
expect(nameLable).toBeInTheDocument(); | ||
expect(nameLable).toHaveValue(''); | ||
}); | ||
|
||
test('Name field to be present and empty after render', async () => { | ||
render(<Render />); | ||
|
||
const nameLable = screen.getByLabelText('Model Name'); | ||
expect(nameLable).toBeInTheDocument(); | ||
expect(nameLable).toHaveValue(''); | ||
}); | ||
|
||
test('Description field to be present after render', async () => { | ||
render(<Render />); | ||
|
||
const descriptionLable = screen.getByLabelText('Model description'); | ||
expect(descriptionLable).toBeInTheDocument(); | ||
}); | ||
|
||
test('Field dropdown to be present after render', async () => { | ||
render(<Render />); | ||
|
||
const labeledNodes = await screen.findAllByLabelText('Field'); | ||
const input = labeledNodes[1]; | ||
const optionsList = labeledNodes[0]; | ||
|
||
expect(input).toBeDefined(); | ||
expect(input).toHaveAccessibleName('Field'); | ||
expect(input.nodeName).toBe('INPUT'); | ||
|
||
expect(optionsList).toBeDefined(); | ||
expect(optionsList.nodeName).toBe('UL'); | ||
}); | ||
|
||
test('Zone dropdown to be present after render', async () => { | ||
render(<Render />); | ||
|
||
const labeledNodes = await screen.findAllByLabelText('Zone'); | ||
const input = labeledNodes[1]; | ||
|
||
expect(input).toBeDefined(); | ||
expect(input).toHaveAccessibleName('Zone'); | ||
expect(input.nodeName).toBe('INPUT'); | ||
}); | ||
|
||
test('Formation dropdown to be present after render', async () => { | ||
render(<Render />); | ||
|
||
const labeledNodes = await screen.findAllByLabelText('Formation'); | ||
const input = labeledNodes[1]; | ||
|
||
expect(input).toBeDefined(); | ||
expect(input).toHaveAccessibleName('Formation'); | ||
expect(input.nodeName).toBe('INPUT'); | ||
}); | ||
|
||
test('Analogue dropdown to be present after render', async () => { | ||
render(<Render />); | ||
|
||
const labeledNodes = await screen.findAllByLabelText('Analogue'); | ||
const input = labeledNodes[1]; | ||
|
||
expect(input).toBeDefined(); | ||
expect(input).toHaveAccessibleName('Analogue'); | ||
expect(input.nodeName).toBe('INPUT'); | ||
}); |