Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(diagnosis): adds new diagnosis fields (#2276)
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmeyer committed Aug 13, 2020
1 parent f3d768d commit 2532435
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 188 deletions.
6 changes: 5 additions & 1 deletion src/__tests__/patients/care-plans/CarePlanForm.test.tsx
Expand Up @@ -6,7 +6,7 @@ import { act } from 'react-dom/test-utils'

import CarePlanForm from '../../../patients/care-plans/CarePlanForm'
import CarePlan, { CarePlanIntent, CarePlanStatus } from '../../../shared/model/CarePlan'
import Diagnosis from '../../../shared/model/Diagnosis'
import Diagnosis, { DiagnosisStatus } from '../../../shared/model/Diagnosis'
import Patient from '../../../shared/model/Patient'

describe('Care Plan Form', () => {
Expand All @@ -15,6 +15,10 @@ describe('Care Plan Form', () => {
id: '123',
name: 'some diagnosis name',
diagnosisDate: new Date().toISOString(),
onsetDate: new Date().toISOString(),
abatementDate: new Date().toISOString(),
status: DiagnosisStatus.Active,
note: 'some note',
}
const carePlan: CarePlan = {
id: 'id',
Expand Down
214 changes: 89 additions & 125 deletions src/__tests__/patients/diagnoses/AddDiagnosisModal.test.tsx
@@ -1,161 +1,125 @@
import { Modal, Alert } from '@hospitalrun/components'
import { act } from '@testing-library/react'
import { Modal } from '@hospitalrun/components'
import { mount } from 'enzyme'
import { createMemoryHistory } from 'history'
import React from 'react'
import { act } from 'react-dom/test-utils'
import { Provider } from 'react-redux'
import { Router } from 'react-router-dom'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import AddDiagnosisModal from '../../../patients/diagnoses/AddDiagnosisModal'
import DiagnosisForm from '../../../patients/diagnoses/DiagnosisForm'
import * as patientSlice from '../../../patients/patient-slice'
import DatePickerWithLabelFormGroup from '../../../shared/components/input/DatePickerWithLabelFormGroup'
import TextInputWithLabelFormGroup from '../../../shared/components/input/TextInputWithLabelFormGroup'
import PatientRepository from '../../../shared/db/PatientRepository'
import Diagnosis from '../../../shared/model/Diagnosis'
import { CarePlanIntent, CarePlanStatus } from '../../../shared/model/CarePlan'
import Patient from '../../../shared/model/Patient'
import { RootState } from '../../../shared/store'

const mockStore = createMockStore<RootState, any>([thunk])

describe('Add Diagnosis Modal', () => {
beforeEach(() => {
jest.spyOn(PatientRepository, 'find')
jest.spyOn(PatientRepository, 'saveOrUpdate')
})

it('should render a modal with the correct labels', () => {
const store = mockStore({
patient: {
patient: {
id: '1234',
},
const patient = {
id: 'patientId',
diagnoses: [{ id: '123', name: 'some name', diagnosisDate: new Date().toISOString() }],
carePlans: [
{
id: '123',
title: 'some title',
description: 'some description',
diagnosisId: '123',
startDate: new Date().toISOString(),
endDate: new Date().toISOString(),
status: CarePlanStatus.Active,
intent: CarePlanIntent.Proposal,
},
} as any)
],
} as Patient

const diagnosisError = {
title: 'some diagnosisError error',
}

const onCloseSpy = jest.fn()
const setup = () => {
jest.spyOn(PatientRepository, 'find').mockResolvedValue(patient)
jest.spyOn(PatientRepository, 'saveOrUpdate')
const store = mockStore({ patient: { patient, diagnosisError } } as any)
const history = createMemoryHistory()
const wrapper = mount(
<Provider store={store}>
<AddDiagnosisModal show onCloseButtonClick={jest.fn()} />
<Router history={history}>
<AddDiagnosisModal show onCloseButtonClick={onCloseSpy} />
</Router>
</Provider>,
)

wrapper.update()
return { wrapper }
}

it('should render a modal', () => {
const { wrapper } = setup()

const modal = wrapper.find(Modal)

expect(modal).toHaveLength(1)

const successButton = modal.prop('successButton')
const cancelButton = modal.prop('closeButton')
expect(modal.prop('title')).toEqual('patient.diagnoses.new')
expect(modal.prop('closeButton')?.children).toEqual('actions.cancel')
expect(modal.prop('closeButton')?.color).toEqual('danger')
expect(modal.prop('successButton')?.children).toEqual('patient.diagnoses.new')
expect(modal.prop('successButton')?.color).toEqual('success')
expect(modal.prop('successButton')?.icon).toEqual('add')
expect(successButton?.children).toEqual('patient.diagnoses.new')
expect(successButton?.icon).toEqual('add')
expect(cancelButton?.children).toEqual('actions.cancel')
})

it('should display an errors', () => {
const expectedDiagnosisError = {
message: 'some message',
date: 'some date message',
name: 'some date message',
}
const store = mockStore({
patient: {
diagnosisError: expectedDiagnosisError,
},
} as any)
const wrapper = mount(
<Provider store={store}>
<AddDiagnosisModal show onCloseButtonClick={jest.fn()} />
</Provider>,
)
wrapper.update()

expect(wrapper.find(Alert)).toHaveLength(1)
it('should render the diagnosis form', () => {
const { wrapper } = setup()

expect(wrapper.find(Alert).prop('title')).toEqual('states.error')
expect(wrapper.find(Alert).prop('message')).toContain(expectedDiagnosisError.message)
expect(wrapper.find(TextInputWithLabelFormGroup).prop('feedback')).toContain(
expectedDiagnosisError.name,
)
expect(wrapper.find(TextInputWithLabelFormGroup).prop('isInvalid')).toBeTruthy()
expect(wrapper.find(DatePickerWithLabelFormGroup).prop('feedback')).toContain(
expectedDiagnosisError.date,
)
expect(wrapper.find(DatePickerWithLabelFormGroup).prop('isInvalid')).toBeTruthy()
const diagnosisForm = wrapper.find(DiagnosisForm)
expect(diagnosisForm).toHaveLength(1)
expect(diagnosisForm.prop('diagnosisError')).toEqual(diagnosisError)
})

describe('cancel', () => {
it('should call the onCloseButtonClick function when the close button is clicked', () => {
const onCloseButtonClickSpy = jest.fn()
const store = mockStore({
patient: {
patient: {
id: '1234',
},
},
} as any)
const wrapper = mount(
<Provider store={store}>
<AddDiagnosisModal show onCloseButtonClick={onCloseButtonClickSpy} />
</Provider>,
)
wrapper.update()

act(() => {
const modal = wrapper.find(Modal)
const { onClick } = modal.prop('closeButton') as any
onClick()
})

expect(onCloseButtonClickSpy).toHaveBeenCalledTimes(1)
it('should dispatch add diagnosis when the save button is clicked', async () => {
const { wrapper } = setup()
jest.spyOn(patientSlice, 'addDiagnosis')

act(() => {
const diagnosisForm = wrapper.find(DiagnosisForm)
const onChange = diagnosisForm.prop('onChange') as any
if (patient.diagnoses != null) {
onChange(patient.diagnoses[0])
}
})
wrapper.update()

await act(async () => {
const modal = wrapper.find(Modal)
const successButton = modal.prop('successButton')
const onClick = successButton?.onClick as any
await onClick()
})

expect(patientSlice.addDiagnosis).toHaveBeenCalledTimes(1)
if (patient.diagnoses != null) {
expect(patientSlice.addDiagnosis).toHaveBeenCalledWith(patient.id, patient.diagnoses[0])
}
})

describe('save', () => {
it('should dispatch add diagnosis', () => {
const expectedName = 'expected name'
const expectedDate = new Date()
jest.spyOn(patientSlice, 'addDiagnosis')
const patient = {
id: '1234',
givenName: 'some name',
}
it('should call the on close function when the cancel button is clicked', () => {
const { wrapper } = setup()

const modal = wrapper.find(Modal)

expect(modal).toHaveLength(1)

jest.spyOn(PatientRepository, 'find').mockResolvedValue(patient as Patient)
jest.spyOn(PatientRepository, 'saveOrUpdate').mockResolvedValue(patient as Patient)

const diagnosis = {
name: expectedName,
diagnosisDate: expectedDate.toISOString(),
} as Diagnosis

const store = mockStore({
patient: {
patient,
},
} as any)
const wrapper = mount(
<Provider store={store}>
<AddDiagnosisModal show onCloseButtonClick={jest.fn()} />
</Provider>,
)

act(() => {
const input = wrapper.findWhere((c: any) => c.prop('name') === 'name')
const onChange = input.prop('onChange')
onChange({ target: { value: expectedName } })
})
wrapper.update()

act(() => {
const input = wrapper.findWhere((c: any) => c.prop('name') === 'diagnosisDate')
const onChange = input.prop('onChange')
onChange(expectedDate)
})
wrapper.update()

act(() => {
const modal = wrapper.find(Modal)
const { onClick } = modal.prop('successButton') as any
onClick()
})

expect(patientSlice.addDiagnosis).toHaveBeenCalledWith(patient.id, { ...diagnosis })
act(() => {
const cancelButton = modal.prop('closeButton')
const onClick = cancelButton?.onClick as any
onClick()
})

expect(onCloseSpy).toHaveBeenCalledTimes(1)
})
})

1 comment on commit 2532435

@vercel
Copy link

@vercel vercel bot commented on 2532435 Aug 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.