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

Commit

Permalink
feat(patients): fixes notifactions for updating/creating patient
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Mar 5, 2020
1 parent 886068a commit 73677e2
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 76 deletions.
28 changes: 28 additions & 0 deletions src/__tests__/patients/allergies/Allergies.test.tsx
Expand Up @@ -10,6 +10,7 @@ import { Router } from 'react-router'
import { Provider } from 'react-redux'
import Patient from 'model/Patient'
import { Button, Modal, List, ListItem, Alert } from '@hospitalrun/components'
import * as components from '@hospitalrun/components'
import { act } from '@testing-library/react'
import { mocked } from 'ts-jest/utils'
import PatientRepository from 'clients/db/PatientRepository'
Expand Down Expand Up @@ -104,6 +105,33 @@ describe('Allergies', () => {
patientSlice.updatePatientSuccess(expectedUpdatedPatient),
)
})

it('should display a success message after the allergy is successfully added', async () => {
jest.spyOn(components, 'Toast')
const mockedComponents = mocked(components, true)

const expectedAllergy = { name: 'name' } as Allergy
const expectedUpdatedPatient = {
...expectedPatient,
allergies: [...(expectedPatient.allergies as any), expectedAllergy],
} as Patient

const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedUpdatedPatient)

const wrapper = setup()

await act(async () => {
const modal = wrapper.find(NewAllergyModal)
await modal.prop('onSave')(expectedAllergy)
})

expect(mockedComponents.Toast).toHaveBeenCalledWith(
'success',
'Success!',
'patient.allergies.successfullyAdded',
)
})
})

describe('allergy list', () => {
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/patients/diagnoses/Diagnoses.test.tsx
Expand Up @@ -11,6 +11,7 @@ import { Router } from 'react-router'
import { Provider } from 'react-redux'
import Diagnoses from 'patients/diagnoses/Diagnoses'
import { Button, Modal, List, ListItem, Alert } from '@hospitalrun/components'
import * as components from '@hospitalrun/components'
import { act } from 'react-dom/test-utils'
import { mocked } from 'ts-jest/utils'
import PatientRepository from 'clients/db/PatientRepository'
Expand Down Expand Up @@ -102,6 +103,36 @@ describe('Diagnoses', () => {
patientSlice.updatePatientSuccess(expectedUpdatedPatient),
)
})

it('should display a success message when successfully added', async () => {
jest.spyOn(components, 'Toast')
const mockedComponents = mocked(components, true)

const expectedDiagnosis = {
name: 'name',
diagnosisDate: new Date().toISOString(),
} as Diagnosis
const expectedUpdatedPatient = {
...expectedPatient,
diagnoses: [...(expectedPatient.diagnoses as any), expectedDiagnosis],
} as Patient

const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedUpdatedPatient)

const wrapper = setup()

await act(async () => {
const modal = wrapper.find(AddDiagnosisModal)
await modal.prop('onSave')(expectedDiagnosis)
})

expect(mockedComponents.Toast).toHaveBeenCalledWith(
'success',
'Success!',
'patient.diagnoses.successfullyAdded',
)
})
})

describe('diagnoses list', () => {
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/patients/new/NewPatient.test.tsx
Expand Up @@ -9,6 +9,7 @@ import { act } from 'react-dom/test-utils'
import { Button } from '@hospitalrun/components'
import configureMockStore, { MockStore } from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as components from '@hospitalrun/components'

import NewPatient from '../../../patients/new/NewPatient'
import GeneralInformation from '../../../patients/GeneralInformation'
Expand Down Expand Up @@ -126,6 +127,38 @@ describe('New Patient', () => {
expect(store.getActions()).toContainEqual(patientSlice.createPatientSuccess())
})

it('should navigate to /patients/:id and display a message after a new patient is successfully created', async () => {
jest.spyOn(components, 'Toast')
const mockedComponents = mocked(components, true)
let wrapper: any
await act(async () => {
wrapper = await setup()
})

const generalInformationForm = wrapper.find(GeneralInformation)

act(() => {
generalInformationForm.prop('onFieldChange')('givenName', 'first')
})

wrapper.update()

const saveButton = wrapper.find(Button).at(0)
const onClick = saveButton.prop('onClick') as any
expect(saveButton.text().trim()).toEqual('actions.save')

await act(async () => {
await onClick()
})

expect(history.location.pathname).toEqual(`/patients/${patient.id}`)
expect(mockedComponents.Toast).toHaveBeenCalledWith(
'success',
'Success!',
`patients.successfullyCreated ${patient.fullName}`,
)
})

it('should navigate to /patients when cancel is clicked', async () => {
let wrapper: any
await act(async () => {
Expand Down
58 changes: 16 additions & 42 deletions src/__tests__/patients/patient-slice.test.ts
@@ -1,7 +1,6 @@
import '../../__mocks__/matchMediaMock'
import { AnyAction } from 'redux'
import { mocked } from 'ts-jest/utils'
import { createMemoryHistory } from 'history'
import * as components from '@hospitalrun/components'

import patient, {
Expand Down Expand Up @@ -113,7 +112,7 @@ describe('patients slice', () => {
id: 'sliceId1',
} as Patient

await createPatient(expectedPatient, createMemoryHistory())(dispatch, getState, null)
await createPatient(expectedPatient)(dispatch, getState, null)

expect(dispatch).toHaveBeenCalledWith({ type: createPatientStart.type })
})
Expand All @@ -127,7 +126,7 @@ describe('patients slice', () => {
id: 'sliceId2',
} as Patient

await createPatient(expectedPatient, createMemoryHistory())(dispatch, getState, null)
await createPatient(expectedPatient)(dispatch, getState, null)

expect(PatientRepository.save).toHaveBeenCalledWith(expectedPatient)
})
Expand All @@ -141,29 +140,13 @@ describe('patients slice', () => {
id: 'slideId3',
} as Patient

await createPatient(expectedPatient, createMemoryHistory())(dispatch, getState, null)
await createPatient(expectedPatient)(dispatch, getState, null)

expect(dispatch).toHaveBeenCalledWith({ type: createPatientSuccess.type })
})

it('should navigate to the /patients/:id where id is the new patient id', async () => {
const expectedPatientId = 'sliceId4'
jest.spyOn(PatientRepository, 'save')
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.save.mockResolvedValue({ id: expectedPatientId } as Patient)
const history = createMemoryHistory()

const dispatch = jest.fn()
const getState = jest.fn()
const expectedPatient = {} as Patient

await createPatient(expectedPatient, history)(dispatch, getState, null)

expect(history.entries[1].pathname).toEqual(`/patients/${expectedPatientId}`)
})

it('should call the Toaster function with the correct data', async () => {
jest.spyOn(components, 'Toast')
it('should call the on success function', async () => {
const onSuccessSpy = jest.fn()
const expectedPatientId = 'sliceId5'
const expectedFullName = 'John Doe II'
const expectedPatient = {
Expand All @@ -173,18 +156,13 @@ describe('patients slice', () => {
jest.spyOn(PatientRepository, 'save')
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.save.mockResolvedValue(expectedPatient)
const mockedComponents = mocked(components, true)
const history = createMemoryHistory()
const dispatch = jest.fn()
const getState = jest.fn()

await createPatient(expectedPatient, history)(dispatch, getState, null)
await createPatient(expectedPatient, onSuccessSpy)(dispatch, getState, null)

expect(mockedComponents.Toast).toHaveBeenCalledWith(
'success',
'Success!',
`patients.successfullyCreated ${expectedFullName}`,
)
expect(onSuccessSpy).toHaveBeenCalled()
expect(onSuccessSpy).toHaveBeenCalledWith(expectedPatient)
})
})

Expand Down Expand Up @@ -248,7 +226,7 @@ describe('patients slice', () => {
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedPatient)

await updatePatient(expectedPatient, createMemoryHistory())(dispatch, getState, null)
await updatePatient(expectedPatient)(dispatch, getState, null)

expect(dispatch).toHaveBeenCalledWith({ type: updatePatientStart.type })
})
Expand All @@ -262,7 +240,7 @@ describe('patients slice', () => {
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedPatient)

await updatePatient(expectedPatient, createMemoryHistory())(dispatch, getState, null)
await updatePatient(expectedPatient)(dispatch, getState, null)

expect(PatientRepository.saveOrUpdate).toHaveBeenCalledWith(expectedPatient)
})
Expand All @@ -276,15 +254,16 @@ describe('patients slice', () => {
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedPatient)

await updatePatient(expectedPatient, createMemoryHistory())(dispatch, getState, null)
await updatePatient(expectedPatient)(dispatch, getState, null)

expect(dispatch).toHaveBeenCalledWith({
type: updatePatientSuccess.type,
payload: expectedPatient,
})
})

it('should call the Toaster function with the correct data', async () => {
it('should call the onSuccess function', async () => {
const onSuccessSpy = jest.fn()
jest.spyOn(components, 'Toast')
const expectedPatientId = 'sliceId11'
const fullName = 'John Doe II'
Expand All @@ -294,18 +273,13 @@ describe('patients slice', () => {
} as Patient
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedPatient)
const mockedComponents = mocked(components, true)
const history = createMemoryHistory()
const dispatch = jest.fn()
const getState = jest.fn()

await updatePatient(expectedPatient, history)(dispatch, getState, null)
await updatePatient(expectedPatient, onSuccessSpy)(dispatch, getState, null)

expect(mockedComponents.Toast).toHaveBeenCalledWith(
'success',
'Success!',
`patients.successfullyUpdated ${fullName}`,
)
expect(onSuccessSpy).toHaveBeenCalled()
expect(onSuccessSpy).toHaveBeenCalledWith(expectedPatient)
})
})
})
21 changes: 21 additions & 0 deletions src/__tests__/patients/related-persons/RelatedPersons.test.tsx
Expand Up @@ -5,6 +5,8 @@ import { createMemoryHistory } from 'history'
import { mount } from 'enzyme'
import RelatedPersonTab from 'patients/related-persons/RelatedPersonTab'
import { Button, List, ListItem, Alert } from '@hospitalrun/components'
import * as components from '@hospitalrun/components'

import NewRelatedPersonModal from 'patients/related-persons/NewRelatedPersonModal'
import { act } from '@testing-library/react'
import PatientRepository from 'clients/db/PatientRepository'
Expand All @@ -26,8 +28,12 @@ describe('Related Persons Tab', () => {
describe('Add New Related Person', () => {
let patient: any
let user: any
jest.spyOn(components, 'Toast')
let mockedComponents = mocked(components, true)

beforeEach(() => {
jest.resetAllMocks()
mockedComponents = mocked(components, true)
history = createMemoryHistory()

patient = {
Expand Down Expand Up @@ -150,6 +156,21 @@ describe('Related Persons Tab', () => {
const newRelatedPersonModal = wrapper.find(NewRelatedPersonModal)
expect(newRelatedPersonModal.prop('show')).toBeFalsy()
})

it('should display a success message when the new related person is added', async () => {
await act(async () => {
const newRelatedPersonModal = wrapper.find(NewRelatedPersonModal)
const onSave = newRelatedPersonModal.prop('onSave') as any
await onSave({ patientId: 'testMessage', type: 'type' })
})
wrapper.update()

expect(mockedComponents.Toast).toHaveBeenCalledWith(
'success',
'Success!',
'patient.relatedPersons.successfullyAdded',
)
})
})

describe('List', () => {
Expand Down
10 changes: 6 additions & 4 deletions src/locales/en-US/translation.json
Expand Up @@ -44,7 +44,8 @@
"label": "Related Persons",
"new": "New Related Person",
"relationshipType": "Relationship Type",
"addRelatedPersonAbove": "Add a related person using the button above."
"addRelatedPersonAbove": "Add a related person using the button above.",
"successfullyAdded": "Successfully added a new related person!"
},
"types": {
"charity": "Charity",
Expand All @@ -62,7 +63,8 @@
"warning": {
"noAllergies": "No Allergies"
},
"addAllergyAbove": "Add an allergy using the button above."
"addAllergyAbove": "Add an allergy using the button above.",
"successfullyAdded": "Successfully added a new allergy!"
},
"diagnoses": {
"label": "Diagnoses",
Expand All @@ -76,8 +78,8 @@
"nameRequired": "Diagnosis Name is required.",
"dateRequired": "Diagnosis Date is required."
},
"addDiagnosisAbove": "Add a diagnosis using the button above."

"addDiagnosisAbove": "Add a diagnosis using the button above.",
"successfullyAdded": "Successfully added a new diagnosis!"
}
},
"sex": {
Expand Down
10 changes: 6 additions & 4 deletions src/patients/allergies/Allergies.tsx
@@ -1,13 +1,12 @@
import React, { useState } from 'react'
import useAddBreadcrumbs from 'breadcrumbs/useAddBreadcrumbs'
import Patient from 'model/Patient'
import { Button, List, ListItem, Alert } from '@hospitalrun/components'
import { Button, List, ListItem, Alert, Toast } from '@hospitalrun/components'
import { useSelector, useDispatch } from 'react-redux'
import { RootState } from 'store'
import Permissions from 'model/Permissions'
import { useTranslation } from 'react-i18next'
import Allergy from 'model/Allergy'
import { useHistory } from 'react-router'
import { updatePatient } from 'patients/patient-slice'
import { getTimestampId } from 'patients/util/timestamp-id-generator'
import NewAllergyModal from './NewAllergyModal'
Expand All @@ -18,7 +17,6 @@ interface AllergiesProps {

const Allergies = (props: AllergiesProps) => {
const { t } = useTranslation()
const history = useHistory()
const dispatch = useDispatch()
const { patient } = props
const { permissions } = useSelector((state: RootState) => state.user)
Expand All @@ -32,6 +30,10 @@ const Allergies = (props: AllergiesProps) => {
]
useAddBreadcrumbs(breadcrumbs)

const onAddAllergySuccess = () => {
Toast('success', t('Success!'), `${t('patient.allergies.successfullyAdded')}`)
}

const onAddAllergy = (allergy: Allergy) => {
allergy.id = getTimestampId()
const allergies = []
Expand All @@ -41,7 +43,7 @@ const Allergies = (props: AllergiesProps) => {

allergies.push(allergy)
const patientToUpdate = { ...patient, allergies }
dispatch(updatePatient(patientToUpdate, history))
dispatch(updatePatient(patientToUpdate, onAddAllergySuccess))
}

return (
Expand Down

0 comments on commit 73677e2

Please sign in to comment.