Newer
Older
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import { QueryResult } from 'cozy-client'
import { DateTime } from 'luxon'
import { UserChallenge } from 'models'
import InitializationService from './initialization.service'
import mockClient from '../../tests/__mocks__/client'
import { ecogesturesData } from '../../tests/__mocks__/ecogesturesData.mock'
import { profileData } from '../../tests/__mocks__/profile.mock'
import { fluidStatusData } from '../../tests/__mocks__/fluidStatusData.mock'
import ecogestureData from 'db/ecogestureData.json'
import { hashFile } from 'utils/hash'
import { getActualAnalysisDate } from 'utils/date'
import { FluidType } from 'enum/fluid.enum'
import { userChallengeData } from '../../tests/__mocks__/userChallengeData.mock'
import { graphData } from '../../tests/__mocks__/datachartData.mock'
const mockCreateIndexKonnector = jest.fn()
jest.mock('./konnector.service', () => {
return jest.fn(() => {
return {
createIndexKonnector: mockCreateIndexKonnector,
}
})
})
const mockCreateIndexAccount = jest.fn()
jest.mock('./account.service', () => {
return jest.fn(() => {
return {
createIndexAccount: mockCreateIndexAccount,
}
})
})
const mockGetProfile = jest.fn()
const mockUpdateProfile = jest.fn()
jest.mock('./profile.service', () => {
return jest.fn(() => {
return {
getProfile: mockGetProfile,
updateProfile: mockUpdateProfile,
}
})
})
const mockGetAllEcogestures = jest.fn()
const mockDeleteAllEcogestures = jest.fn()
jest.mock('./ecogesture.service', () => {
return jest.fn(() => {
return {
getAllEcogestures: mockGetAllEcogestures,
deleteAllEcogestures: mockDeleteAllEcogestures,
}
})
})
const mockGetKonnectorAccountStatus = jest.fn()
jest.mock('./konnectorStatus.service', () => {
return jest.fn(() => {
return {
getKonnectorAccountStatus: mockGetKonnectorAccountStatus,
}
})
})
const mockGetFluidStatus = jest.fn()
jest.mock('./fluid.service', () => {
return jest.fn(() => {
return {
getFluidStatus: mockGetFluidStatus,
}
})
})
const mockBuildUserChallengeList = jest.fn()
const mockGetUserChallengeDataload = jest.fn()
const mockUserChallengeUpdateFlag = jest.fn()
jest.mock('./challenge.service', () => {
return jest.fn(() => {
return {
buildUserChallengeList: mockBuildUserChallengeList,
getUserChallengeDataload: mockGetUserChallengeDataload,
updateUserChallenge: mockUserChallengeUpdateFlag,
}
})
})
describe('Initialization service', () => {
const initializationService = new InitializationService(mockClient)
describe('initIndex method', () => {
it('shoud return true when all indexes created', async () => {
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.query.mockResolvedValueOnce(mockQueryResult)
mockCreateIndexKonnector.mockResolvedValueOnce(mockQueryResult)
mockCreateIndexAccount.mockResolvedValueOnce(mockQueryResult)
await expect(initializationService.initIndex()).resolves.toBe(true)
})
it('shoud throw error when an index is not created', async () => {
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.query.mockResolvedValueOnce(mockQueryResult)
mockCreateIndexKonnector.mockRejectedValueOnce(new Error())
mockCreateIndexAccount.mockResolvedValueOnce(mockQueryResult)
await expect(initializationService.initIndex()).rejects.toThrow(
new Error()
)
})
})
describe('initProfile method', () => {
it('shoud return the profil when existing', async () => {
mockGetProfile.mockResolvedValueOnce(profileData)
mockUpdateProfile.mockResolvedValueOnce(profileData)
await expect(initializationService.initProfile()).resolves.toEqual(
profileData
)
})
it('shoud create and return the profil when no existing', async () => {
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockGetProfile.mockResolvedValueOnce(null)
mockClient.create.mockResolvedValueOnce(mockQueryResult)
mockUpdateProfile.mockResolvedValueOnce(profileData)
await expect(initializationService.initProfile()).resolves.toEqual(
profileData
)
})
it('shoud throw error when the profile is not created', async () => {
const mockQueryResult: QueryResult<null> = {
data: null,
bookmark: '',
next: false,
skip: 0,
}
mockGetProfile.mockResolvedValueOnce(null)
mockClient.create.mockResolvedValueOnce(mockQueryResult)
await expect(initializationService.initProfile()).rejects.toThrow(
new Error('initProfile: Profile not created')
)
})
it('shoud throw error when the profile could not be fetched', () => {
mockGetProfile.mockRejectedValueOnce(new Error())
expect(initializationService.initProfile()).rejects.toEqual(new Error())
})
it('shoud throw error when the profile failed to be created', () => {
mockGetProfile.mockResolvedValueOnce(null)
mockClient.create.mockRejectedValueOnce(new Error())
expect(initializationService.initProfile()).rejects.toEqual(new Error())
})
})
describe('initEcoGesture method', () => {
it('shoud return true and profile = null when ecogestures hash is already up to date', async () => {
mockGetAllEcogestures.mockResolvedValue(ecogestureData)
await expect(
initializationService.initEcogesture(hashFile(ecogestureData))
).resolves.toEqual({ result: true, profile: null })
})
it('shoud return true and ecogestures when ecogestures are created', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(ecogestureData)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
const mockProfile = {
...profileData,
ecogestureHash: hashFile(ecogestureData),
}
mockUpdateProfile.mockResolvedValueOnce(mockProfile)
await expect(
initializationService.initEcogesture(hashFile(ecogestureData))
).resolves.toEqual({ result: true, profile: mockProfile })
})
it('shoud throw an error when ecogestures should be created and created ecogestures number does not match', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(ecogesturesData)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initEcogesture(hashFile(ecogestureData))
).rejects.toThrow(
new Error(
'initEcogesture: Created ecogesture type entities does not match'
)
)
})
it('shoud throw an error when ecogestures should be created and updateProfile failed', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(ecogestureData)
mockUpdateProfile.mockResolvedValueOnce(null)
await expect(
initializationService.initEcogesture(hashFile(ecogestureData))
).rejects.toThrow(new Error('initEcogesture: Profile not updated'))
})
it('shoud throw an error when ecogestures should be created and challenge creation failed', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(ecogestureData)
mockUpdateProfile.mockRejectedValueOnce(new Error())
await expect(
initializationService.initEcogesture(hashFile(ecogestureData))
).rejects.toThrow(new Error())
})
it('shoud return true and profil when ecogestures are updated', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(ecogestureData)
.mockResolvedValueOnce(ecogestureData)
mockDeleteAllEcogestures.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
const mockProfile = {
...profileData,
ecogestureHash: hashFile(ecogestureData),
}
mockUpdateProfile.mockResolvedValueOnce(mockProfile)
await expect(initializationService.initEcogesture('')).resolves.toEqual({
result: true,
profile: mockProfile,
})
})
it('shoud throw an error when ecogestures should be updated and created ecogestures number does not match', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(ecogestureData)
.mockResolvedValueOnce(ecogesturesData)
mockDeleteAllEcogestures.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(initializationService.initEcogesture('')).rejects.toThrow(
new Error(
'initEcogesture: Created ecogesture type entities does not match'
)
)
})
it('shoud throw an error when ecogestures should be updated and updateProfile failed', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(ecogestureData)
.mockResolvedValueOnce(ecogestureData)
mockDeleteAllEcogestures.mockResolvedValue(true)
mockUpdateProfile.mockResolvedValueOnce(null)
await expect(initializationService.initEcogesture('')).rejects.toThrow(
new Error('initEcogesture: Profile not updated')
)
})
it('shoud throw an error when ecogestures should be updated and ecogestures creation failed', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(ecogestureData)
.mockResolvedValueOnce(ecogestureData)
mockDeleteAllEcogestures.mockResolvedValue(true)
mockUpdateProfile.mockRejectedValueOnce(new Error())
expect(initializationService.initEcogesture('')).rejects.toThrow(
new Error()
)
})
})
describe('initAnalysis method', () => {
it('should return true and profile when analysis is up to date', async () => {
const mockProfile = {
...profileData,
monthlyAnalysisDate: getActualAnalysisDate(),
}
await expect(
initializationService.initAnalysis(mockProfile)
).resolves.toEqual({
result: true,
profile: mockProfile,
})
})
it('should return true and updated profile when analysis is not up to date', async () => {
const mockProfile = {
...profileData,
monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', {
zone: 'utc',
}),
haveSeenLastAnalysis: true,
}
const updatedProfile = {
...profileData,
monthlyAnalysisDate: getActualAnalysisDate(),
haveSeenLastAnalysis: false,
}
mockUpdateProfile.mockResolvedValueOnce(updatedProfile)
await expect(
initializationService.initAnalysis(mockProfile)
).resolves.toEqual({
result: true,
profile: updatedProfile,
})
})
it('should throw error when analysis is not up to date and profile is not updated', async () => {
const mockProfile = {
...profileData,
monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', {
zone: 'utc',
}),
}
mockUpdateProfile.mockResolvedValueOnce(null)
await expect(
initializationService.initAnalysis(mockProfile)
).rejects.toThrow(new Error('initAnalysis: Profile not updated'))
})
it('should throw error when analysis is not up to date and update profile failed', async () => {
const mockProfile = {
...profileData,
monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', {
zone: 'utc',
}),
}
mockUpdateProfile.mockRejectedValueOnce(new Error())
await expect(
initializationService.initAnalysis(mockProfile)
).rejects.toThrow(new Error())
})
})
describe('initFluidType method', () => {
it('shoud return all fluid types', async () => {
mockGetKonnectorAccountStatus.mockResolvedValueOnce([
FluidType.ELECTRICITY,
FluidType.WATER,
FluidType.GAS,
])
await expect(initializationService.initFluidTypes()).resolves.toEqual([
FluidType.ELECTRICITY,
FluidType.WATER,
FluidType.GAS,
])
})
it('shoud throw an error when null is retrieved as fluid types', async () => {
mockGetKonnectorAccountStatus.mockResolvedValueOnce(null)
await expect(initializationService.initFluidTypes()).rejects.toThrow(
new Error('initFluidTypes: FluidTypes not found')
)
})
it('shoud throw an error when it fails to retrieve the fluid types', async () => {
mockGetKonnectorAccountStatus.mockRejectedValueOnce(new Error())
await expect(initializationService.initFluidTypes()).rejects.toThrow(
new Error()
)
})
})
describe('initFluidStatus method', () => {
it('shoud return all fluids type', async () => {
mockGetFluidStatus.mockResolvedValueOnce(fluidStatusData)
await expect(initializationService.initFluidStatus()).resolves.toEqual(
fluidStatusData
)
})
it('shoud throw an error when null is retrieved as status', async () => {
mockGetFluidStatus.mockResolvedValueOnce(null)
await expect(initializationService.initFluidStatus()).rejects.toThrow(
new Error('initFluidStatus: fluidStatus not found')
)
})
it('shoud throw an error when it fails to retrieve the status', async () => {
mockGetFluidStatus.mockRejectedValueOnce(new Error())
await expect(initializationService.initFluidStatus()).rejects.toThrow(
new Error()
)
})
})
describe('initUserChallenges method', () => {
it('shoud return all userChallenges', async () => {
mockBuildUserChallengeList.mockResolvedValueOnce(userChallengeData)
await expect(initializationService.initUserChallenges()).resolves.toEqual(
userChallengeData
)
})
it('shoud throw an error when null is retrieved as status', async () => {
mockBuildUserChallengeList.mockResolvedValueOnce(null)
await expect(initializationService.initUserChallenges()).rejects.toThrow(
new Error('initUserChallenges: userChallengeList not found')
)
})
it('shoud throw an error when it fails to retrieve the status', async () => {
mockBuildUserChallengeList.mockRejectedValueOnce(new Error())
await expect(initializationService.initUserChallenges()).rejects.toThrow(
new Error()
)
})
})
describe('initDuelProgress method', () => {
it('shoud return updatedUserChallenge and dataload ', async () => {
mockGetUserChallengeDataload.mockResolvedValueOnce(graphData.actualData)
const expectedUpdatedUserChallenge: UserChallenge = {
...userChallengeData[0],
duel: {
...userChallengeData[0].duel,
userConsumption: 130.83585,
},
}
mockUserChallengeUpdateFlag.mockResolvedValueOnce(
expectedUpdatedUserChallenge
)
const expectedResult = {
updatedUserChallenge: expectedUpdatedUserChallenge,
dataloads: graphData.actualData,
}
await expect(
initializationService.initDuelProgress(userChallengeData[0])
).resolves.toEqual(expectedResult)
})
it('shoud throw an error when it fails to retrieve the status', async () => {
mockGetUserChallengeDataload.mockRejectedValueOnce(new Error())
await expect(
initializationService.initDuelProgress(userChallengeData[0])
).rejects.toThrow(new Error())
})
})
})