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
/
Diagnoses.test.tsx
159 lines (130 loc) · 5.15 KB
/
Diagnoses.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import '../../../__mocks__/matchMediaMock'
import React from 'react'
import { mount } from 'enzyme'
import { createMemoryHistory } from 'history'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import Patient from 'model/Patient'
import Diagnosis from 'model/Diagnosis'
import Permissions from 'model/Permissions'
import { Router } from 'react-router'
import { Provider } from 'react-redux'
import Diagnoses from 'patients/diagnoses/Diagnoses'
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'
import AddDiagnosisModal from 'patients/diagnoses/AddDiagnosisModal'
import * as patientSlice from '../../../patients/patient-slice'
const expectedPatient = {
id: '123',
diagnoses: [
{ id: '123', name: 'diagnosis1', diagnosisDate: new Date().toISOString() } as Diagnosis,
],
} as Patient
const mockStore = configureMockStore([thunk])
const history = createMemoryHistory()
let user: any
let store: any
const setup = (patient = expectedPatient, permissions = [Permissions.AddDiagnosis]) => {
user = { permissions }
store = mockStore({ patient, user })
const wrapper = mount(
<Router history={history}>
<Provider store={store}>
<Diagnoses patient={patient} />
</Provider>
</Router>,
)
return wrapper
}
describe('Diagnoses', () => {
describe('add diagnoses button', () => {
beforeEach(() => {
jest.resetAllMocks()
jest.spyOn(PatientRepository, 'saveOrUpdate')
})
it('should render a add diagnoses button', () => {
const wrapper = setup()
const addDiagnosisButton = wrapper.find(components.Button)
expect(addDiagnosisButton).toHaveLength(1)
expect(addDiagnosisButton.text().trim()).toEqual('patient.diagnoses.new')
})
it('should not render a diagnoses button if the user does not have permissions', () => {
const wrapper = setup(expectedPatient, [])
const addDiagnosisButton = wrapper.find(components.Button)
expect(addDiagnosisButton).toHaveLength(0)
})
it('should open the Add Diagnosis Modal', () => {
const wrapper = setup()
act(() => {
wrapper.find(components.Button).prop('onClick')()
})
wrapper.update()
expect(wrapper.find(components.Modal).prop('show')).toBeTruthy()
})
it('should update the patient with the new diagnosis when the save button is clicked', async () => {
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(mockedPatientRepository.saveOrUpdate).toHaveBeenCalledWith(expectedUpdatedPatient)
expect(store.getActions()).toContainEqual(patientSlice.updatePatientStart())
expect(store.getActions()).toContainEqual(
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',
'states.success',
'patient.diagnoses.successfullyAdded',
)
})
})
describe('diagnoses list', () => {
it('should list the patients diagnoses', () => {
const diagnoses = expectedPatient.diagnoses as Diagnosis[]
const wrapper = setup()
const list = wrapper.find(components.List)
const listItems = wrapper.find(components.ListItem)
expect(list).toHaveLength(1)
expect(listItems).toHaveLength(diagnoses.length)
})
it('should render a warning message if the patient does not have any diagnoses', () => {
const wrapper = setup({ ...expectedPatient, diagnoses: [] })
const alert = wrapper.find(components.Alert)
expect(alert).toHaveLength(1)
expect(alert.prop('title')).toEqual('patient.diagnoses.warning.noDiagnoses')
expect(alert.prop('message')).toEqual('patient.diagnoses.addDiagnosisAbove')
})
})
})