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
/
ReportIncident.test.tsx
255 lines (208 loc) · 9.17 KB
/
ReportIncident.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
import { Button } 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 from 'redux-mock-store'
import thunk from 'redux-thunk'
import ReportIncident from '../../../incidents/report/ReportIncident'
import * as validationUtil from '../../../incidents/util/validate-incident'
import * as breadcrumbUtil from '../../../page-header/breadcrumbs/useAddBreadcrumbs'
import * as ButtonBarProvider from '../../../page-header/button-toolbar/ButtonBarProvider'
import * as titleUtil from '../../../page-header/title/useTitle'
import IncidentRepository from '../../../shared/db/IncidentRepository'
import Incident from '../../../shared/model/Incident'
import Permissions from '../../../shared/model/Permissions'
import { RootState } from '../../../shared/store'
const mockStore = createMockStore<RootState, any>([thunk])
describe('Report Incident', () => {
let history: any
beforeEach(() => {
jest.resetAllMocks()
console.error = jest.fn()
})
let setButtonToolBarSpy: any
const setup = async (permissions: Permissions[]) => {
jest.spyOn(breadcrumbUtil, 'default')
setButtonToolBarSpy = jest.fn()
jest.spyOn(titleUtil, 'default')
jest.spyOn(ButtonBarProvider, 'useButtonToolbarSetter').mockReturnValue(setButtonToolBarSpy)
history = createMemoryHistory()
history.push(`/incidents/new`)
const store = mockStore({
title: '',
user: {
permissions,
user: {
id: 'some id',
},
},
} as any)
let wrapper: any
await act(async () => {
wrapper = await mount(
<ButtonBarProvider.ButtonBarProvider>
<Provider store={store}>
<Router history={history}>
<Route path="/incidents/new">
<ReportIncident />
</Route>
</Router>
</Provider>
</ButtonBarProvider.ButtonBarProvider>,
)
})
wrapper.update()
return wrapper as ReactWrapper
}
describe('layout', () => {
it('should set the title', async () => {
await setup([Permissions.ReportIncident])
expect(titleUtil.default).toHaveBeenCalledWith('incidents.reports.new')
})
it('should set the breadcrumbs properly', async () => {
await setup([Permissions.ReportIncident])
expect(breadcrumbUtil.default).toHaveBeenCalledWith([
{ i18nKey: 'incidents.reports.new', location: '/incidents/new' },
])
})
it('should have a date input', async () => {
const wrapper = await setup([Permissions.ReportIncident])
const dateInput = wrapper.findWhere((w) => w.prop('name') === 'dateOfIncident')
expect(dateInput).toHaveLength(1)
expect(dateInput.prop('label')).toEqual('incidents.reports.dateOfIncident')
expect(dateInput.prop('isEditable')).toBeTruthy()
expect(dateInput.prop('isRequired')).toBeTruthy()
})
it('should have a department input', async () => {
const wrapper = await setup([Permissions.ReportIncident])
const departmentInput = wrapper.findWhere((w) => w.prop('name') === 'department')
expect(departmentInput).toHaveLength(1)
expect(departmentInput.prop('label')).toEqual('incidents.reports.department')
expect(departmentInput.prop('isEditable')).toBeTruthy()
expect(departmentInput.prop('isRequired')).toBeTruthy()
})
it('should have a category input', async () => {
const wrapper = await setup([Permissions.ReportIncident])
const categoryInput = wrapper.findWhere((w) => w.prop('name') === 'category')
expect(categoryInput).toHaveLength(1)
expect(categoryInput.prop('label')).toEqual('incidents.reports.category')
expect(categoryInput.prop('isEditable')).toBeTruthy()
expect(categoryInput.prop('isRequired')).toBeTruthy()
})
it('should have a category item input', async () => {
const wrapper = await setup([Permissions.ReportIncident])
const categoryInput = wrapper.findWhere((w) => w.prop('name') === 'categoryItem')
expect(categoryInput).toHaveLength(1)
expect(categoryInput.prop('label')).toEqual('incidents.reports.categoryItem')
expect(categoryInput.prop('isEditable')).toBeTruthy()
expect(categoryInput.prop('isRequired')).toBeTruthy()
})
it('should have a description input', async () => {
const wrapper = await setup([Permissions.ReportIncident])
const descriptionInput = wrapper.findWhere((w) => w.prop('name') === 'description')
expect(descriptionInput).toHaveLength(1)
expect(descriptionInput.prop('label')).toEqual('incidents.reports.description')
expect(descriptionInput.prop('isEditable')).toBeTruthy()
expect(descriptionInput.prop('isRequired')).toBeTruthy()
})
})
describe('on save', () => {
it('should report the incident', async () => {
const wrapper = await setup([Permissions.ReportIncident])
const expectedIncident = {
date: new Date().toISOString(),
department: 'some department',
category: 'some category',
categoryItem: 'some category item',
description: 'some description',
} as Incident
jest
.spyOn(IncidentRepository, 'save')
.mockResolvedValue({ id: 'someId', ...expectedIncident })
const dateInput = wrapper.findWhere((w) => w.prop('name') === 'dateOfIncident')
act(() => {
const onChange = dateInput.prop('onChange')
onChange(new Date(expectedIncident.date))
})
const departmentInput = wrapper.findWhere((w) => w.prop('name') === 'department')
act(() => {
const onChange = departmentInput.prop('onChange')
onChange({ currentTarget: { value: expectedIncident.department } })
})
const categoryInput = wrapper.findWhere((w) => w.prop('name') === 'category')
act(() => {
const onChange = categoryInput.prop('onChange')
onChange({ currentTarget: { value: expectedIncident.category } })
})
const categoryItemInput = wrapper.findWhere((w) => w.prop('name') === 'categoryItem')
act(() => {
const onChange = categoryItemInput.prop('onChange')
onChange({ currentTarget: { value: expectedIncident.categoryItem } })
})
const descriptionInput = wrapper.findWhere((w) => w.prop('name') === 'description')
act(() => {
const onChange = descriptionInput.prop('onChange')
onChange({ currentTarget: { value: expectedIncident.description } })
})
wrapper.update()
const saveButton = wrapper.find(Button).at(0)
await act(async () => {
const onClick = saveButton.prop('onClick') as any
onClick()
})
expect(IncidentRepository.save).toHaveBeenCalledTimes(1)
expect(IncidentRepository.save).toHaveBeenCalledWith(
expect.objectContaining(expectedIncident),
)
expect(history.location.pathname).toEqual(`/incidents/someId`)
})
it('should display errors if validation fails', async () => {
const error = {
name: 'incident error',
message: 'something went wrong',
date: 'some date error',
department: 'some department error',
category: 'some category error',
categoryItem: 'some category item error',
description: 'some description error',
}
jest.spyOn(validationUtil, 'default').mockReturnValue(error)
const wrapper = await setup([Permissions.ReportIncident])
const saveButton = wrapper.find(Button).at(0)
await act(async () => {
const onClick = saveButton.prop('onClick') as any
await onClick()
})
wrapper.update()
const dateInput = wrapper.findWhere((w) => w.prop('name') === 'dateOfIncident')
const departmentInput = wrapper.findWhere((w) => w.prop('name') === 'department')
const categoryInput = wrapper.findWhere((w) => w.prop('name') === 'category')
const categoryItemInput = wrapper.findWhere((w) => w.prop('name') === 'categoryItem')
const descriptionInput = wrapper.findWhere((w) => w.prop('name') === 'description')
expect(dateInput.prop('isInvalid')).toBeTruthy()
expect(dateInput.prop('feedback')).toEqual(error.date)
expect(departmentInput.prop('isInvalid')).toBeTruthy()
expect(departmentInput.prop('feedback')).toEqual(error.department)
expect(categoryInput.prop('isInvalid')).toBeTruthy()
expect(categoryInput.prop('feedback')).toEqual(error.category)
expect(categoryItemInput.prop('isInvalid')).toBeTruthy()
expect(categoryItemInput.prop('feedback')).toEqual(error.categoryItem)
expect(descriptionInput.prop('isInvalid')).toBeTruthy()
expect(descriptionInput.prop('feedback')).toEqual(error.description)
})
})
describe('on cancel', () => {
it('should navigate to /incidents', async () => {
const wrapper = await setup([Permissions.ReportIncident])
const cancelButton = wrapper.find(Button).at(1)
act(() => {
const onClick = cancelButton.prop('onClick') as any
onClick()
})
expect(history.location.pathname).toEqual('/incidents')
})
})
})