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
/
HospitalRun.test.tsx
328 lines (289 loc) · 10.6 KB
/
HospitalRun.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
323
324
325
326
327
328
import '../__mocks__/matchMediaMock'
import React from 'react'
import { mount } from 'enzyme'
import { MemoryRouter } from 'react-router'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import { Toaster } from '@hospitalrun/components'
import { act } from 'react-dom/test-utils'
import Dashboard from 'dashboard/Dashboard'
import Appointments from 'scheduling/appointments/Appointments'
import NewAppointment from 'scheduling/appointments/new/NewAppointment'
import EditAppointment from 'scheduling/appointments/edit/EditAppointment'
import { addBreadcrumbs } from 'breadcrumbs/breadcrumbs-slice'
import ViewLabs from 'labs/ViewLabs'
import LabRepository from 'clients/db/LabRepository'
import PatientRepository from '../clients/db/PatientRepository'
import AppointmentRepository from '../clients/db/AppointmentRepository'
import Patient from '../model/Patient'
import Appointment from '../model/Appointment'
import HospitalRun from '../HospitalRun'
import Permissions from '../model/Permissions'
import Incidents from '../incidents/Incidents'
const mockStore = configureMockStore([thunk])
describe('HospitalRun', () => {
describe('routing', () => {
describe('/appointments', () => {
it('should render the appointments screen when /appointments is accessed', async () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ReadAppointments] },
appointments: { appointments: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/appointments']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
await act(async () => {
wrapper.update()
})
expect(wrapper.find(Appointments)).toHaveLength(1)
expect(store.getActions()).toContainEqual(
addBreadcrumbs([
{ i18nKey: 'scheduling.appointments.label', location: '/appointments' },
{ i18nKey: 'dashboard.label', location: '/' },
]),
)
})
it('should render the Dashboard when the user does not have read appointment privileges', () => {
const wrapper = mount(
<Provider
store={mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
>
<MemoryRouter initialEntries={['/appointments']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
})
describe('/appointments/new', () => {
it('should render the new appointment screen when /appointments/new is accessed', async () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.WriteAppointments] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/appointments/new']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
wrapper.update()
expect(wrapper.find(NewAppointment)).toHaveLength(1)
expect(store.getActions()).toContainEqual(
addBreadcrumbs([
{ i18nKey: 'scheduling.appointments.label', location: '/appointments' },
{ i18nKey: 'scheduling.appointments.new', location: '/appointments/new' },
{ i18nKey: 'dashboard.label', location: '/' },
]),
)
})
it('should render the Dashboard when the user does not have read appointment privileges', () => {
const wrapper = mount(
<Provider
store={mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
>
<MemoryRouter initialEntries={['/appointments/new']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
})
describe('/appointments/edit/:id', () => {
it('should render the edit appointment screen when /appointments/edit/:id is accessed', () => {
const appointment = {
id: '123',
patientId: '456',
} as Appointment
const patient = {
id: '456',
} as Patient
jest.spyOn(AppointmentRepository, 'find').mockResolvedValue(appointment)
jest.spyOn(PatientRepository, 'find').mockResolvedValue(patient)
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.WriteAppointments, Permissions.ReadAppointments] },
appointment: { appointment, patient: {} as Patient },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/appointments/edit/123']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(EditAppointment)).toHaveLength(1)
expect(store.getActions()).toContainEqual(
addBreadcrumbs([
{ i18nKey: 'scheduling.appointments.label', location: '/appointments' },
{ text: '123', location: '/appointments/123' },
{
i18nKey: 'scheduling.appointments.editAppointment',
location: '/appointments/edit/123',
},
{ i18nKey: 'dashboard.label', location: '/' },
]),
)
})
it('should render the Dashboard when the user does not have read appointment privileges', () => {
const wrapper = mount(
<Provider
store={mockStore({
title: 'test',
user: { permissions: [Permissions.WriteAppointments] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
>
<MemoryRouter initialEntries={['/appointments/edit/123']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
it('should render the Dashboard when the user does not have write appointment privileges', () => {
const wrapper = mount(
<Provider
store={mockStore({
title: 'test',
user: { permissions: [Permissions.ReadAppointments] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
>
<MemoryRouter initialEntries={['/appointments/edit/123']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
})
describe('/labs', () => {
it('should render the Labs component when /labs is accessed', async () => {
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ViewLabs] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
let wrapper: any
await act(async () => {
wrapper = await mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
})
wrapper.update()
expect(wrapper.find(ViewLabs)).toHaveLength(1)
})
it('should render the dashboard if the user does not have permissions to view labs', () => {
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
const store = mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/labs']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(ViewLabs)).toHaveLength(0)
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
})
describe('/incidents', () => {
it('should render the Incidents component when /incidents is accessed', async () => {
const store = mockStore({
title: 'test',
user: { permissions: [Permissions.ViewIncidents] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
let wrapper: any
await act(async () => {
wrapper = await mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/incidents']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
})
wrapper.update()
expect(wrapper.find(Incidents)).toHaveLength(1)
})
it('should render the dashboard if the user does not have permissions to view incidents', () => {
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
const store = mockStore({
title: 'test',
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
const wrapper = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/incidents']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(Incidents)).toHaveLength(0)
expect(wrapper.find(Dashboard)).toHaveLength(1)
})
})
})
describe('layout', () => {
it('should render a Toaster', () => {
const wrapper = mount(
<Provider
store={mockStore({
title: 'test',
user: { permissions: [Permissions.WritePatients] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
>
<MemoryRouter initialEntries={['/']}>
<HospitalRun />
</MemoryRouter>
</Provider>,
)
expect(wrapper.find(Toaster)).toHaveLength(1)
})
})
})