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
/
EditAppointment.test.tsx
224 lines (182 loc) · 6.7 KB
/
EditAppointment.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
import '../../../../__mocks__/matchMediaMock'
import React from 'react'
import { mount } from 'enzyme'
import { Router, Route } from 'react-router-dom'
import { Provider } from 'react-redux'
import { mocked } from 'ts-jest/utils'
import { createMemoryHistory } from 'history'
import { act } from 'react-dom/test-utils'
import configureMockStore, { MockStore } from 'redux-mock-store'
import thunk from 'redux-thunk'
import { roundToNearestMinutes, addMinutes, subDays } from 'date-fns'
import { Button, Alert } from '@hospitalrun/components'
import EditAppointment from '../../../../scheduling/appointments/edit/EditAppointment'
import AppointmentDetailForm from '../../../../scheduling/appointments/AppointmentDetailForm'
import Appointment from '../../../../model/Appointment'
import Patient from '../../../../model/Patient'
import * as titleUtil from '../../../../page-header/useTitle'
import * as appointmentSlice from '../../../../scheduling/appointments/appointment-slice'
import AppointmentRepository from '../../../../clients/db/AppointmentRepository'
import PatientRepository from '../../../../clients/db/PatientRepository'
const mockStore = configureMockStore([thunk])
describe('Edit Appointment', () => {
const appointment = {
id: '123',
patientId: '456',
startDateTime: roundToNearestMinutes(new Date(), { nearestTo: 15 }).toISOString(),
endDateTime: addMinutes(roundToNearestMinutes(new Date(), { nearestTo: 15 }), 60).toISOString(),
location: 'location',
reason: 'reason',
type: 'type',
} as Appointment
const patient = {
id: '456',
prefix: 'prefix',
givenName: 'givenName',
familyName: 'familyName',
suffix: 'suffix',
fullName: 'givenName familyName suffix',
sex: 'male',
type: 'charity',
occupation: 'occupation',
preferredLanguage: 'preferredLanguage',
phoneNumber: 'phoneNumber',
email: 'email@email.com',
address: 'address',
friendlyId: 'P00001',
dateOfBirth: new Date().toISOString(),
} as Patient
let history: any
let store: MockStore
const setup = () => {
jest.spyOn(AppointmentRepository, 'saveOrUpdate')
jest.spyOn(AppointmentRepository, 'find')
jest.spyOn(PatientRepository, 'find')
const mockedAppointmentRepository = mocked(AppointmentRepository, true)
mockedAppointmentRepository.find.mockResolvedValue(appointment)
mockedAppointmentRepository.saveOrUpdate.mockResolvedValue(appointment)
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.find.mockResolvedValue(patient)
history = createMemoryHistory()
store = mockStore({ appointment: { appointment, patient } })
history.push('/appointments/edit/123')
const wrapper = mount(
<Provider store={store}>
<Router history={history}>
<Route path="/appointments/edit/:id">
<EditAppointment />
</Route>
</Router>
</Provider>,
)
wrapper.update()
return wrapper
}
beforeEach(() => {
jest.restoreAllMocks()
})
it('should render an edit appointment form', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup()
})
wrapper.update()
expect(wrapper.find(AppointmentDetailForm)).toHaveLength(1)
})
it('should dispatch fetchAppointment when component loads', async () => {
await act(async () => {
await setup()
})
expect(AppointmentRepository.find).toHaveBeenCalledWith(appointment.id)
expect(PatientRepository.find).toHaveBeenCalledWith(appointment.patientId)
expect(store.getActions()).toContainEqual(appointmentSlice.fetchAppointmentStart())
expect(store.getActions()).toContainEqual(
appointmentSlice.fetchAppointmentSuccess({ appointment, patient }),
)
})
it('should use the correct title', async () => {
jest.spyOn(titleUtil, 'default')
await act(async () => {
await setup()
})
expect(titleUtil.default).toHaveBeenCalledWith('scheduling.appointments.editAppointment')
})
it('should display an error if the end date is before the start date', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup()
})
const startDateTime = roundToNearestMinutes(new Date(), { nearestTo: 15 })
const endDateTime = subDays(startDateTime, 1)
wrapper.update()
act(() => {
const appointmentDetailForm = wrapper.find(AppointmentDetailForm)
const onFieldChange = appointmentDetailForm.prop('onFieldChange')
onFieldChange('startDateTime', startDateTime)
})
wrapper.update()
act(() => {
const appointmentDetailForm = wrapper.find(AppointmentDetailForm)
const onFieldChange = appointmentDetailForm.prop('onFieldChange')
onFieldChange('endDateTime', endDateTime)
})
wrapper.update()
act(() => {
const saveButton = wrapper.find(Button).at(0)
const onClick = saveButton.prop('onClick') as any
onClick()
})
wrapper.update()
const alert = wrapper.find(Alert)
expect(alert).toHaveLength(1)
expect(alert.prop('message')).toEqual(
'scheduling.appointment.errors.startDateMustBeBeforeEndDate',
)
})
it('should dispatch updateAppointment when save button is clicked', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup()
})
wrapper.update()
const saveButton = wrapper.find(Button).at(0)
const onClick = saveButton.prop('onClick') as any
expect(saveButton.text().trim()).toEqual('actions.save')
await act(async () => {
await onClick()
})
expect(AppointmentRepository.saveOrUpdate).toHaveBeenCalledWith(appointment)
expect(store.getActions()).toContainEqual(appointmentSlice.updateAppointmentStart())
expect(store.getActions()).toContainEqual(
appointmentSlice.updateAppointmentSuccess(appointment),
)
})
it('should navigate to /appointments/:id when save is successful', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup()
})
wrapper.update()
const saveButton = wrapper.find(Button).at(0)
const onClick = saveButton.prop('onClick') as any
await act(async () => {
await onClick()
})
expect(history.location.pathname).toEqual('/appointments/123')
})
it('should navigate to /appointments/:id when cancel is clicked', async () => {
let wrapper: any
await act(async () => {
wrapper = await setup()
})
wrapper.update()
const cancelButton = wrapper.find(Button).at(1)
const onClick = cancelButton.prop('onClick') as any
expect(cancelButton.text().trim()).toEqual('actions.cancel')
act(() => {
onClick()
})
wrapper.update()
expect(history.location.pathname).toEqual('/appointments/123')
})
})