import { ComponentFixture , TestBed } from "@angular/core/testing" ; import { FitnessTrendGraphComponent } from "./fitness-trend-graph.component" ; import { FitnessService } from "../shared/services/fitness.service" ; import { ActivityService } from "../../shared/services/activity/activity.service" ; import { TEST_SYNCED_ACTIVITIES } from "../../../shared-fixtures/activities-2015.fixture" ; import * as moment from "moment" ; import { Moment } from "moment" ; import { userSettingsData } from "@elevate/shared/data" ; import { CoreModule } from "../../core/core.module" ; import { SharedModule } from "../../shared/shared.module" ; import { DayFitnessTrendModel } from "../shared/models/day-fitness-trend.model" ; import * as _ from "lodash" ; import { PeriodModel } from "../shared/models/period.model" ; import { FitnessTrendModule } from "../fitness-trend.module" ; import { HeartRateImpulseMode } from "../shared/enums/heart-rate-impulse-mode.enum" ; import { UserSettingsService } from "../../shared/services/user-settings/user-settings.service" ; describe ( "FitnessTrendGraphComponent" , ( ) => { let userSettingsService ; let activityService ; let fitnessService ; let component ; let fixture ; let todayMoment ; let updateGraphSpy ; const todayDate = "2015-12-01 12:00" ; const momentDatePattern = "YYYY-MM-DD hh:mm" ; let FITNESS_TREND = null ; beforeEach ( ( done ) => { TestBed . configureTestingModule ( { imports : [ CoreModule , SharedModule , FitnessTrendModule ] , } ) . compileComponents ( ) ; userSettingsService = TestBed . get ( UserSettingsService ) ; activityService = TestBed . get ( ActivityService ) ; fitnessService = TestBed . get ( FitnessService ) ; spyOn ( activityService , "fetch" ) . and . returnValue ( Promise . resolve ( _ . cloneDeep ( TEST_SYNCED_ACTIVITIES ) ) ) ; spyOn ( userSettingsService , "fetch" ) . and . returnValue ( Promise . resolve ( _ . cloneDeep ( userSettingsData ) ) ) ; todayMoment = moment ( "2015-12-01 12:00" , "YYYY-MM-DD hh:mm" ) ; spyOn ( fitnessService , "getTodayMoment" ) . and . returnValue ( todayMoment ) ; const fitnessTrendConfigModel = { heartRateImpulseMode : HeartRateImpulseMode . HRSS , initializedFitnessTrendModel : { atl : null , ctl : null } , allowEstimatedPowerStressScore : false , allowEstimatedRunningStressScore : false , ignoreBeforeDate : null , ignoreActivityNamePatterns : null } ; const powerMeterEnable = true ; const swimEnable = true ; const promise = fitnessService . computeTrend ( fitnessTrendConfigModel , powerMeterEnable , swimEnable ) ; promise . then ( ( fitnessTrend ) => { FITNESS_TREND = fitnessTrend ; done ( ) ; } ) ; } ) ; beforeEach ( ( done ) => { fixture = TestBed . createComponent ( FitnessTrendGraphComponent ) ; component = fixture . componentInstance ; component . isTrainingZonesEnabled = true ; component . fitnessTrend = FITNESS_TREND ; component . dateMin = moment ( _ . first ( FITNESS_TREND ) . date ) . startOf ( "day" ) . toDate ( ) ; component . dateMax = moment ( _ . last ( FITNESS_TREND ) . date ) . startOf ( "day" ) . toDate ( ) ; component . periodViewed = { from : component . dateMin , to : component . dateMax } ; spyOn ( component , "getTodayViewedDay" ) . and . returnValue ( component . getDayFitnessTrendFromDate ( todayMoment . toDate ( ) ) ) ; updateGraphSpy = spyOn ( component , "updateGraph" ) . and . stub ( ) ; fixture . detectChanges ( ) ; done ( ) ; } ) ; it ( "should create" , ( done ) => { expect ( component ) . toBeTruthy ( ) ; done ( ) ; } ) ; it ( "should allow forward period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-01-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-01-06" ; const periodTo = "2015-01-16" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( "2015-01-20" , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( "2015-01-30" , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedForward ( ) ; expect ( component . periodViewed . from . getTime ( ) ) . toBe ( expectedPeriodFrom . getTime ( ) ) ; expect ( component . periodViewed . to . getTime ( ) ) . toBe ( expectedPeriodTo . getTime ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 2 ) ; done ( ) ; } ) ; it ( "should NOT forward period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-01-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-01-10" ; const periodTo = "2015-01-20" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedForward ( ) ; expect ( component . periodViewed . from . getTime ( ) ) . toBe ( expectedPeriodFrom . getTime ( ) ) ; expect ( component . periodViewed . to . getTime ( ) ) . toBe ( expectedPeriodTo . getTime ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } ) ; it ( "should allow backward period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-01-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-01-20" ; const periodTo = "2015-01-30" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( "2015-01-6" , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( "2015-01-16" , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedBackward ( ) ; expect ( component . periodViewed . from . toDateString ( ) ) . toBe ( expectedPeriodFrom . toDateString ( ) ) ; expect ( component . periodViewed . to . toDateString ( ) ) . toBe ( expectedPeriodTo . toDateString ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 2 ) ; done ( ) ; } ) ; it ( "should NOT backward period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-01-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-01-10" ; const periodTo = "2015-01-20" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedBackward ( ) ; expect ( component . periodViewed . from . getTime ( ) ) . toBe ( expectedPeriodFrom . getTime ( ) ) ; expect ( component . periodViewed . to . getTime ( ) ) . toBe ( expectedPeriodTo . getTime ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } ) ; it ( "should allow zoom in period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-12-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-06-01" ; const periodTo = "2015-12-31" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( "2015-06-15" , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( "2015-12-31" , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedZoomIn ( ) ; expect ( component . periodViewed . from . getTime ( ) ) . toBe ( expectedPeriodFrom . getTime ( ) ) ; expect ( component . periodViewed . to . getTime ( ) ) . toBe ( expectedPeriodTo . getTime ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 2 ) ; done ( ) ; } ) ; it ( "should NOT allow zoom in period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-12-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-06-20" ; const periodTo = "2015-06-30" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedZoomIn ( ) ; expect ( component . periodViewed . from . getTime ( ) ) . toBe ( expectedPeriodFrom . getTime ( ) ) ; expect ( component . periodViewed . to . getTime ( ) ) . toBe ( expectedPeriodTo . getTime ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } ) ; it ( "should allow zoom out period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-12-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-05-15" ; const periodTo = "2015-12-31" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( "2015-05-01" , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( "2015-12-31" , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedZoomOut ( ) ; expect ( component . periodViewed . from . getTime ( ) ) . toBe ( expectedPeriodFrom . getTime ( ) ) ; expect ( component . periodViewed . to . getTime ( ) ) . toBe ( expectedPeriodTo . getTime ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 2 ) ; done ( ) ; } ) ; it ( "should NOT allow zoom out period viewed" , ( done ) => { component . dateMin = moment ( "2015-01-01" , "YYYY-MM-DD" ) . toDate ( ) ; component . dateMax = moment ( "2015-12-31" , "YYYY-MM-DD" ) . toDate ( ) ; const periodFrom = "2015-01-10" ; const periodTo = "2015-10-31" ; component . periodViewed = { from : moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) , to : moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) } ; const expectedPeriodFrom = moment ( periodFrom , "YYYY-MM-DD" ) . toDate ( ) ; const expectedPeriodTo = moment ( periodTo , "YYYY-MM-DD" ) . toDate ( ) ; component . onPeriodViewedZoomOut ( ) ; expect ( component . periodViewed . from . getTime ( ) ) . toBe ( expectedPeriodFrom . getTime ( ) ) ; expect ( component . periodViewed . to . getTime ( ) ) . toBe ( expectedPeriodTo . getTime ( ) ) ; expect ( updateGraphSpy ) . toHaveBeenCalledTimes ( 1 ) ; done ( ) ; } ) ; it ( "should convert -7 days date based period 'from/to' to 'start/end' fitness trends indexes" , ( done ) => { const period = { from : moment ( todayDate , momentDatePattern ) . subtract ( 7 , "days" ) . toDate ( ) , to : null } ; const indexes = component . indexesOf ( period ) ; expect ( indexes ) . not . toBeNull ( ) ; expect ( indexes . start ) . toEqual ( 324 ) ; expect ( indexes . end ) . toEqual ( 345 ) ; done ( ) ; } ) ; it ( "should convert -6 weeks date based period 'from/to' to 'start/end' fitness trends indexes" , ( done ) => { const period = { from : moment ( todayDate , momentDatePattern ) . subtract ( 6 , "weeks" ) . toDate ( ) , to : null } ; const indexes = component . indexesOf ( period ) ; expect ( indexes . start ) . toEqual ( 289 ) ; expect ( indexes . end ) . toEqual ( 345 ) ; done ( ) ; } ) ; it ( "should convert date based period 'from/to' to 'start/end' fitness trends indexes" , ( done ) => { const period = { from : moment ( "2015-07-01" , DayFitnessTrendModel . DATE_FORMAT ) . startOf ( "day" ) . toDate ( ) , to : moment ( "2015-09-30" , DayFitnessTrendModel . DATE_FORMAT ) . startOf ( "day" ) . toDate ( ) , } ; const indexes = component . indexesOf ( period ) ; expect ( indexes ) . not . toBeNull ( ) ; expect ( indexes . start ) . toEqual ( 178 ) ; expect ( indexes . end ) . toEqual ( 269 ) ; done ( ) ; } ) ; it ( "should failed when find indexes of 'from > to' date" , ( done ) => { const period = { from : moment ( "2015-06-01" , DayFitnessTrendModel . DATE_FORMAT ) . toDate ( ) , to : moment ( "2015-05-01" , DayFitnessTrendModel . DATE_FORMAT ) . toDate ( ) } ; let error = null ; try { component . indexesOf ( period ) ; } catch ( e ) { error = e ; } expect ( error ) . not . toBeNull ( ) ; expect ( error ) . toBe ( "FROM cannot be upper than TO date" ) ; done ( ) ; } ) ; it ( "should provide 'start' index of the first known activity when FROM don't matches athlete activities" , ( done ) => { const period = { from : moment ( "2014-06-01" , DayFitnessTrendModel . DATE_FORMAT ) . startOf ( "day" ) . toDate ( ) , to : moment ( "2015-09-30" , DayFitnessTrendModel . DATE_FORMAT ) . startOf ( "day" ) . toDate ( ) , } ; const indexes = component . indexesOf ( period ) ; expect ( indexes ) . not . toBeNull ( ) ; expect ( indexes . start ) . toEqual ( 0 ) ; expect ( indexes . end ) . toEqual ( 269 ) ; done ( ) ; } ) ; it ( "should failed when find index of TO which do not exists " , ( done ) => { const period = { from : moment ( "2015-06-01" , DayFitnessTrendModel . DATE_FORMAT ) . toDate ( ) , to : moment ( "2018-05-01" , DayFitnessTrendModel . DATE_FORMAT ) . toDate ( ) } ; let error = null ; try { component . indexesOf ( period ) ; } catch ( e ) { error = e ; } expect ( error ) . not . toBeNull ( ) ; expect ( error ) . toBe ( "No end activity index found for this TO date" ) ; done ( ) ; } ) ; } ) ;