import * as _ from "lodash" ; import { AppResourcesModel } from "../../scripts/models/app-resources.model" ; import { editActivityFromArray , removeActivityFromArray } from "../tools/specs-tools" ; import { StravaActivityModel } from "../../scripts/models/sync/strava-activity.model" ; import { ActivitiesSynchronize } from "../../scripts/processors/activities-synchronize" ; import { AthleteSnapshotResolver } from "@elevate/shared/resolvers" ; import { ActivitiesChangesModel , AthleteModel , SyncedActivityModel , UserSettingsModel } from "@elevate/shared/models" ; import { userSettingsData } from "@elevate/shared/data" ; describe ( "ActivitiesSynchronize" , ( ) => { let userSettingsMock ; let athleteModelResolver ; beforeEach ( ( done ) => { userSettingsMock = _ . cloneDeep ( userSettingsData ) ; athleteModelResolver = new AthleteSnapshotResolver ( AthleteModel . DEFAULT_MODEL ) ; done ( ) ; } ) ; it ( "should remove activity from array properly " , ( done ) => { let rawPageOfActivities = _ . cloneDeep ( require ( "../fixtures/sync/rawPage0120161213.json" ) . models ) ; const sourceCount = rawPageOfActivities . length ; rawPageOfActivities = removeActivityFromArray ( 722210052 , rawPageOfActivities ) ; expect ( rawPageOfActivities ) . not . toBeNull ( ) ; expect ( _ . find ( rawPageOfActivities , { id : 722210052 } ) ) . toBeUndefined ( ) ; expect ( rawPageOfActivities . length ) . toEqual ( sourceCount - 1 ) ; done ( ) ; } ) ; it ( "should edit activity from array properly " , ( done ) => { let rawPageOfActivities = _ . cloneDeep ( require ( "../fixtures/sync/rawPage0120161213.json" ) . models ) ; const sourceCount = rawPageOfActivities . length ; rawPageOfActivities = editActivityFromArray ( 722210052 , rawPageOfActivities , "New_Name" , "Ride" ) ; expect ( rawPageOfActivities ) . not . toBeNull ( ) ; const foundBack = _ . find ( rawPageOfActivities , { id : 722210052 } ) ; expect ( foundBack ) . toBeDefined ( ) ; expect ( foundBack . name ) . toEqual ( "New_Name" ) ; expect ( foundBack . type ) . toEqual ( "Ride" ) ; expect ( foundBack . display_type ) . toEqual ( "Ride" ) ; expect ( rawPageOfActivities . length ) . toEqual ( sourceCount ) ; done ( ) ; } ) ; it ( "should detect activities added, modified and deleted " , ( done ) => { let syncedActivities = _ . cloneDeep ( require ( "../fixtures/sync/syncedActivities20161213.json" ) . syncedActivities ) ; let rawPageOfActivities = _ . cloneDeep ( require ( "../fixtures/sync/rawPage0120161213.json" ) . models ) ; syncedActivities = removeActivityFromArray ( 723224273 , syncedActivities ) ; syncedActivities = removeActivityFromArray ( 707356065 , syncedActivities ) ; rawPageOfActivities = editActivityFromArray ( 799672885 , rawPageOfActivities , "Run comeback" , "Run" ) ; rawPageOfActivities = editActivityFromArray ( 708752345 , rawPageOfActivities , "MTB @ Bastille" , "Ride" ) ; const changes = ActivitiesSynchronize . findAddedAndEditedActivities ( rawPageOfActivities , syncedActivities ) ; expect ( changes ) . not . toBeNull ( ) ; expect ( changes . deleted ) . toEqual ( [ ] ) ; expect ( changes . added . length ) . toEqual ( 2 ) ; expect ( _ . indexOf ( changes . added , 723224273 ) ) . not . toEqual ( - 1 ) ; expect ( _ . indexOf ( changes . added , 707356065 ) ) . not . toEqual ( - 1 ) ; expect ( _ . indexOf ( changes . added , 999999999 ) ) . toEqual ( - 1 ) ; expect ( changes . edited . length ) . toEqual ( 2 ) ; expect ( _ . find ( changes . edited , { id : 799672885 } ) ) . toBeDefined ( ) ; expect ( _ . find ( changes . edited , { id : 708752345 } ) ) . toBeDefined ( ) ; let findWhere = _ . find ( changes . edited , { id : 799672885 } ) ; expect ( findWhere . name ) . toEqual ( "Run comeback" ) ; expect ( findWhere . type ) . toEqual ( "Run" ) ; expect ( findWhere . display_type ) . toEqual ( "Run" ) ; findWhere = _ . find ( changes . edited , { id : 708752345 } ) ; expect ( findWhere . name ) . toEqual ( "MTB @ Bastille" ) ; expect ( findWhere . type ) . toEqual ( "Ride" ) ; expect ( findWhere . display_type ) . toEqual ( "Ride" ) ; expect ( ActivitiesSynchronize . findAddedAndEditedActivities ( null , null ) ) . not . toBeNull ( ) ; done ( ) ; } ) ; it ( "should append activities of pages where activities added, modified and deleted " , ( done ) => { const appResourcesMock = _ . cloneDeep ( require ( "../fixtures/app-resources/app-resources.json" ) ) ; const activitiesSynchronize = new ActivitiesSynchronize ( appResourcesMock , userSettingsMock , athleteModelResolver ) ; activitiesSynchronize . appendGlobalActivitiesChanges ( { added : [ 1 , 2 ] , deleted : [ ] , edited : [ ] } ) ; expect ( activitiesSynchronize . activitiesChanges ) . not . toBeNull ( ) ; expect ( activitiesSynchronize . activitiesChanges . added ) . toEqual ( [ 1 , 2 ] ) ; expect ( activitiesSynchronize . activitiesChanges . deleted . length ) . toEqual ( 0 ) ; expect ( activitiesSynchronize . activitiesChanges . edited . length ) . toEqual ( 0 ) ; activitiesSynchronize . appendGlobalActivitiesChanges ( { added : [ 4 , 5 ] , deleted : [ ] , edited : [ { id : 6 , name : "rideName" , type : "Ride" , display_type : "Ride" } ] } ) ; expect ( activitiesSynchronize . activitiesChanges ) . not . toBeNull ( ) ; expect ( activitiesSynchronize . activitiesChanges . added . length ) . toEqual ( 4 ) ; expect ( activitiesSynchronize . activitiesChanges . deleted . length ) . toEqual ( 0 ) ; expect ( activitiesSynchronize . activitiesChanges . edited . length ) . toEqual ( 1 ) ; activitiesSynchronize . appendGlobalActivitiesChanges ( { added : [ 5 , 10 , 11 ] , deleted : [ 15 , 16 ] , edited : [ { id : 6 , name : "rideName" , type : "Ride" , display_type : "Ride" } , { id : 22 , name : "Run..." , type : "Run" , display_type : "Run" } ] } ) ; expect ( activitiesSynchronize . activitiesChanges ) . not . toBeNull ( ) ; expect ( activitiesSynchronize . activitiesChanges . added . length ) . toEqual ( 6 ) ; expect ( activitiesSynchronize . activitiesChanges . deleted . length ) . toEqual ( 2 ) ; expect ( activitiesSynchronize . activitiesChanges . edited . length ) . toEqual ( 3 ) ; done ( ) ; } ) ; } ) ;