Skip to content
Snippets Groups Projects
initialization.service.spec.ts 29.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Yoan VALLET's avatar
    Yoan VALLET committed
    import { QueryResult } from 'cozy-client'
    
    import challengeEntityData from 'db/challengeEntity.json'
    import duelEntityData from 'db/duelEntity.json'
    import explorationEntityData from 'db/explorationEntity.json'
    
    import quizEntityData from 'db/quizEntity.json'
    
    import { FluidType } from 'enums'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { DateTime } from 'luxon'
    
    import { Profile, Trigger, UserChallenge } from 'models'
    
    import { allChallengeEntityData } from 'tests/__mocks__/challengeEntity.mock'
    import { graphData } from 'tests/__mocks__/chartData.mock'
    
    import mockClient from 'tests/__mocks__/client.mock'
    
    import { allDuelEntity } from 'tests/__mocks__/duelData.mock'
    import { allExplorationEntities } from 'tests/__mocks__/explorationData.mock'
    import { fluidStatusData } from 'tests/__mocks__/fluidStatusData.mock'
    import { allQuizEntities } from 'tests/__mocks__/quizData.mock'
    import { mockProfileState } from 'tests/__mocks__/store'
    
    import {
      mockOutdatedTerm,
      mockUpToDateTerm,
    
    } from 'tests/__mocks__/termsData.mock'
    import { userChallengeData } from 'tests/__mocks__/userChallengeData.mock'
    import { getActualAnalysisDate } from 'utils/date'
    import { hashFile } from 'utils/hash'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import InitializationService from './initialization.service'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    const mockCreateIndexKonnector = jest.fn()
    jest.mock('./konnector.service', () => {
    
      return jest.fn(() => ({
        createIndexKonnector: mockCreateIndexKonnector,
      }))
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })
    
    const mockCreateIndexAccount = jest.fn()
    jest.mock('./account.service', () => {
    
      return jest.fn(() => ({
        createIndexAccount: mockCreateIndexAccount,
      }))
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })
    
    const mockGetProfile = jest.fn()
    const mockUpdateProfile = jest.fn()
    jest.mock('./profile.service', () => {
    
      return jest.fn(() => ({
        getProfile: mockGetProfile,
        updateProfile: mockUpdateProfile,
      }))
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })
    
    
    const mockGetAllChallengeEntities = jest.fn()
    const mockDeleteAllChallengeEntities = jest.fn()
    const mockBuildUserChallengeList = jest.fn()
    const mockGetUserChallengeDataload = jest.fn()
    const mockUserChallengeUpdateFlag = jest.fn()
    
    const mockInitChallengeDuelProgress = jest.fn()
    
    jest.mock('./challenge.service', () => {
    
      return jest.fn(() => ({
        getAllChallengeEntities: mockGetAllChallengeEntities,
        deleteAllChallengeEntities: mockDeleteAllChallengeEntities,
        buildUserChallengeList: mockBuildUserChallengeList,
        getUserChallengeDataload: mockGetUserChallengeDataload,
        updateUserChallenge: mockUserChallengeUpdateFlag,
        initChallengeDuelProgress: mockInitChallengeDuelProgress,
      }))
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })
    
    
    const mockGetAllPrices = jest.fn()
    const mockDeleteAllFluidsPrices = jest.fn()
    jest.mock('./fluidsPrices.service', () => {
    
      return jest.fn(() => ({
        getAllPrices: mockGetAllPrices,
        deleteAllFluidsPrices: mockDeleteAllFluidsPrices,
      }))
    
    const mockGetAllDuelEntities = jest.fn()
    const mockDeleteAllDuelEntities = jest.fn()
    jest.mock('./duel.service', () => {
    
      return jest.fn(() => ({
        getAllDuelEntities: mockGetAllDuelEntities,
        deleteAllDuelEntities: mockDeleteAllDuelEntities,
      }))
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })
    
    
    const mockGetAllQuizEntities = jest.fn()
    const mockDeleteAllQuizEntities = jest.fn()
    jest.mock('./quiz.service', () => {
    
      return jest.fn(() => ({
        getAllQuizEntities: mockGetAllQuizEntities,
        deleteAllQuizEntities: mockDeleteAllQuizEntities,
      }))
    
    })
    
    const mockGetAllExplorationEntities = jest.fn()
    const mockDeleteAllExplorationEntities = jest.fn()
    jest.mock('./exploration.service', () => {
    
      return jest.fn(() => ({
        getAllExplorationEntities: mockGetAllExplorationEntities,
        deleteAllExplorationEntities: mockDeleteAllExplorationEntities,
      }))
    
    })
    
    const mockGetKonnectorAccountStatus = jest.fn()
    jest.mock('./konnectorStatus.service', () => {
    
      return jest.fn(() => ({
        getKonnectorAccountStatus: mockGetKonnectorAccountStatus,
      }))
    
    })
    
    const mockGetFluidStatus = jest.fn()
    jest.mock('./fluid.service', () => {
    
      return jest.fn(() => ({
        getFluidStatus: mockGetFluidStatus,
      }))
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })
    
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
    const mockIsConsentVersionUpToDate = jest.fn()
    const mockIsLastTermValidated = jest.fn()
    
    const mockGetTermsVersionType = jest.fn()
    const mockGetLastTerm = jest.fn()
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
    jest.mock('./terms.service', () => {
    
      return jest.fn(() => ({
        isConsentVersionUpToDate: mockIsConsentVersionUpToDate,
        isLastTermValidated: mockIsLastTermValidated,
        getTermsVersionType: mockGetTermsVersionType,
        getLastTerm: mockGetLastTerm,
      }))
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
    })
    
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    describe('Initialization service', () => {
    
      const initializationService = new InitializationService(
        mockClient,
        jest.fn(),
        jest.fn()
      )
    
      beforeEach(() => {
    
        jest.clearAllMocks()
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
      describe('initProfile method', () => {
    
        it('should return the profil when existing', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          mockGetProfile.mockResolvedValueOnce(mockProfileState)
          mockUpdateProfile.mockResolvedValueOnce(mockProfileState)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          await expect(initializationService.initProfile()).resolves.toEqual(
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            mockProfileState
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          )
        })
    
        it('should create and return the profil when no existing', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          const mockQueryResult: QueryResult<boolean> = {
            data: true,
            bookmark: '',
            next: false,
            skip: 0,
          }
          mockGetProfile.mockResolvedValueOnce(null)
          mockClient.create.mockResolvedValueOnce(mockQueryResult)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          mockUpdateProfile.mockResolvedValueOnce(mockProfileState)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          await expect(initializationService.initProfile()).resolves.toEqual(
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            mockProfileState
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          )
        })
    
        it('should throw error when the profile is not created', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          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('should throw error when the profile could not be fetched', () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetProfile.mockRejectedValueOnce(new Error())
          expect(initializationService.initProfile()).rejects.toEqual(new Error())
        })
    
        it('should throw error when the profile failed to be created', () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetProfile.mockResolvedValueOnce(null)
          mockClient.create.mockRejectedValueOnce(new Error())
          expect(initializationService.initProfile()).rejects.toEqual(new Error())
        })
      })
    
    
      describe('initFluidPrices method', () => {
    
        it('should launch fluidsPrices service', async () => {
          const mockQueryResult: QueryResult<Trigger[]> = {
            data: [
              {
                _id: 'triggerId',
                type: '',
                arguments: '',
                message: { account: '', konnector: '' },
                worker: '',
              },
            ],
            bookmark: '',
            next: false,
            skip: 0,
          }
          mockClient.query.mockResolvedValueOnce(mockQueryResult)
          await initializationService.initFluidPrices()
          expect(mockClient.getStackClient).toBeCalledTimes(1)
    
        it('should not launch fluidsPrices service because trigger is not found', async () => {
          const mockQueryResult: QueryResult<Trigger[]> = {
            data: [],
            bookmark: '',
            next: false,
            skip: 0,
          }
          mockClient.query.mockResolvedValueOnce(mockQueryResult)
          await initializationService.initFluidPrices()
          expect(mockClient.getStackClient).toBeCalledTimes(0)
    
      describe('initChallengeEntity method', () => {
    
        it('should 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('should 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('should 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('should 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('should 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('should 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('should 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(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            new Error()
          )
        })
      })
    
    
      describe('initDuelEntity method', () => {
    
        it('should 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('should 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('should 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,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          }
    
          mockClient.create.mockResolvedValue(mockQueryResult)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          await expect(
    
            initializationService.initDuelEntity(hashFile(duelEntityData))
          ).rejects.toThrow(
            new Error('initDuelEntity: Created duel entities does not match')
          )
        })
    
        it('should 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('should 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('should 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')
          )
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        })
    
        it('should 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()
          )
        })
      })
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    
      describe('initQuizEntity method', () => {
    
        it('should 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('should 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('should 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('should 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('should 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('should 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('should 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', () => {
    
        it('should 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('should 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('should 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('should 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('should 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,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          }
    
          mockClient.create.mockResolvedValue(mockQueryResult)
          await expect(
            initializationService.initExplorationEntity('')
          ).resolves.toEqual(hashFile(explorationEntityData))
        })
    
        it('should 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('should 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 () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          const profile: Profile = {
            ...mockProfileState,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            monthlyAnalysisDate: getActualAnalysisDate(),
          }
          await expect(
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            initializationService.initAnalysis(profile)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          ).resolves.toEqual({
    
            monthlyAnalysisDate: getActualAnalysisDate(),
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            haveSeenLastAnalysis: profile.haveSeenLastAnalysis,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          })
        })
    
        it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=true when analysis is not up to date and isFirstConnection', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          const profile: Profile = {
            ...mockProfileState,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', {
              zone: 'utc',
            }),
    
            haveSeenLastAnalysis: true,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            isFirstConnection: true,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          }
          await expect(
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            initializationService.initAnalysis(profile)
    
          ).resolves.toEqual({
            monthlyAnalysisDate: getActualAnalysisDate(),
            haveSeenLastAnalysis: true,
          })
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        })
    
        it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=false when analysis is not up to date and isFirstConnection is false', async () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          const profile: Profile = {
            ...mockProfileState,
    
            isFirstConnection: false,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', {
              zone: 'utc',
            }),
    
            haveSeenLastAnalysis: true,
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          }
          await expect(
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
            initializationService.initAnalysis(profile)
    
          ).resolves.toEqual({
            monthlyAnalysisDate: getActualAnalysisDate(),
            haveSeenLastAnalysis: false,
          })
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        })
      })
    
      describe('initFluidType method', () => {
    
        it('should return all fluid types', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetKonnectorAccountStatus.mockResolvedValueOnce([
            FluidType.ELECTRICITY,
            FluidType.WATER,
            FluidType.GAS,
          ])
          await expect(initializationService.initFluidTypes()).resolves.toEqual([
            FluidType.ELECTRICITY,
            FluidType.WATER,
            FluidType.GAS,
          ])
        })
    
        it('should throw an error when null is retrieved as fluid types', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetKonnectorAccountStatus.mockResolvedValueOnce(null)
          await expect(initializationService.initFluidTypes()).rejects.toThrow(
            new Error('initFluidTypes: FluidTypes not found')
          )
        })
    
        it('should throw an error when it fails to retrieve the fluid types', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetKonnectorAccountStatus.mockRejectedValueOnce(new Error())
          await expect(initializationService.initFluidTypes()).rejects.toThrow(
            new Error()
          )
        })
      })
    
      describe('initFluidStatus method', () => {
    
        it('should return  all fluids type', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetFluidStatus.mockResolvedValueOnce(fluidStatusData)
          await expect(initializationService.initFluidStatus()).resolves.toEqual(
            fluidStatusData
          )
        })
    
        it('should throw an error when null is retrieved as status', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetFluidStatus.mockResolvedValueOnce(null)
          await expect(initializationService.initFluidStatus()).rejects.toThrow(
            new Error('initFluidStatus: fluidStatus not found')
          )
        })
    
        it('should throw an error when it fails to retrieve the status', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockGetFluidStatus.mockRejectedValueOnce(new Error())
          await expect(initializationService.initFluidStatus()).rejects.toThrow(
            new Error()
          )
        })
      })
    
      describe('initUserChallenges method', () => {
    
        it('should return all userChallenges', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockBuildUserChallengeList.mockResolvedValueOnce(userChallengeData)
    
          await expect(
            initializationService.initUserChallenges([])
          ).resolves.toEqual(userChallengeData)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        })
    
        it('should throw an error when null is retrieved as status', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockBuildUserChallengeList.mockResolvedValueOnce(null)
    
          await expect(
            initializationService.initUserChallenges([])
          ).rejects.toThrow(
    
    Yoan VALLET's avatar
    Yoan VALLET committed
            new Error('initUserChallenges: userChallengeList not found')
          )
        })
    
        it('should throw an error when it fails to retrieve the status', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          mockBuildUserChallengeList.mockRejectedValueOnce(new Error())
    
          await expect(
            initializationService.initUserChallenges([])
          ).rejects.toThrow(new Error())
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        })
      })
    
      describe('initDuelProgress method', () => {
    
        it('should return updatedUserChallenge and dataload', async () => {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          const expectedUpdatedUserChallenge: UserChallenge = {
            ...userChallengeData[0],
            duel: {
              ...userChallengeData[0].duel,
              userConsumption: 130.83585,
            },
          }
          const expectedResult = {
            updatedUserChallenge: expectedUpdatedUserChallenge,
            dataloads: graphData.actualData,
          }
    
          mockInitChallengeDuelProgress.mockResolvedValueOnce(expectedResult)
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          await expect(
            initializationService.initDuelProgress(userChallengeData[0])
          ).resolves.toEqual(expectedResult)
        })
    
        it('should throw an error when it fails to retrieve the status', async () => {
    
          mockInitChallengeDuelProgress.mockRejectedValueOnce(new Error())
    
    Yoan VALLET's avatar
    Yoan VALLET committed
          await expect(
            initializationService.initDuelProgress(userChallengeData[0])
          ).rejects.toThrow(new Error())
        })
      })
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
      describe('initTerms method', () => {
    
        it('should not show consent page', async () => {
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
          mockIsConsentVersionUpToDate.mockResolvedValueOnce(true)
          mockIsLastTermValidated.mockResolvedValueOnce(true)
    
          mockGetLastTerm.mockResolvedValueOnce(mockUpToDateTerm)
          await expect(initializationService.initConsent()).resolves.toEqual({
            accepted: true,
            versionType: 'init',
          })
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
        })
    
        it('should show consent page', async () => {
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
          mockIsConsentVersionUpToDate.mockResolvedValueOnce(false)
          mockIsLastTermValidated.mockResolvedValueOnce(false)
    
          mockGetLastTerm.mockResolvedValueOnce(mockOutdatedTerm)
          await expect(initializationService.initConsent()).resolves.toEqual({
            accepted: false,
            versionType: 'major',
          })
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
        })
    
        it('should throw an error when it fails to checks for consent', async () => {
    
    Guilhem CARRON's avatar
     
    Guilhem CARRON committed
          mockIsConsentVersionUpToDate.mockRejectedValueOnce(new Error())
          await expect(initializationService.initConsent()).rejects.toThrow(
            new Error()
          )
        })
      })
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    })