import { TestBed } from "@angular/core/testing" ; import { UserSettingsService } from "./user-settings.service" ; import { UserSettingsModel , UserZonesModel , ZoneModel } from "@elevate/shared/models" ; import { userSettingsData } from "@elevate/shared/data" ; import * as _ from "lodash" ; import { ZoneDefinitionModel } from "../../models/zone-definition.model" ; import { SharedModule } from "../../shared.module" ; import { CoreModule } from "../../../core/core.module" ; describe ( "UserSettingsService" , ( ) => { let userSettingsService ; beforeEach ( ( done ) => { TestBed . configureTestingModule ( { imports : [ CoreModule , SharedModule ] } ) ; userSettingsService = TestBed . get ( UserSettingsService ) ; done ( ) ; } ) ; it ( "should be created" , ( done ) => { expect ( userSettingsService ) . toBeTruthy ( ) ; done ( ) ; } ) ; it ( "should fetch user settings" , ( done ) => { const expectedSettings = _ . cloneDeep ( userSettingsData ) ; const fetchDaoSpy = spyOn ( userSettingsService . userSettingsDao , "fetch" ) . and . returnValue ( Promise . resolve ( expectedSettings ) ) ; const promiseFetch = userSettingsService . fetch ( ) ; promiseFetch . then ( ( result ) => { expect ( result ) . not . toBeNull ( ) ; expect ( result ) . toEqual ( expectedSettings ) ; expect ( fetchDaoSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should save user setting property" , ( done ) => { const key = "displayAdvancedHrData" ; const displayAdvancedHrData = false ; const expectedSettings = _ . cloneDeep ( userSettingsData ) ; expectedSettings . displayAdvancedHrData = displayAdvancedHrData ; const savePropertyDaoSpy = spyOn ( userSettingsService . userSettingsDao , "upsertProperty" ) . and . returnValue ( Promise . resolve ( expectedSettings ) ) ; const promiseUpdate = userSettingsService . saveProperty < boolean > ( key , displayAdvancedHrData ) ; promiseUpdate . then ( ( result ) => { expect ( result ) . not . toBeNull ( ) ; expect ( result . displayAdvancedHrData ) . toEqual ( displayAdvancedHrData ) ; expect ( result ) . toEqual ( expectedSettings ) ; expect ( result ) . not . toEqual ( userSettingsData ) ; expect ( result . displayAdvancedHrData ) . not . toEqual ( userSettingsData . displayAdvancedHrData ) ; expect ( savePropertyDaoSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( savePropertyDaoSpy ) . toHaveBeenCalledWith ( key , displayAdvancedHrData ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should mark local storage to be clear" , ( done ) => { const expectedSettings = _ . cloneDeep ( userSettingsData ) ; expectedSettings . localStorageMustBeCleared = true ; const savePropertyDaoSpy = spyOn ( userSettingsService . userSettingsDao , "upsertProperty" ) . and . returnValue ( Promise . resolve ( expectedSettings ) ) ; const promiseClearLS = userSettingsService . clearLocalStorageOnNextLoad ( ) ; promiseClearLS . then ( ( ) => { expect ( savePropertyDaoSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( savePropertyDaoSpy ) . toHaveBeenCalledWith ( UserSettingsService . MARK_LOCAL_STORAGE_CLEAR , true ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should save user zone" , ( done ) => { const TO_BE_SAVED_ZONES = [ { from : 0 , to : 50 } , { from : 50 , to : 100 } , { from : 100 , to : 150 } , { from : 150 , to : 200 } , { from : 200 , to : 250 } , { from : 250 , to : 300 } , { from : 300 , to : 400 } , { from : 400 , to : 500 } ] ; const zoneDefinition = { name : "Cycling Speed" , value : "speed" , units : "KPH" , step : 0.1 , min : 0 , max : 9999 , customDisplay : null } ; const settings = _ . cloneDeep ( userSettingsData ) ; const serializedZones = UserZonesModel . serialize ( TO_BE_SAVED_ZONES ) ; settings . zones . speed = serializedZones ; const upsertNestedPropertyDaoSpy = spyOn ( userSettingsService . userSettingsDao , "upsertProperty" ) . and . returnValue ( Promise . resolve ( settings ) ) ; const promiseUpdateZones = userSettingsService . saveZones ( zoneDefinition , TO_BE_SAVED_ZONES ) ; promiseUpdateZones . then ( ( savedZones ) => { expect ( savedZones ) . not . toBeNull ( ) ; expect ( savedZones ) . toEqual ( TO_BE_SAVED_ZONES ) ; expect ( upsertNestedPropertyDaoSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( upsertNestedPropertyDaoSpy ) . toHaveBeenCalledWith ( [ "zones" , "speed" ] , serializedZones ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should reset user settings" , ( done ) => { const saveDaoSpy = spyOn ( userSettingsService . userSettingsDao , "save" ) . and . returnValue ( Promise . resolve ( userSettingsData ) ) ; const promiseUpdate = userSettingsService . reset ( ) ; promiseUpdate . then ( ( result ) => { expect ( result ) . not . toBeNull ( ) ; expect ( result ) . toEqual ( userSettingsData ) ; expect ( saveDaoSpy ) . toHaveBeenCalledTimes ( 1 ) ; expect ( saveDaoSpy ) . toHaveBeenCalledWith ( userSettingsData ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should reset user zones settings" , ( done ) => { const upsertPropertyDao = spyOn ( userSettingsService . userSettingsDao , "upsertProperty" ) . and . returnValue ( Promise . resolve ( userSettingsData ) ) ; const promiseUpdate = userSettingsService . resetZones ( ) ; promiseUpdate . then ( ( result ) => { expect ( result ) . not . toBeNull ( ) ; expect ( upsertPropertyDao ) . toHaveBeenCalledTimes ( 1 ) ; expect ( upsertPropertyDao ) . toHaveBeenCalledWith ( [ "zones" ] , userSettingsData . zones ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( ) ; done ( ) ; } ) ; } ) ; } ) ;