-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of sdc form tests based on allergies questionnaire
- Loading branch information
Showing
8 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
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,34 @@ | ||
import { screen, fireEvent, within } from '@testing-library/react'; | ||
|
||
export async function chooseSelectOption(questionId: string, optionLabel: string) { | ||
const questionElement = await screen.findByTestId(questionId); | ||
fireEvent.focus(questionElement.querySelector('input')!); | ||
fireEvent.keyDown(questionElement.querySelector('input')!, { key: 'ArrowDown', code: 40 }); | ||
|
||
const optionLabelMatcher = (_content: string, element: Element | null) => element?.textContent === optionLabel; | ||
const option = await screen.findByText(optionLabelMatcher, { | ||
selector: '.react-select__option', | ||
}); | ||
|
||
fireEvent.click(option); | ||
await screen.findByText(optionLabelMatcher, { | ||
selector: '.react-select__single-value,.react-select__multi-value', | ||
}); | ||
} | ||
|
||
export async function chooseInlineOption(questionId: string, optionLabel: string) { | ||
const questionElement = await screen.findByTestId(questionId); | ||
const optionLabelMatcher = (_content: string, element: Element | null) => element?.textContent === optionLabel; | ||
const option = await within(questionElement).findByText(optionLabelMatcher, { | ||
selector: 'label', | ||
}); | ||
|
||
fireEvent.click(option); | ||
} | ||
|
||
export async function inputText(questionId: string, value: string) { | ||
const questionElement = await screen.findByTestId(questionId); | ||
const input = (questionElement.querySelector('input') ?? questionElement.querySelector('textarea'))!; | ||
|
||
fireEvent.change(input, { target: { value: value } }); | ||
} |
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
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
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
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
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
67 changes: 67 additions & 0 deletions
67
src/containers/PatientDetails/PatientDocument/__tests__/allergies-form.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,67 @@ | ||
import { i18n } from '@lingui/core'; | ||
import { I18nProvider } from '@lingui/react'; | ||
import { screen, render, waitFor, fireEvent, act } from '@testing-library/react'; | ||
import { expect, test, vi } from 'vitest'; | ||
|
||
import { extractBundleResources, getFHIRResources } from 'aidbox-react/lib/services/fhir'; | ||
|
||
import { AllergyIntolerance } from '@beda.software/aidbox-types'; | ||
import { ensure, withRootAccess } from '@beda.software/fhir-react'; | ||
|
||
import { chooseInlineOption, inputText } from 'src/__tests__/sdc-helpers'; | ||
import { PatientDocument } from 'src/containers/PatientDetails/PatientDocument'; | ||
import { axiosInstance } from 'src/services/fhir'; | ||
import { createPatient, loginAdminUser } from 'src/setupTests'; | ||
|
||
test('Create new Clinical Trial Test', async () => { | ||
await loginAdminUser(); | ||
const { patient } = await withRootAccess(axiosInstance, async () => { | ||
const patient = await createPatient({ | ||
name: [{ given: ['John'], family: 'Smith' }], | ||
}); | ||
|
||
return { patient }; | ||
}); | ||
|
||
const onSuccess = vi.fn(); | ||
act(() => { | ||
i18n.activate('en'); | ||
}); | ||
|
||
render( | ||
<I18nProvider i18n={i18n}> | ||
<PatientDocument patient={patient} author={patient} questionnaireId="allergies" onSuccess={onSuccess} /> | ||
</I18nProvider>, | ||
); | ||
|
||
await chooseInlineOption('type', 'Food'); | ||
await chooseInlineOption('reaction', 'Headache'); | ||
await chooseInlineOption('substance-food', 'Eggs'); | ||
await inputText('notes', 'Notes'); | ||
|
||
fireEvent.click(screen.getByTestId('submit-button')); | ||
|
||
await waitFor(() => expect(onSuccess).toHaveBeenCalled()); | ||
|
||
await withRootAccess(axiosInstance, async () => { | ||
const allergyIntoleranceList = await waitFor(async () => { | ||
const allergyIntoleranceResponse = await getFHIRResources<AllergyIntolerance>('AllergyIntolerance', { | ||
patient: `Patient/${patient.id}`, | ||
}); | ||
return extractBundleResources(ensure(allergyIntoleranceResponse)).AllergyIntolerance; | ||
}); | ||
|
||
expect(allergyIntoleranceList.length).toBe(1); | ||
|
||
// TODO: Looks like a bug in the extract, category is undefined | ||
// expect(allergyIntoleranceList[0]?.category?.[0]).toBe('food'); | ||
expect(allergyIntoleranceList[0]?.code?.coding?.[0]).toMatchObject({ | ||
code: '102263004', | ||
display: 'Eggs', | ||
}); | ||
expect(allergyIntoleranceList[0]?.reaction?.[0]?.manifestation?.[0]?.coding?.[0]).toMatchObject({ | ||
code: '25064002', | ||
display: 'Headache', | ||
}); | ||
}); | ||
}, 60000); |
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