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
/
ViewAppointment.test.tsx
301 lines (249 loc) · 10.2 KB
/
ViewAppointment.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
import '../../../../__mocks__/matchMediaMock'
import React from 'react'
import { mount } from 'enzyme'
import { Provider } from 'react-redux'
import configureMockStore, { MockStore } from 'redux-mock-store'
import thunk from 'redux-thunk'
import Appointment from 'model/Appointment'
import ViewAppointment from 'scheduling/appointments/view/ViewAppointment'
import { Router, Route } from 'react-router-dom'
import { createMemoryHistory } from 'history'
import AppointmentRepository from 'clients/db/AppointmentRepository'
import { mocked } from 'ts-jest/utils'
import { act } from 'react-dom/test-utils'
import * as components from '@hospitalrun/components'
import AppointmentDetailForm from 'scheduling/appointments/AppointmentDetailForm'
import PatientRepository from 'clients/db/PatientRepository'
import Patient from 'model/Patient'
import * as ButtonBarProvider from 'page-header/ButtonBarProvider'
import Permissions from 'model/Permissions'
import * as titleUtil from '../../../../page-header/useTitle'
import * as appointmentSlice from '../../../../scheduling/appointments/appointment-slice'
const mockStore = configureMockStore([thunk])
const appointment = {
id: '123',
startDateTime: new Date().toISOString(),
endDateTime: new Date().toISOString(),
reason: 'reason',
location: 'location',
} as Appointment
const patient = {
id: '123',
fullName: 'full name',
} as Patient
describe('View Appointment', () => {
let history: any
let store: MockStore
const setup = (status: string, permissions = [Permissions.ReadAppointments]) => {
jest.spyOn(AppointmentRepository, 'find')
jest.spyOn(AppointmentRepository, 'delete')
const mockedAppointmentRepository = mocked(AppointmentRepository, true)
mockedAppointmentRepository.find.mockResolvedValue(appointment)
mockedAppointmentRepository.delete.mockResolvedValue(appointment)
jest.spyOn(PatientRepository, 'find')
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.find.mockResolvedValue(patient)
history = createMemoryHistory()
history.push('/appointments/123')
store = mockStore({
user: {
permissions,
},
appointment: {
appointment,
status,
patient,
},
})
const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<Route path="/appointments/:id">
<ViewAppointment />
</Route>
</Router>
</Provider>,
)
wrapper.update()
return wrapper
}
beforeEach(() => {
jest.restoreAllMocks()
})
it('should use the correct title', async () => {
jest.spyOn(titleUtil, 'default')
await act(async () => {
await setup('loading')
})
expect(titleUtil.default).toHaveBeenCalledWith('scheduling.appointments.viewAppointment')
})
it('should add a "Edit Appointment" button to the button tool bar if has WriteAppointment permissions', () => {
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter')
const setButtonToolBarSpy = jest.fn()
mocked(ButtonBarProvider).useButtonToolbarSetter.mockReturnValue(setButtonToolBarSpy)
setup('loading', [Permissions.WriteAppointments, Permissions.ReadAppointments])
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect((actualButtons[0] as any).props.children).toEqual('actions.edit')
})
it('should add a "Delete Appointment" button to the button tool bar if has DeleteAppointment permissions', () => {
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter')
const setButtonToolBarSpy = jest.fn()
mocked(ButtonBarProvider).useButtonToolbarSetter.mockReturnValue(setButtonToolBarSpy)
setup('loading', [Permissions.DeleteAppointment, Permissions.ReadAppointments])
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect((actualButtons[0] as any).props.children).toEqual(
'scheduling.appointments.deleteAppointment',
)
})
it('button toolbar empty if has only ReadAppointments permission', () => {
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter')
const setButtonToolBarSpy = jest.fn()
mocked(ButtonBarProvider).useButtonToolbarSetter.mockReturnValue(setButtonToolBarSpy)
setup('loading')
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect(actualButtons.length).toEqual(0)
})
it('should dispatch getAppointment if id is present', async () => {
await act(async () => {
await setup('loading')
})
expect(AppointmentRepository.find).toHaveBeenCalledWith(appointment.id)
expect(store.getActions()).toContainEqual(appointmentSlice.fetchAppointmentStart())
expect(store.getActions()).toContainEqual(
appointmentSlice.fetchAppointmentSuccess({ appointment, patient }),
)
})
it('should render a loading spinner', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup('loading')
})
expect(wrapper.find(components.Spinner)).toHaveLength(1)
})
it('should render a AppointmentDetailForm with the correct data', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup('completed')
})
const appointmentDetailForm = wrapper.find(AppointmentDetailForm)
expect(appointmentDetailForm.prop('appointment')).toEqual(appointment)
expect(appointmentDetailForm.prop('isEditable')).toBeFalsy()
})
it('should render a modal for delete confirmation', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup('completed')
})
const deleteAppointmentConfirmationModal = wrapper.find(components.Modal)
expect(deleteAppointmentConfirmationModal).toHaveLength(1)
expect(deleteAppointmentConfirmationModal.prop('closeButton').children).toEqual(
'actions.delete',
)
expect(deleteAppointmentConfirmationModal.prop('body')).toEqual(
'scheduling.appointment.deleteConfirmationMessage',
)
expect(deleteAppointmentConfirmationModal.prop('title')).toEqual('actions.confirmDelete')
})
describe('delete appointment', () => {
let setButtonToolBarSpy = jest.fn()
let deleteAppointmentSpy = jest.spyOn(AppointmentRepository, 'delete')
beforeEach(() => {
jest.resetAllMocks()
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter')
deleteAppointmentSpy = jest.spyOn(AppointmentRepository, 'delete')
setButtonToolBarSpy = jest.fn()
mocked(ButtonBarProvider).useButtonToolbarSetter.mockReturnValue(setButtonToolBarSpy)
})
it('should render a delete appointment button in the button toolbar', async () => {
await act(async () => {
await setup('completed', [Permissions.ReadAppointments, Permissions.DeleteAppointment])
})
expect(setButtonToolBarSpy).toHaveBeenCalledTimes(1)
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
expect((actualButtons[0] as any).props.children).toEqual(
'scheduling.appointments.deleteAppointment',
)
})
it('should pop up the modal when on delete appointment click', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup('completed', [
Permissions.ReadAppointments,
Permissions.DeleteAppointment,
])
})
expect(setButtonToolBarSpy).toHaveBeenCalledTimes(1)
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
act(() => {
const { onClick } = (actualButtons[0] as any).props
onClick({ preventDefault: jest.fn() })
})
wrapper.update()
const deleteConfirmationModal = wrapper.find(components.Modal)
expect(deleteConfirmationModal.prop('show')).toEqual(true)
})
it('should close the modal when the toggle button is clicked', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup('completed', [
Permissions.ReadAppointments,
Permissions.DeleteAppointment,
])
})
expect(setButtonToolBarSpy).toHaveBeenCalledTimes(1)
const actualButtons: React.ReactNode[] = setButtonToolBarSpy.mock.calls[0][0]
act(() => {
const { onClick } = (actualButtons[0] as any).props
onClick({ preventDefault: jest.fn() })
})
wrapper.update()
act(() => {
const deleteConfirmationModal = wrapper.find(components.Modal)
deleteConfirmationModal.prop('toggle')()
})
wrapper.update()
const deleteConfirmationModal = wrapper.find(components.Modal)
expect(deleteConfirmationModal.prop('show')).toEqual(false)
})
it('should dispatch DELETE_APPOINTMENT action when modal confirmation button is clicked', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup('completed', [
Permissions.ReadAppointments,
Permissions.DeleteAppointment,
])
})
const deleteConfirmationModal = wrapper.find(components.Modal)
await act(async () => {
await deleteConfirmationModal.prop('closeButton').onClick()
})
wrapper.update()
expect(deleteAppointmentSpy).toHaveBeenCalledTimes(1)
expect(deleteAppointmentSpy).toHaveBeenCalledWith(appointment)
expect(store.getActions()).toContainEqual(appointmentSlice.deleteAppointmentStart())
expect(store.getActions()).toContainEqual(appointmentSlice.deleteAppointmentSuccess())
})
it('should navigate to /appointments and display a message when delete is successful', async () => {
jest.spyOn(components, 'Toast')
const mockedComponents = mocked(components, true)
let wrapper: any
await act(async () => {
wrapper = await setup('completed', [
Permissions.ReadAppointments,
Permissions.DeleteAppointment,
])
})
const deleteConfirmationModal = wrapper.find(components.Modal)
await act(async () => {
await deleteConfirmationModal.prop('closeButton').onClick()
})
wrapper.update()
expect(history.location.pathname).toEqual('/appointments')
expect(mockedComponents.Toast).toHaveBeenCalledWith(
'success',
'states.success',
'scheduling.appointment.successfullyDeleted',
)
})
})
})