Newer
Older
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 challengeEntityData from 'db/challengeEntity.json'
import duelEntityData from 'db/duelEntity.json'
import quizEntityData from 'db/quizEntity.json'
import explorationEntityData from 'db/explorationEntity.json'
import { hashFile } from 'utils/hash'
import { getActualAnalysisDate } from 'utils/date'
import { FluidType } from 'enum/fluid.enum'
import { ecogesturesData } from '../../tests/__mocks__/ecogesturesData.mock'
import { profileData } from '../../tests/__mocks__/profile.mock'
import { fluidStatusData } from '../../tests/__mocks__/fluidStatusData.mock'
import { userChallengeData } from '../../tests/__mocks__/userChallengeData.mock'
import { graphData } from '../../tests/__mocks__/datachartData.mock'
import { allChallengeEntityData } from '../../tests/__mocks__/challengeEntity.mock'
import { allDuelEntity } from '../../tests/__mocks__/duelData.mock'
import { allQuizEntities } from '../../tests/__mocks__/quizData.mock'
import { allExplorationEntities } from '../../tests/__mocks__/explorationData.mock'
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
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 mockGetAllChallengeEntities = jest.fn()
const mockDeleteAllChallengeEntities = jest.fn()
const mockBuildUserChallengeList = jest.fn()
const mockGetUserChallengeDataload = jest.fn()
const mockUserChallengeUpdateFlag = jest.fn()
jest.mock('./challenge.service', () => {
getAllChallengeEntities: mockGetAllChallengeEntities,
deleteAllChallengeEntities: mockDeleteAllChallengeEntities,
buildUserChallengeList: mockBuildUserChallengeList,
getUserChallengeDataload: mockGetUserChallengeDataload,
updateUserChallenge: mockUserChallengeUpdateFlag,
const mockGetAllDuelEntities = jest.fn()
const mockDeleteAllDuelEntities = jest.fn()
jest.mock('./duel.service', () => {
getAllDuelEntities: mockGetAllDuelEntities,
deleteAllDuelEntities: mockDeleteAllDuelEntities,
const mockGetAllQuizEntities = jest.fn()
const mockDeleteAllQuizEntities = jest.fn()
jest.mock('./quiz.service', () => {
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
getAllQuizEntities: mockGetAllQuizEntities,
deleteAllQuizEntities: mockDeleteAllQuizEntities,
}
})
})
const mockGetAllExplorationEntities = jest.fn()
const mockDeleteAllExplorationEntities = jest.fn()
jest.mock('./exploration.service', () => {
return jest.fn(() => {
return {
getAllExplorationEntities: mockGetAllExplorationEntities,
deleteAllExplorationEntities: mockDeleteAllExplorationEntities,
}
})
})
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 mockIsConsentVersionUpToDate = jest.fn()
const mockIsLastTermValidated = jest.fn()
jest.mock('./terms.service', () => {
return jest.fn(() => {
return {
isConsentVersionUpToDate: mockIsConsentVersionUpToDate,
isLastTermValidated: mockIsLastTermValidated,
}
})
})
describe('Initialization service', () => {
const initializationService = new InitializationService(mockClient)
beforeEach(() => {
mockClient.query.mockClear()
mockClient.create.mockClear()
})
beforeEach(() => {
mockCreateIndexKonnector.mockClear()
mockCreateIndexAccount.mockClear()
})
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
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', () => {
beforeEach(() => {
mockGetProfile.mockClear()
mockUpdateProfile.mockClear()
})
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
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', () => {
beforeEach(() => {
mockGetAllEcogestures.mockClear()
mockDeleteAllEcogestures.mockClear()
it('shoud return hash when ecogestures hash is already up to date', async () => {
mockGetAllEcogestures.mockResolvedValueOnce(ecogestureData)
const hash = hashFile(ecogestureData)
await expect(initializationService.initEcogesture(hash)).resolves.toEqual(
hash
)
})
it('shoud return hash 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 hash = hashFile(ecogestureData)
await expect(initializationService.initEcogesture(hash)).resolves.toEqual(
hash
)
})
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 creation failed', async () => {
mockGetAllEcogestures.mockResolvedValueOnce(null)
mockClient.create.mockRejectedValue(new Error())
await expect(
initializationService.initEcogesture(hashFile(ecogestureData))
).rejects.toThrow(new Error())
})
it('shoud return hash 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)
await expect(initializationService.initEcogesture('')).resolves.toEqual(
hashFile(ecogestureData)
)
})
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 ecogestures creation failed', async () => {
mockGetAllEcogestures
.mockResolvedValueOnce(ecogestureData)
.mockResolvedValueOnce(ecogestureData)
mockDeleteAllEcogestures.mockResolvedValue(true)
mockClient.create.mockRejectedValueOnce(new Error())
expect(initializationService.initEcogesture('')).rejects.toThrow(
new Error()
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
describe('initChallengeEntity method', () => {
beforeEach(() => {
mockGetAllChallengeEntities.mockClear()
mockDeleteAllChallengeEntities.mockClear()
})
it('shoud return hash when challenges hash is already up to date', async () => {
mockGetAllChallengeEntities.mockResolvedValueOnce(challengeEntityData)
const hash = hashFile(challengeEntityData)
await expect(
initializationService.initChallengeEntity(hash)
).resolves.toEqual(hash)
})
it('shoud return hash when challenge entities are created', async () => {
mockGetAllChallengeEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(challengeEntityData)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
const hash = hashFile(challengeEntityData)
await expect(
initializationService.initChallengeEntity(hash)
).resolves.toEqual(hash)
})
it('shoud throw an error when challenge entities should be created and created challenge entities number does not match', async () => {
mockGetAllChallengeEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(allChallengeEntityData)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initChallengeEntity(hashFile(challengeEntityData))
).rejects.toThrow(
new Error(
'initChallengeEntity: Created challenge entities does not match'
)
)
})
it('shoud throw an error when challenge entities should be created and creation failed', async () => {
mockGetAllChallengeEntities.mockResolvedValueOnce(null)
mockClient.create.mockRejectedValue(new Error())
await expect(
initializationService.initChallengeEntity(hashFile(challengeEntityData))
).rejects.toThrow(new Error())
})
it('shoud return hash when challenge entities are updated', async () => {
mockGetAllChallengeEntities
.mockResolvedValueOnce(challengeEntityData)
.mockResolvedValueOnce(challengeEntityData)
mockDeleteAllChallengeEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initChallengeEntity('')
).resolves.toEqual(hashFile(challengeEntityData))
})
it('shoud throw an error when challenge entities should be updated and created challenge entities number does not match', async () => {
mockGetAllChallengeEntities
.mockResolvedValueOnce(challengeEntityData)
.mockResolvedValueOnce(allChallengeEntityData)
mockDeleteAllChallengeEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initChallengeEntity('')
).rejects.toThrow(
new Error(
'initChallengeEntity: Created challenge entities does not match'
)
)
})
it('shoud throw an error when challenge entities should be updated and challenge entities creation failed', async () => {
mockGetAllChallengeEntities
.mockResolvedValueOnce(challengeEntityData)
.mockResolvedValueOnce(challengeEntityData)
mockDeleteAllChallengeEntities.mockResolvedValue(true)
mockClient.create.mockRejectedValueOnce(new Error())
expect(initializationService.initChallengeEntity('')).rejects.toThrow(
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
describe('initDuelEntity method', () => {
beforeEach(() => {
mockGetAllDuelEntities.mockClear()
mockDeleteAllDuelEntities.mockClear()
})
it('shoud return hash when duel hash is already up to date', async () => {
mockGetAllDuelEntities.mockResolvedValueOnce(duelEntityData)
const hash = hashFile(duelEntityData)
await expect(initializationService.initDuelEntity(hash)).resolves.toEqual(
hash
)
})
it('shoud return hash when duel entities are created', async () => {
mockGetAllDuelEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(duelEntityData)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
const hash = hashFile(duelEntityData)
await expect(initializationService.initDuelEntity(hash)).resolves.toEqual(
hash
)
})
it('shoud throw an error when duel entities should be created and created duel entities number does not match', async () => {
mockGetAllDuelEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(allDuelEntity)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
mockClient.create.mockResolvedValue(mockQueryResult)
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
initializationService.initDuelEntity(hashFile(duelEntityData))
).rejects.toThrow(
new Error('initDuelEntity: Created duel entities does not match')
)
})
it('shoud throw an error when duel entities should be created and creation failed', async () => {
mockGetAllDuelEntities.mockResolvedValueOnce(null)
mockClient.create.mockRejectedValue(new Error())
await expect(
initializationService.initDuelEntity(hashFile(duelEntityData))
).rejects.toThrow(new Error())
})
it('shoud return hash when duel entities are updated', async () => {
mockGetAllDuelEntities
.mockResolvedValueOnce(duelEntityData)
.mockResolvedValueOnce(duelEntityData)
mockDeleteAllDuelEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(initializationService.initDuelEntity('')).resolves.toEqual(
hashFile(duelEntityData)
)
})
it('shoud throw an error when duel entities should be updated and created duel entities number does not match', async () => {
mockGetAllDuelEntities
.mockResolvedValueOnce(duelEntityData)
.mockResolvedValueOnce(allDuelEntity)
mockDeleteAllDuelEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(initializationService.initDuelEntity('')).rejects.toThrow(
new Error('initDuelEntity: Created duel entities does not match')
)
it('shoud throw an error when duel entities should be updated and duel entities creation failed', async () => {
mockGetAllDuelEntities
.mockResolvedValueOnce(duelEntityData)
.mockResolvedValueOnce(duelEntityData)
mockDeleteAllDuelEntities.mockResolvedValue(true)
mockClient.create.mockRejectedValueOnce(new Error())
expect(initializationService.initDuelEntity('')).rejects.toThrow(
new Error()
)
})
})
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
describe('initQuizEntity method', () => {
beforeEach(() => {
mockGetAllQuizEntities.mockClear()
mockDeleteAllQuizEntities.mockClear()
})
it('shoud return hash when quiz hash is already up to date', async () => {
mockGetAllQuizEntities.mockResolvedValueOnce(quizEntityData)
const hash = hashFile(quizEntityData)
await expect(initializationService.initQuizEntity(hash)).resolves.toEqual(
hash
)
})
it('shoud return hash when quiz entities are created', async () => {
mockGetAllQuizEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(quizEntityData)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
const hash = hashFile(quizEntityData)
await expect(initializationService.initQuizEntity(hash)).resolves.toEqual(
hash
)
})
it('shoud throw an error when quiz entities should be created and created quiz entities number does not match', async () => {
mockGetAllQuizEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(allQuizEntities)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initQuizEntity(hashFile(quizEntityData))
).rejects.toThrow(
new Error('initQuizEntity: Created quiz entities does not match')
)
})
it('shoud throw an error when quiz entities should be created and creation failed', async () => {
mockGetAllQuizEntities.mockResolvedValueOnce(null)
mockClient.create.mockRejectedValue(new Error())
await expect(
initializationService.initQuizEntity(hashFile(quizEntityData))
).rejects.toThrow(new Error())
})
it('shoud return hash when quiz entities are updated', async () => {
mockGetAllQuizEntities
.mockResolvedValueOnce(quizEntityData)
.mockResolvedValueOnce(quizEntityData)
mockDeleteAllQuizEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(initializationService.initQuizEntity('')).resolves.toEqual(
hashFile(quizEntityData)
)
})
it('shoud throw an error when quiz entities should be updated and created quiz entities number does not match', async () => {
mockGetAllQuizEntities
.mockResolvedValueOnce(quizEntityData)
.mockResolvedValueOnce(allQuizEntities)
mockDeleteAllQuizEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(initializationService.initQuizEntity('')).rejects.toThrow(
new Error('initQuizEntity: Created quiz entities does not match')
)
})
it('shoud throw an error when quiz entities should be updated and quiz entities creation failed', async () => {
mockGetAllQuizEntities
.mockResolvedValueOnce(quizEntityData)
.mockResolvedValueOnce(quizEntityData)
mockDeleteAllQuizEntities.mockResolvedValue(true)
mockClient.create.mockRejectedValueOnce(new Error())
expect(initializationService.initQuizEntity('')).rejects.toThrow(
new Error()
)
})
})
describe('initExplorationEntity method', () => {
beforeEach(() => {
mockGetAllExplorationEntities.mockClear()
mockDeleteAllExplorationEntities.mockClear()
})
it('shoud return hash when explorations hash is already up to date', async () => {
mockGetAllExplorationEntities.mockResolvedValueOnce(explorationEntityData)
const hash = hashFile(explorationEntityData)
await expect(
initializationService.initExplorationEntity(hash)
).resolves.toEqual(hash)
})
it('shoud return hash when exploration entities are created', async () => {
mockGetAllExplorationEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(explorationEntityData)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
const hash = hashFile(explorationEntityData)
await expect(
initializationService.initExplorationEntity(hash)
).resolves.toEqual(hash)
})
it('shoud throw an error when exploration entities should be created and created exploration entities number does not match', async () => {
mockGetAllExplorationEntities
.mockResolvedValueOnce(null)
.mockResolvedValueOnce(allExplorationEntities)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initExplorationEntity(
hashFile(explorationEntityData)
)
).rejects.toThrow(
new Error(
'initExplorationEntity: Created exploration entities does not match'
)
)
})
it('shoud throw an error when exploration entities should be created and creation failed', async () => {
mockGetAllExplorationEntities.mockResolvedValueOnce(null)
mockClient.create.mockRejectedValue(new Error())
await expect(
initializationService.initExplorationEntity(
hashFile(explorationEntityData)
)
).rejects.toThrow(new Error())
})
it('shoud return hash when exploration entities are updated', async () => {
mockGetAllExplorationEntities
.mockResolvedValueOnce(explorationEntityData)
.mockResolvedValueOnce(explorationEntityData)
mockDeleteAllExplorationEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initExplorationEntity('')
).resolves.toEqual(hashFile(explorationEntityData))
})
it('shoud throw an error when exploration entities should be updated and created exploration entities number does not match', async () => {
mockGetAllExplorationEntities
.mockResolvedValueOnce(explorationEntityData)
.mockResolvedValueOnce(allExplorationEntities)
mockDeleteAllExplorationEntities.mockResolvedValue(true)
const mockQueryResult: QueryResult<boolean> = {
data: true,
bookmark: '',
next: false,
skip: 0,
}
mockClient.create.mockResolvedValue(mockQueryResult)
await expect(
initializationService.initExplorationEntity('')
).rejects.toThrow(
new Error(
'initExplorationEntity: Created exploration entities does not match'
)
)
})
it('shoud throw an error when exploration entities should be updated and exploration entities creation failed', async () => {
mockGetAllExplorationEntities
.mockResolvedValueOnce(explorationEntityData)
.mockResolvedValueOnce(explorationEntityData)
mockDeleteAllExplorationEntities.mockResolvedValue(true)
mockClient.create.mockRejectedValueOnce(new Error())
expect(initializationService.initExplorationEntity('')).rejects.toThrow(
new Error()
)
})
})
describe('initAnalysis method', () => {
it('should return monthlyAnalysisDate and haveSeenLastAnalysis when analysis is up to date', async () => {
const mockProfile = {
...profileData,
monthlyAnalysisDate: getActualAnalysisDate(),
}
await expect(
initializationService.initAnalysis(mockProfile)
).resolves.toEqual({
monthlyAnalysisDate: getActualAnalysisDate(),
haveSeenLastAnalysis: mockProfile.haveSeenLastAnalysis,
it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=true when analysis is not up to date and isFirstConnection', async () => {
const mockProfile = {
...profileData,
monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', {
zone: 'utc',
}),
}
await expect(
initializationService.initAnalysis(mockProfile)
).resolves.toEqual({
monthlyAnalysisDate: getActualAnalysisDate(),
haveSeenLastAnalysis: true,
})
it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=false when analysis is not up to date and isFirstConnection is false', async () => {
monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', {
zone: 'utc',
}),
}
await expect(
initializationService.initAnalysis(mockProfile)
).resolves.toEqual({
monthlyAnalysisDate: getActualAnalysisDate(),
haveSeenLastAnalysis: false,
})
})
})
describe('initFluidType method', () => {
beforeEach(() => {
mockGetKonnectorAccountStatus.mockClear()
})
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
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', () => {
beforeEach(() => {
mockGetFluidStatus.mockClear()
})
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', () => {
beforeEach(() => {
mockBuildUserChallengeList.mockClear()
})
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', () => {
beforeEach(() => {
mockGetUserChallengeDataload.mockClear()
mockUserChallengeUpdateFlag.mockClear()
})
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
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())
})
})
describe('initTerms method', () => {
it('shoud not show consent page', async () => {
mockIsConsentVersionUpToDate.mockResolvedValueOnce(true)
mockIsLastTermValidated.mockResolvedValueOnce(true)
await expect(initializationService.initConsent()).resolves.toEqual(true)
})
it('shoud show consent page', async () => {
mockIsConsentVersionUpToDate.mockResolvedValueOnce(false)
mockIsLastTermValidated.mockResolvedValueOnce(false)
await expect(initializationService.initConsent()).resolves.toEqual(false)
})
it('shoud throw an error when it fails to checks for consent', async () => {
mockIsConsentVersionUpToDate.mockRejectedValueOnce(new Error())
await expect(initializationService.initConsent()).rejects.toThrow(
new Error()
)
})
})