Skip to content

Commit

Permalink
test(newlabrequest): added additional tests to the visit dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewGregory committed Dec 13, 2020
1 parent d38322f commit bdb9ef2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
55 changes: 53 additions & 2 deletions src/__tests__/labs/requests/NewLabRequest.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Button, Typeahead, Label, Alert } from '@hospitalrun/components'
import format from 'date-fns/format'
import { mount, ReactWrapper } from 'enzyme'
import { createMemoryHistory } from 'history'
import React from 'react'
Expand All @@ -19,6 +20,7 @@ import LabRepository from '../../../shared/db/LabRepository'
import PatientRepository from '../../../shared/db/PatientRepository'
import Lab from '../../../shared/model/Lab'
import Patient from '../../../shared/model/Patient'
import Visit from '../../../shared/model/Visit'
import { RootState } from '../../../shared/store'

const mockStore = createMockStore<RootState, any>([thunk])
Expand Down Expand Up @@ -97,7 +99,8 @@ describe('New Lab Request', () => {

expect(visitSelect).toBeDefined()
expect(visitSelect.prop('label') as string).toEqual('patient.visit')
expect(visitSelect.prop('isRequired')).toBeTruthy()
expect(visitSelect.prop('isRequired')).toBeFalsy()
expect(visitSelect.prop('defaultSelected')).toEqual([])
})

it('should render a save button', async () => {
Expand All @@ -113,6 +116,54 @@ describe('New Lab Request', () => {
expect(cancelButton).toBeDefined()
expect(cancelButton.text().trim()).toEqual('actions.cancel')
})

it('should clear visit when patient is changed', async () => {
const { wrapper } = await setup()
const patientTypeahead = wrapper.find(Typeahead)
const expectedDate = new Date()
const expectedNotes = 'expected notes'
const expectedLab = {
patient: '1234567',
type: 'expected type',
status: 'requested',
notes: [expectedNotes],
id: '1234',
requestedOn: expectedDate.toISOString(),
} as Lab

const visits = [
{
startDateTime: new Date().toISOString(),
id: 'visit_id',
type: 'visit_type',
},
] as Visit[]

await act(async () => {
const onChange = patientTypeahead.prop('onChange') as any
await onChange([{ id: expectedLab.patient, visits }] as Patient[])
})
wrapper.update()

// The visits dropdown should be populated with the patient's visits.
expect(wrapper.find(SelectWithLabelFormGroup).prop('options')).toEqual([
{
label: `${visits[0].type} at ${format(
new Date(visits[0].startDateTime),
'yyyy-MM-dd hh:mm a',
)}`,
value: 'visit_id',
},
])
await act(async () => {
const onChange = patientTypeahead.prop('onChange')
await onChange([] as Patient[])
})

wrapper.update()
// The visits dropdown option should be reset when the patient is changed.
expect(wrapper.find(SelectWithLabelFormGroup).prop('options')).toEqual([])
})
})

describe('errors', () => {
Expand Down Expand Up @@ -197,7 +248,7 @@ describe('New Lab Request', () => {
const patientTypeahead = wrapper.find(Typeahead)
await act(async () => {
const onChange = patientTypeahead.prop('onChange')
await onChange([{ id: expectedLab.patient }] as Patient[])
await onChange([{ id: expectedLab.patient, visits: [] as Visit[] }] as Patient[])
})

const typeInput = wrapper.find(TextInputWithLabelFormGroup)
Expand Down
36 changes: 22 additions & 14 deletions src/labs/requests/NewLabRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,26 @@ const NewLabRequest = () => {
useAddBreadcrumbs(breadcrumbs)

const onPatientChange = (patient: Patient) => {
const visits = patient.visits?.map((v) => ({
label: `${v.type} at ${format(new Date(v.startDateTime), 'yyyy-MM-dd hh:mm a')}`,
value: v.id,
})) as Option[]
setVisitOptions(visits)
if (patient) {
const visits = patient.visits.map((v) => ({
label: `${v.type} at ${format(new Date(v.startDateTime), 'yyyy-MM-dd hh:mm a')}`,
value: v.id,
})) as Option[]

setNewLabRequest((previousNewLabRequest) => ({
...previousNewLabRequest,
patient: patient.id,
fullName: patient.fullName,
}))
setVisitOptions(visits)
setNewLabRequest((previousNewLabRequest) => ({
...previousNewLabRequest,
patient: patient.id,
fullName: patient.fullName,
}))
} else {
setVisitOptions([])
setNewLabRequest((previousNewLabRequest) => ({
...previousNewLabRequest,
patient: '',
visitId: '',
}))
}
}

const onLabTypeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -97,10 +106,10 @@ const NewLabRequest = () => {
}
}

const onVisitChange = (value: string) => {
const onVisitChange = (visitId: string) => {
setNewLabRequest((previousNewLabRequest) => ({
...previousNewLabRequest,
visitId: value,
visitId,
}))
}

Expand Down Expand Up @@ -140,9 +149,8 @@ const NewLabRequest = () => {
<SelectWithLabelFormGroup
name="visit"
label={t('patient.visit')}
isRequired
isEditable={newLabRequest.patient !== undefined}
options={visitOptions || []}
options={visitOptions}
defaultSelected={defaultSelectedVisitsOption()}
onChange={(values) => {
onVisitChange(values[0])
Expand Down

0 comments on commit bdb9ef2

Please sign in to comment.