This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(diagnosis): adds new diagnosis fields (#2276)
- Loading branch information
Showing
9 changed files
with
550 additions
and
188 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
214 changes: 89 additions & 125 deletions
214
src/__tests__/patients/diagnoses/AddDiagnosisModal.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 |
---|---|---|
@@ -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) | ||
}) | ||
}) |
Oops, something went wrong.
2532435
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: