diff --git a/src/__tests__/patients/allergies/Allergies.test.tsx b/src/__tests__/patients/allergies/Allergies.test.tsx index ef3fbf692f..73fc40b7f5 100644 --- a/src/__tests__/patients/allergies/Allergies.test.tsx +++ b/src/__tests__/patients/allergies/Allergies.test.tsx @@ -9,8 +9,8 @@ import createMockStore from 'redux-mock-store' import thunk from 'redux-thunk' import Allergies from '../../../patients/allergies/Allergies' +import AllergiesList from '../../../patients/allergies/AllergiesList' import PatientRepository from '../../../shared/db/PatientRepository' -import Allergy from '../../../shared/model/Allergy' import Patient from '../../../shared/model/Patient' import Permissions from '../../../shared/model/Permissions' import { RootState } from '../../../shared/store' @@ -26,12 +26,15 @@ const expectedPatient = { ], } as Patient -let user: any let store: any -const setup = (patient = expectedPatient, permissions = [Permissions.AddAllergy]) => { - user = { permissions } - store = mockStore({ patient, user } as any) +const setup = ( + patient = expectedPatient, + permissions = [Permissions.AddAllergy], + route = '/patients/123/allergies', +) => { + store = mockStore({ patient: { patient }, user: { permissions } } as any) + history.push(route) const wrapper = mount( @@ -81,15 +84,11 @@ describe('Allergies', () => { }) describe('allergy list', () => { - it('should list the patients allergies', () => { - const allergies = expectedPatient.allergies as Allergy[] + it('should render allergies', () => { const wrapper = setup() + const allergiesList = wrapper.find(AllergiesList) - const list = wrapper.find(components.List) - const listItems = wrapper.find(components.ListItem) - - expect(list).toHaveLength(1) - expect(listItems).toHaveLength(allergies.length) + expect(allergiesList).toHaveLength(1) }) it('should render a warning message if the patient does not have any allergies', () => { diff --git a/src/__tests__/patients/allergies/AllergiesList.test.tsx b/src/__tests__/patients/allergies/AllergiesList.test.tsx new file mode 100644 index 0000000000..f48ff1213c --- /dev/null +++ b/src/__tests__/patients/allergies/AllergiesList.test.tsx @@ -0,0 +1,63 @@ +import { List, ListItem } from '@hospitalrun/components' +import { mount, ReactWrapper } 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 AllergiesList from '../../../patients/allergies/AllergiesList' +import Allergy from '../../../shared/model/Allergy' +import Patient from '../../../shared/model/Patient' +import { RootState } from '../../../shared/store' + +const mockStore = createMockStore([thunk]) + +describe('Allergies list', () => { + const allergy: Allergy = { + id: 'id', + name: 'name', + } + const patient = { + id: 'patientId', + diagnoses: [{ id: '123', name: 'some name', diagnosisDate: new Date().toISOString() }], + allergies: [allergy], + } as Patient + + const setup = () => { + const store = mockStore({ patient: { patient } } as any) + const history = createMemoryHistory() + history.push(`/patients/${patient.id}/allergies/${patient.allergies![0].id}`) + const wrapper = mount( + + + + + , + ) + return { wrapper: wrapper as ReactWrapper, history } + } + + it('should render a list', () => { + const allergies = patient.allergies as Allergy[] + const { wrapper } = setup() + const list = wrapper.find(List) + const listItems = wrapper.find(ListItem) + + expect(list).toHaveLength(1) + expect(listItems).toHaveLength(allergies.length) + }) + + it('should navigate to the allergy view when the allergy is clicked', () => { + const { wrapper, history } = setup() + const item = wrapper.find(ListItem) + act(() => { + const onClick = item.prop('onClick') as any + onClick({ stopPropagation: jest.fn() }) + }) + + expect(history.location.pathname).toEqual(`/patients/${patient.id}/allergies/${allergy.id}`) + }) +}) diff --git a/src/__tests__/patients/allergies/ViewAllergy.test.tsx b/src/__tests__/patients/allergies/ViewAllergy.test.tsx new file mode 100644 index 0000000000..eb2b85e398 --- /dev/null +++ b/src/__tests__/patients/allergies/ViewAllergy.test.tsx @@ -0,0 +1,46 @@ +import { mount } from 'enzyme' +import { createMemoryHistory } from 'history' +import React from 'react' +import { Provider } from 'react-redux' +import { Route, Router } from 'react-router-dom' +import createMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' + +import ViewAllergy from '../../../patients/allergies/ViewAllergy' +import TextInputWithLabelFormGroup from '../../../shared/components/input/TextInputWithLabelFormGroup' +import Patient from '../../../shared/model/Patient' +import { RootState } from '../../../shared/store' + +const mockStore = createMockStore([thunk]) + +describe('View Care Plan', () => { + const patient = { + id: 'patientId', + allergies: [{ id: '123', name: 'some name' }], + } as Patient + + const setup = () => { + const store = mockStore({ patient: { patient }, user: { user: { id: '123' } } } as any) + const history = createMemoryHistory() + history.push(`/patients/${patient.id}/allergies/${patient.allergies![0].id}`) + const wrapper = mount( + + + + + + + , + ) + + return { wrapper } + } + + it('should render a allergy input with the correct data', () => { + const { wrapper } = setup() + + const allergyName = wrapper.find(TextInputWithLabelFormGroup) + expect(allergyName).toHaveLength(1) + expect(allergyName.prop('value')).toEqual(patient.allergies![0].name) + }) +}) diff --git a/src/patients/allergies/Allergies.tsx b/src/patients/allergies/Allergies.tsx index d37c5327e9..555285189a 100644 --- a/src/patients/allergies/Allergies.tsx +++ b/src/patients/allergies/Allergies.tsx @@ -1,14 +1,16 @@ -import { Button, List, ListItem, Alert } from '@hospitalrun/components' +import { Button, Alert } from '@hospitalrun/components' import React, { useState } from 'react' import { useSelector } from 'react-redux' +import { Route, Switch } from 'react-router-dom' import useAddBreadcrumbs from '../../page-header/breadcrumbs/useAddBreadcrumbs' import useTranslator from '../../shared/hooks/useTranslator' -import Allergy from '../../shared/model/Allergy' import Patient from '../../shared/model/Patient' import Permissions from '../../shared/model/Permissions' import { RootState } from '../../shared/store' +import AllergiesList from './AllergiesList' import NewAllergyModal from './NewAllergyModal' +import ViewAllergy from './ViewAllergy' interface AllergiesProps { patient: Patient @@ -46,6 +48,14 @@ const Allergies = (props: AllergiesProps) => {
+ + + + + + + + {(!patient.allergies || patient.allergies.length === 0) && ( { message={t('patient.allergies.addAllergyAbove')} /> )} - - {patient.allergies?.map((a: Allergy) => ( - {a.name} - ))} - setShowNewAllergyModal(false)} diff --git a/src/patients/allergies/AllergiesList.tsx b/src/patients/allergies/AllergiesList.tsx new file mode 100644 index 0000000000..109c4aacfa --- /dev/null +++ b/src/patients/allergies/AllergiesList.tsx @@ -0,0 +1,28 @@ +import { List, ListItem } from '@hospitalrun/components' +import React from 'react' +import { useSelector } from 'react-redux' +import { useHistory } from 'react-router-dom' + +import Allergy from '../../shared/model/Allergy' +import { RootState } from '../../shared/store' + +const AllergiesList = () => { + const history = useHistory() + const { patient } = useSelector((state: RootState) => state.patient) + + return ( + + {patient.allergies?.map((a: Allergy) => ( + history.push(`/patients/${patient.id}/allergies/${a.id}`)} + > + {a.name} + + ))} + + ) +} + +export default AllergiesList diff --git a/src/patients/allergies/ViewAllergy.tsx b/src/patients/allergies/ViewAllergy.tsx new file mode 100644 index 0000000000..703d0891be --- /dev/null +++ b/src/patients/allergies/ViewAllergy.tsx @@ -0,0 +1,40 @@ +import React, { useEffect, useState } from 'react' +import { useSelector } from 'react-redux' +import { useParams } from 'react-router-dom' + +import TextInputWithLabelFormGroup from '../../shared/components/input/TextInputWithLabelFormGroup' +import useTranslator from '../../shared/hooks/useTranslator' +import Allergy from '../../shared/model/Allergy' +import { RootState } from '../../shared/store' + +const ViewAllergy = () => { + const { t } = useTranslator() + const { patient } = useSelector((root: RootState) => root.patient) + const { allergyId } = useParams() + + const [allergy, setAllergy] = useState() + + useEffect(() => { + if (patient && allergyId) { + const currentAllergy = patient.allergies?.find((a: Allergy) => a.id === allergyId) + setAllergy(currentAllergy) + } + }, [setAllergy, allergyId, patient]) + + if (allergy) { + return ( + <> + + + ) + } + return <> +} + +export default ViewAllergy diff --git a/src/patients/view/ViewPatient.tsx b/src/patients/view/ViewPatient.tsx index bb4218e783..233dad0959 100644 --- a/src/patients/view/ViewPatient.tsx +++ b/src/patients/view/ViewPatient.tsx @@ -144,7 +144,7 @@ const ViewPatient = () => { - +