-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write tests; fix coded obs so that it actually works
- Loading branch information
Showing
7 changed files
with
398 additions
and
70 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
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
60 changes: 60 additions & 0 deletions
60
...s/esm-patient-registration-app/src/patient-registration/field/__mocks__/field.resource.ts
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,60 @@ | ||
import { ConceptResponse } from '../../patient-registration-types'; | ||
|
||
export const useConcept = jest.fn(function mockUseConceptImplementation(uuid: string): { | ||
data: ConceptResponse; | ||
isLoading: boolean; | ||
} { | ||
let data; | ||
if (uuid == 'weight-uuid') { | ||
data = { | ||
uuid: 'weight-uuid', | ||
display: 'Weight (kg)', | ||
datatype: { display: 'Numeric', uuid: 'num' }, | ||
answers: [], | ||
setMembers: [], | ||
}; | ||
} else if (uuid == 'chief-complaint-uuid') { | ||
data = { | ||
uuid: 'chief-complaint-uuid', | ||
display: 'Chief Complaint', | ||
datatype: { display: 'Text', uuid: 'txt' }, | ||
answers: [], | ||
setMembers: [], | ||
}; | ||
} else if (uuid == 'nationality-uuid') { | ||
data = { | ||
uuid: 'nationality-uuid', | ||
display: 'Nationality', | ||
datatype: { display: 'Coded', uuid: 'cdd' }, | ||
answers: [ | ||
{ display: 'USA', uuid: 'usa' }, | ||
{ display: 'Mexico', uuid: 'mex' }, | ||
], | ||
setMembers: [], | ||
}; | ||
} | ||
return { | ||
data: data ?? null, | ||
isLoading: !data, | ||
}; | ||
}); | ||
|
||
export const useConceptAnswers = jest.fn((uuid: string) => { | ||
if (uuid == 'nationality-uuid') { | ||
return { | ||
data: [ | ||
{ display: 'USA', uuid: 'usa' }, | ||
{ display: 'Mexico', uuid: 'mex' }, | ||
], | ||
isLoading: false, | ||
}; | ||
} else if (uuid == 'other-countries-uuid') { | ||
return { | ||
data: [ | ||
{ display: 'Kenya', uuid: 'ke' }, | ||
{ display: 'Uganda', uuid: 'ug' }, | ||
], | ||
isLoading: false, | ||
}; | ||
} | ||
}); |
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
103 changes: 103 additions & 0 deletions
103
packages/esm-patient-registration-app/src/patient-registration/field/obs/obs-field.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,103 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { FieldDefinition, RegistrationConfig } from '../../../config-schema'; | ||
import { ObsField } from './obs-field.component'; | ||
import { useConfig } from '@openmrs/esm-framework'; | ||
import { useConcept, useConceptAnswers } from '../field.resource'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
const mockUseConfig = useConfig as jest.Mock; | ||
|
||
// The UUIDs in this test all refer to ones that are in `__mocks__/field.resource.ts` | ||
jest.mock('../field.resource'); | ||
|
||
type FieldProps = { | ||
children: ({ field, form: { touched, errors } }) => React.ReactNode; | ||
}; | ||
jest.mock('formik', () => ({ | ||
...(jest.requireActual('formik') as object), | ||
Field: jest.fn(({ children }: FieldProps) => <>{children({ field: {}, form: { touched: {}, errors: {} } })}</>), | ||
useField: jest.fn(() => [{ value: null }, {}]), | ||
})); | ||
|
||
const textFieldDef: FieldDefinition = { | ||
id: 'chief-complaint', | ||
type: 'obs', | ||
label: '', | ||
placeholder: '', | ||
uuid: 'chief-complaint-uuid', | ||
validation: { | ||
required: false, | ||
matches: null, | ||
}, | ||
answerConceptSetUuid: null, | ||
}; | ||
|
||
const numberFieldDef: FieldDefinition = { | ||
id: 'weight', | ||
type: 'obs', | ||
label: '', | ||
placeholder: '', | ||
uuid: 'weight-uuid', | ||
validation: { | ||
required: false, | ||
matches: null, | ||
}, | ||
answerConceptSetUuid: null, | ||
}; | ||
|
||
const codedFieldDef: FieldDefinition = { | ||
id: 'nationality', | ||
type: 'obs', | ||
label: '', | ||
placeholder: '', | ||
uuid: 'nationality-uuid', | ||
validation: { | ||
required: false, | ||
matches: null, | ||
}, | ||
answerConceptSetUuid: null, | ||
}; | ||
|
||
describe('ObsField', () => { | ||
beforeEach(() => { | ||
mockUseConfig.mockReturnValue({ registrationObs: { encounterTypeUuid: 'reg-enc-uuid' } }); | ||
}); | ||
|
||
it("logs an error and doesn't render if no registration encounter type is provided", () => { | ||
mockUseConfig.mockReturnValue({ registrationObs: { encounterTypeUuid: null } }); | ||
console.error = jest.fn(); | ||
render(<ObsField fieldDefinition={textFieldDef} />); | ||
expect(console.error).toHaveBeenCalledWith(expect.stringMatching(/no registration encounter type.*configure/i)); | ||
expect(screen.queryByRole('textbox')).toBeNull(); | ||
}); | ||
|
||
it('renders a text box for text concept', () => { | ||
render(<ObsField fieldDefinition={textFieldDef} />); | ||
// I don't know why the labels aren't in the DOM, but they aren't | ||
// expect(screen.getByLabelText("Chief Complaint")).toBeInTheDocument(); | ||
expect(screen.getByRole('textbox')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders a number box for number concept', () => { | ||
render(<ObsField fieldDefinition={numberFieldDef} />); | ||
// expect(screen.getByLabelText("Weight (kg)")).toBeInTheDocument(); | ||
expect(screen.getByRole('spinbutton')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders a select for a coded concept', () => { | ||
render(<ObsField fieldDefinition={codedFieldDef} />); | ||
// expect(screen.getByLabelText("Nationality")).toBeInTheDocument(); | ||
const select = screen.getByRole('combobox'); | ||
expect(select).toBeInTheDocument(); | ||
expect(select).toHaveDisplayValue(''); | ||
}); | ||
|
||
it('select uses answerConcept for answers when it is provided', () => { | ||
render(<ObsField fieldDefinition={{ ...codedFieldDef, answerConceptSetUuid: 'other-countries-uuid' }} />); | ||
// expect(screen.getByLabelText("Nationality")).toBeInTheDocument(); | ||
const select = screen.getByRole('combobox'); | ||
expect(select).toBeInTheDocument(); | ||
userEvent.selectOptions(select, 'Kenya'); | ||
}); | ||
}); |
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
Oops, something went wrong.