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
/
incident-slice.test.ts
204 lines (177 loc) · 7.06 KB
/
incident-slice.test.ts
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
import { AnyAction } from 'redux'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import shortid from 'shortid'
import { addDays } from 'date-fns'
import incident, {
reportIncidentStart,
reportIncidentSuccess,
reportIncidentError,
reportIncident,
fetchIncidentStart,
fetchIncidentSuccess,
fetchIncident,
} from '../../incidents/incident-slice'
import Incident from '../../model/Incident'
import { RootState } from '../../store'
import IncidentRepository from '../../clients/db/IncidentRepository'
import Permissions from '../../model/Permissions'
import User from '../../model/User'
const mockStore = createMockStore<RootState, any>([thunk])
describe('incident slice', () => {
describe('actions', () => {
it('should default the store correctly', () => {
const incidentStore = incident(undefined, {} as AnyAction)
expect(incidentStore.status).toEqual('loading')
expect(incidentStore.incident).toBeUndefined()
expect(incidentStore.error).toBeUndefined()
})
it('should handle the report incident start', () => {
const incidentStore = incident(undefined, reportIncidentStart)
expect(incidentStore.status).toEqual('loading')
})
it('should handle the report incident success', () => {
const expectedIncident = {
id: 'some id',
reportedOn: new Date().toISOString(),
reportedBy: 'some user id',
description: 'description',
date: new Date().toISOString(),
department: 'some department',
category: 'category',
categoryItem: 'categoryItem',
code: 'some code',
} as Incident
const incidentStore = incident(undefined, reportIncidentSuccess(expectedIncident))
expect(incidentStore.status).toEqual('completed')
expect(incidentStore.incident).toEqual(expectedIncident)
expect(incidentStore.error).toBeUndefined()
})
it('should handle the report incident error', () => {
const expectedError = {
date: 'some description error',
description: 'some description error',
}
const incidentStore = incident(undefined, reportIncidentError(expectedError))
expect(incidentStore.status).toEqual('error')
expect(incidentStore.error).toEqual(expectedError)
})
it('should handle fetch incident start', () => {
const incidentStore = incident(undefined, fetchIncidentStart())
expect(incidentStore.status).toEqual('loading')
})
it('should handle fetch incident success', () => {
const expectedIncident = {
id: '1234',
code: 'some code',
} as Incident
const incidentStore = incident(undefined, fetchIncidentSuccess(expectedIncident))
expect(incidentStore.status).toEqual('completed')
expect(incidentStore.incident).toEqual(expectedIncident)
})
})
describe('report incident', () => {
beforeEach(() => {
jest.restoreAllMocks()
})
it('should successfully create an incident', async () => {
const expectedDate = new Date()
const expectedShortId = '123456'
Date.now = jest.fn().mockReturnValue(expectedDate)
jest.spyOn(shortid, 'generate').mockReturnValue(expectedShortId)
const onSuccessSpy = jest.fn()
const newIncident = {
description: 'description',
date: expectedDate.toISOString(),
department: 'some department',
category: 'category',
categoryItem: 'categoryItem',
} as Incident
const expectedIncident = {
...newIncident,
code: `I-${expectedShortId}`,
reportedOn: expectedDate.toISOString(),
reportedBy: 'some user id',
status: 'reported',
} as Incident
jest.spyOn(IncidentRepository, 'save').mockResolvedValue(expectedIncident)
const store = mockStore({
user: {
user: { id: expectedIncident.reportedBy } as User,
permissions: [] as Permissions[],
},
} as any)
await store.dispatch(reportIncident(newIncident, onSuccessSpy))
expect(store.getActions()[0]).toEqual(reportIncidentStart())
expect(store.getActions()[1]).toEqual(reportIncidentSuccess(expectedIncident))
expect(IncidentRepository.save).toHaveBeenCalledWith(expectedIncident)
expect(onSuccessSpy).toHaveBeenLastCalledWith(expectedIncident)
})
it('should validate the required fields apart of an incident', async () => {
const onSuccessSpy = jest.fn()
const newIncident = {} as Incident
jest.spyOn(IncidentRepository, 'save')
const expectedError = {
date: 'incidents.reports.error.dateRequired',
department: 'incidents.reports.error.departmentRequired',
category: 'incidents.reports.error.categoryRequired',
categoryItem: 'incidents.reports.error.categoryItemRequired',
description: 'incidents.reports.error.descriptionRequired',
}
const store = mockStore({
user: {
user: { id: 'some id' } as User,
permissions: [] as Permissions[],
},
} as any)
await store.dispatch(reportIncident(newIncident, onSuccessSpy))
expect(store.getActions()[0]).toEqual(reportIncidentStart())
expect(store.getActions()[1]).toEqual(reportIncidentError(expectedError))
expect(IncidentRepository.save).not.toHaveBeenCalled()
expect(onSuccessSpy).not.toHaveBeenCalled()
})
it('should validate that the incident date is not a date in the future', async () => {
const onSuccessSpy = jest.fn()
const newIncident = {
description: 'description',
date: addDays(new Date(), 4).toISOString(),
department: 'some department',
category: 'category',
categoryItem: 'categoryItem',
} as Incident
jest.spyOn(IncidentRepository, 'save')
const expectedError = {
date: 'incidents.reports.error.dateMustBeInThePast',
}
const store = mockStore({
user: {
user: { id: 'some id' } as User,
permissions: [] as Permissions[],
},
} as any)
await store.dispatch(reportIncident(newIncident, onSuccessSpy))
expect(store.getActions()[0]).toEqual(reportIncidentStart())
expect(store.getActions()[1]).toEqual(reportIncidentError(expectedError))
expect(IncidentRepository.save).not.toHaveBeenCalled()
expect(onSuccessSpy).not.toHaveBeenCalled()
})
})
describe('fetch incident', () => {
it('should fetch the incident', async () => {
const expectedIncident = {
id: '123',
description: 'description',
date: addDays(new Date(), 4).toISOString(),
department: 'some department',
category: 'category',
categoryItem: 'categoryItem',
} as Incident
jest.spyOn(IncidentRepository, 'find').mockResolvedValue(expectedIncident)
const store = mockStore()
await store.dispatch(fetchIncident(expectedIncident.id))
expect(store.getActions()[0]).toEqual(fetchIncidentStart())
expect(IncidentRepository.find).toHaveBeenCalledWith(expectedIncident.id)
expect(store.getActions()[1]).toEqual(fetchIncidentSuccess(expectedIncident))
})
})
})