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
/
ViewPatient.test.tsx
322 lines (259 loc) · 11.6 KB
/
ViewPatient.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { TabsHeader, Tab } 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 { Route, Router } from 'react-router-dom'
import createMockStore, { MockStore } from 'redux-mock-store'
import thunk from 'redux-thunk'
import { mocked } from 'ts-jest/utils'
import * as ButtonBarProvider from '../../../page-header/button-toolbar/ButtonBarProvider'
import * as titleUtil from '../../../page-header/title/useTitle'
import Allergies from '../../../patients/allergies/Allergies'
import AppointmentsList from '../../../patients/appointments/AppointmentsList'
import CarePlanTab from '../../../patients/care-plans/CarePlanTab'
import Diagnoses from '../../../patients/diagnoses/Diagnoses'
import GeneralInformation from '../../../patients/GeneralInformation'
import LabsTab from '../../../patients/labs/LabsTab'
import NotesTab from '../../../patients/notes/NoteTab'
import * as patientSlice from '../../../patients/patient-slice'
import RelatedPersonTab from '../../../patients/related-persons/RelatedPersonTab'
import ViewPatient from '../../../patients/view/ViewPatient'
import PatientRepository from '../../../shared/db/PatientRepository'
import Patient from '../../../shared/model/Patient'
import Permissions from '../../../shared/model/Permissions'
import { RootState } from '../../../shared/store'
const mockStore = createMockStore<RootState, any>([thunk])
describe('ViewPatient', () => {
const patient = {
id: '123',
prefix: 'prefix',
givenName: 'givenName',
familyName: 'familyName',
suffix: 'suffix',
sex: 'male',
type: 'charity',
occupation: 'occupation',
preferredLanguage: 'preferredLanguage',
phoneNumber: 'phoneNumber',
email: 'email@email.com',
address: 'address',
code: 'P00001',
dateOfBirth: new Date().toISOString(),
} as Patient
let history: any
let store: MockStore
const setup = async (permissions = [Permissions.ReadPatients]) => {
jest.spyOn(PatientRepository, 'find')
jest.spyOn(PatientRepository, 'getLabs').mockResolvedValue([])
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.find.mockResolvedValue(patient)
history = createMemoryHistory()
store = mockStore({
patient: { patient },
user: { permissions },
appointments: { appointments: [] },
} as any)
history.push('/patients/123')
let wrapper: any
await act(async () => {
wrapper = await mount(
<Provider store={store}>
<Router history={history}>
<Route path="/patients/:id">
<ViewPatient />
</Route>
</Router>
</Provider>,
)
})
wrapper.update()
return { wrapper: wrapper as ReactWrapper }
}
beforeEach(() => {
jest.restoreAllMocks()
})
it('should dispatch fetchPatient when component loads', async () => {
await setup()
expect(PatientRepository.find).toHaveBeenCalledWith(patient.id)
expect(store.getActions()).toContainEqual(patientSlice.fetchPatientStart())
expect(store.getActions()).toContainEqual(patientSlice.fetchPatientSuccess(patient))
})
it('should render a header with the patients given, family, and suffix', async () => {
jest.spyOn(titleUtil, 'default')
await setup()
expect(titleUtil.default).toHaveBeenCalledWith(
`${patient.givenName} ${patient.familyName} ${patient.suffix} (${patient.code})`,
)
})
it('should add a "Edit Patient" button to the button tool bar if has WritePatients permissions', async () => {
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter')
const setButtonToolBarSpy = jest.fn()
mocked(ButtonBarProvider).useButtonToolbarSetter.mockReturnValue(setButtonToolBarSpy)
await setup([Permissions.WritePatients])
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect((actualButtons[0] as any).props.children).toEqual('actions.edit')
})
it('button toolbar empty if only has ReadPatients permission', async () => {
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter')
const setButtonToolBarSpy = jest.fn()
mocked(ButtonBarProvider).useButtonToolbarSetter.mockReturnValue(setButtonToolBarSpy)
await setup()
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect(actualButtons.length).toEqual(0)
})
it('should render a tabs header with the correct tabs', async () => {
const { wrapper } = await setup()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
expect(tabsHeader).toHaveLength(1)
expect(tabs).toHaveLength(9)
expect(tabs.at(0).prop('label')).toEqual('patient.generalInformation')
expect(tabs.at(1).prop('label')).toEqual('patient.relatedPersons.label')
expect(tabs.at(2).prop('label')).toEqual('scheduling.appointments.label')
expect(tabs.at(3).prop('label')).toEqual('patient.allergies.label')
expect(tabs.at(4).prop('label')).toEqual('patient.diagnoses.label')
expect(tabs.at(5).prop('label')).toEqual('patient.notes.label')
expect(tabs.at(6).prop('label')).toEqual('patient.labs.label')
expect(tabs.at(7).prop('label')).toEqual('patient.carePlan.label')
expect(tabs.at(8).prop('label')).toEqual('patient.visits.label')
})
it('should mark the general information tab as active and render the general information component when route is /patients/:id', async () => {
const { wrapper } = await setup()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const generalInformation = wrapper.find(GeneralInformation)
expect(tabs.at(0).prop('active')).toBeTruthy()
expect(generalInformation).toHaveLength(1)
expect(generalInformation.prop('patient')).toEqual(patient)
})
it('should navigate /patients/:id when the general information tab is clicked', async () => {
const { wrapper } = await setup()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
await act(async () => {
await (tabs.at(0).prop('onClick') as any)()
})
wrapper.update()
expect(history.location.pathname).toEqual('/patients/123')
})
it('should mark the related persons tab as active when it is clicked and render the Related Person Tab component when route is /patients/:id/relatedpersons', async () => {
const { wrapper } = await setup()
await act(async () => {
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const onClick = tabs.at(1).prop('onClick') as any
onClick()
})
wrapper.update()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const relatedPersonTab = wrapper.find(RelatedPersonTab)
expect(history.location.pathname).toEqual(`/patients/${patient.id}/relatedpersons`)
expect(tabs.at(1).prop('active')).toBeTruthy()
expect(relatedPersonTab).toHaveLength(1)
expect(relatedPersonTab.prop('patient')).toEqual(patient)
})
it('should mark the appointments tab as active when it is clicked and render the appointments tab component when route is /patients/:id/appointments', async () => {
const { wrapper } = await setup()
await act(async () => {
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const onClick = tabs.at(2).prop('onClick') as any
onClick()
})
wrapper.update()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const appointmentsTab = wrapper.find(AppointmentsList)
expect(history.location.pathname).toEqual(`/patients/${patient.id}/appointments`)
expect(tabs.at(2).prop('active')).toBeTruthy()
expect(appointmentsTab).toHaveLength(1)
expect(appointmentsTab.prop('patientId')).toEqual(patient.id)
})
it('should mark the allergies tab as active when it is clicked and render the allergies component when route is /patients/:id/allergies', async () => {
const { wrapper } = await setup()
await act(async () => {
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const onClick = tabs.at(3).prop('onClick') as any
onClick()
})
wrapper.update()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const allergiesTab = wrapper.find(Allergies)
expect(history.location.pathname).toEqual(`/patients/${patient.id}/allergies`)
expect(tabs.at(3).prop('active')).toBeTruthy()
expect(allergiesTab).toHaveLength(1)
expect(allergiesTab.prop('patient')).toEqual(patient)
})
it('should mark the diagnoses tab as active when it is clicked and render the diagnoses component when route is /patients/:id/diagnoses', async () => {
const { wrapper } = await setup()
await act(async () => {
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const onClick = tabs.at(4).prop('onClick') as any
onClick()
})
wrapper.update()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const diagnosesTab = wrapper.find(Diagnoses)
expect(history.location.pathname).toEqual(`/patients/${patient.id}/diagnoses`)
expect(tabs.at(4).prop('active')).toBeTruthy()
expect(diagnosesTab).toHaveLength(1)
expect(diagnosesTab.prop('patient')).toEqual(patient)
})
it('should mark the notes tab as active when it is clicked and render the note component when route is /patients/:id/notes', async () => {
const { wrapper } = await setup()
await act(async () => {
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const onClick = tabs.at(5).prop('onClick') as any
onClick()
})
wrapper.update()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const notesTab = wrapper.find(NotesTab)
expect(history.location.pathname).toEqual(`/patients/${patient.id}/notes`)
expect(tabs.at(5).prop('active')).toBeTruthy()
expect(notesTab).toHaveLength(1)
expect(notesTab.prop('patient')).toEqual(patient)
})
it('should mark the labs tab as active when it is clicked and render the lab component when route is /patients/:id/labs', async () => {
const { wrapper } = await setup()
await act(async () => {
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const onClick = tabs.at(6).prop('onClick') as any
onClick()
})
wrapper.update()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const labsTab = wrapper.find(LabsTab)
expect(history.location.pathname).toEqual(`/patients/${patient.id}/labs`)
expect(tabs.at(6).prop('active')).toBeTruthy()
expect(labsTab).toHaveLength(1)
expect(labsTab.prop('patientId')).toEqual(patient.id)
})
it('should mark the care plans tab as active when it is clicked and render the care plan tab component when route is /patients/:id/care-plans', async () => {
const { wrapper } = await setup()
await act(async () => {
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const onClick = tabs.at(7).prop('onClick') as any
onClick()
})
wrapper.update()
const tabsHeader = wrapper.find(TabsHeader)
const tabs = tabsHeader.find(Tab)
const carePlansTab = wrapper.find(CarePlanTab)
expect(history.location.pathname).toEqual(`/patients/${patient.id}/care-plans`)
expect(tabs.at(7).prop('active')).toBeTruthy()
expect(carePlansTab).toHaveLength(1)
})
})