import { ComponentFixture , TestBed } from "@angular/core/testing" ; import { YearProgressComponent } from "./year-progress.component" ; import { SharedModule } from "../shared/shared.module" ; import { ActivityCountByTypeModel } from "./shared/models/activity-count-by-type.model" ; import { CoreModule } from "../core/core.module" ; import { YearProgressStyleModel } from "./year-progress-graph/models/year-progress-style.model" ; import { YearProgressModel } from "./shared/models/year-progress.model" ; import { ActivityDao } from "../shared/dao/activity/activity.dao" ; import { SyncService } from "../shared/services/sync/sync.service" ; import { UserSettingsService } from "../shared/services/user-settings/user-settings.service" ; import { SyncState } from "../shared/services/sync/sync-state.enum" ; import { YearProgressActivitiesFixture } from "./shared/services/year-progress-activities.fixture" ; import { SyncedActivityModel } from "@elevate/shared/models" ; import { userSettingsData } from "@elevate/shared/data" ; import { YearProgressModule } from "./year-progress.module" ; import { YearToDateProgressPresetModel } from "./shared/models/year-to-date-progress-preset.model" ; import { ProgressType } from "./shared/enums/progress-type.enum" ; import { ChromeEventsService } from "../shared/services/external-updates/impl/chrome-events.service" ; describe ( "YearProgressComponent" , ( ) => { const pluginId = "c061d18abea0" ; const yearProgressPresetModels = [ new YearToDateProgressPresetModel ( ProgressType . DISTANCE , [ "Run" ] , false , false , 750 ) , new YearToDateProgressPresetModel ( ProgressType . COUNT , [ "VirtualRide" ] , false , false ) , new YearToDateProgressPresetModel ( ProgressType . ELEVATION , [ "Ride" ] , false , false , 30000 ) , ] ; let component ; let fixture ; let syncService ; let userSettingsService ; let activityDao ; let TEST_SYNCED_ACTIVITIES ; beforeEach ( ( done ) => { spyOn ( ChromeEventsService , "getBrowserExternalMessages" ) . and . returnValue ( { addListener : ( request , sender ) => { } } ) ; spyOn ( ChromeEventsService , "getBrowserPluginId" ) . and . returnValue ( pluginId ) ; TestBed . configureTestingModule ( { imports : [ CoreModule , SharedModule , YearProgressModule ] , providers : [ ] } ) . compileComponents ( ) ; TEST_SYNCED_ACTIVITIES = YearProgressActivitiesFixture . provide ( ) ; syncService = TestBed . get ( SyncService ) ; userSettingsService = TestBed . get ( UserSettingsService ) ; activityDao = TestBed . get ( ActivityDao ) ; spyOn ( syncService , "getLastSyncDateTime" ) . and . returnValue ( Promise . resolve ( Date . now ( ) ) ) ; spyOn ( syncService , "getSyncState" ) . and . returnValue ( Promise . resolve ( SyncState . SYNCED ) ) ; spyOn ( userSettingsService , "fetch" ) . and . returnValue ( Promise . resolve ( userSettingsData ) ) ; spyOn ( activityDao , "fetch" ) . and . returnValue ( Promise . resolve ( TEST_SYNCED_ACTIVITIES ) ) ; done ( ) ; } ) ; beforeEach ( ( done ) => { fixture = TestBed . createComponent ( YearProgressComponent ) ; component = fixture . componentInstance ; spyOn ( component . yearProgressService , "fetchPresets" ) . and . returnValue ( Promise . resolve ( yearProgressPresetModels ) ) ; fixture . detectChanges ( ) ; done ( ) ; } ) ; it ( "should create" , ( done ) => { expect ( component ) . toBeTruthy ( ) ; done ( ) ; } ) ; it ( "should determine most performed activity type" , ( done ) => { const expected = "Ride" ; const activitiesCountByTypeModels = [ { type : "AlpineSki" , count : 12 } , { type : "Ride" , count : 522 } , { type : "Run" , count : 25 } , { type : "Walk" , count : 32 } , { type : "Hike" , count : 8 } , { type : "Swim" , count : 5 } , { type : "VirtualRide" , count : 29 } , { type : "InlineSkate" , count : 3 } , { type : "Workout" , count : 6 } ] ; const mostPerformedType = YearProgressComponent . findMostPerformedActivityType ( activitiesCountByTypeModels ) ; expect ( mostPerformedType ) . toEqual ( expected ) ; done ( ) ; } ) ; it ( "should give proper colors to all year lines from a color palette" , ( done ) => { const colorPalette = [ "red" , "blue" , "green" , "purple" , "orange" ] ; const expectedGlobalColors = [ "red" , "blue" , "green" , "purple" , "orange" , "red" , "blue" ] ; const yearProgressions = [ new YearProgressModel ( 2011 , [ ] ) , new YearProgressModel ( 2012 , [ ] ) , new YearProgressModel ( 2013 , [ ] ) , new YearProgressModel ( 2014 , [ ] ) , new YearProgressModel ( 2015 , [ ] ) , new YearProgressModel ( 2016 , [ ] ) , new YearProgressModel ( 2017 , [ ] ) , ] ; const style = component . styleFromPalette ( yearProgressions , colorPalette ) ; expect ( style . colors ) . toEqual ( expectedGlobalColors ) ; expect ( style . yearsColorsMap . get ( 2011 ) ) . toEqual ( "red" ) ; expect ( style . yearsColorsMap . get ( 2012 ) ) . toEqual ( "blue" ) ; expect ( style . yearsColorsMap . get ( 2013 ) ) . toEqual ( "green" ) ; expect ( style . yearsColorsMap . get ( 2014 ) ) . toEqual ( "purple" ) ; expect ( style . yearsColorsMap . get ( 2015 ) ) . toEqual ( "orange" ) ; expect ( style . yearsColorsMap . get ( 2016 ) ) . toEqual ( "red" ) ; expect ( style . yearsColorsMap . get ( 2017 ) ) . toEqual ( "blue" ) ; done ( ) ; } ) ; } ) ;