From 0b0e66e219683cf1d1b864fa273b9596b9ae9509 Mon Sep 17 00:00:00 2001 From: Guido Quezada Date: Tue, 29 Dec 2020 19:01:57 -0500 Subject: [PATCH] TT-39 fix: default month when changing year --- .../month-picker/month-picker.component.html | 4 ++-- .../month-picker.component.spec.ts | 20 ++++++++--------- .../month-picker/month-picker.component.ts | 22 ++++++------------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/app/modules/shared/components/month-picker/month-picker.component.html b/src/app/modules/shared/components/month-picker/month-picker.component.html index 8bb7c7901..2beec3794 100644 --- a/src/app/modules/shared/components/month-picker/month-picker.component.html +++ b/src/app/modules/shared/components/month-picker/month-picker.component.html @@ -1,12 +1,12 @@
-

{{selectedYearText}}

-
diff --git a/src/app/modules/shared/components/month-picker/month-picker.component.spec.ts b/src/app/modules/shared/components/month-picker/month-picker.component.spec.ts index ea46623f0..0012af386 100644 --- a/src/app/modules/shared/components/month-picker/month-picker.component.spec.ts +++ b/src/app/modules/shared/components/month-picker/month-picker.component.spec.ts @@ -37,11 +37,11 @@ describe('MonthPickerComponent', () => { it('should add a year to current year', () => { component.selectedYearMoment = moment(); const incrementYear = moment().add(1, 'year').format('Y'); - spyOn(component, 'increment').and.callThrough(); + spyOn(component, 'changeYear').and.callThrough(); - component.increment(); + component.changeYear('add'); - expect(component.increment).toHaveBeenCalled(); + expect(component.changeYear).toHaveBeenCalled(); expect(component.selectedYearText).toEqual(incrementYear); expect(component.selectedYearMoment.format('Y')).toEqual(incrementYear); }); @@ -50,21 +50,19 @@ describe('MonthPickerComponent', () => { it('should subtract a year to current year', () => { component.selectedYearMoment = moment(); const decrementYear = moment().subtract(1, 'year').format('Y'); - spyOn(component, 'decrement').and.callThrough(); + spyOn(component, 'changeYear').and.callThrough(); - component.decrement(); + component.changeYear('subtract'); - expect(component.decrement).toHaveBeenCalled(); + expect(component.changeYear).toHaveBeenCalled(); expect(component.selectedYearMoment.format('Y')).toEqual(decrementYear); }); - it('selectMonth should call selectDates', () => { + it('selectMonth should call selectDate', () => { spyOn(component, 'selectDate'); - - component.selectMonth(8); - - expect(component.selectDate).toHaveBeenCalledWith(8, 2020); + component.selectMonth(10); + expect(component.selectDate).toHaveBeenCalledWith(10, 2020); }); }); diff --git a/src/app/modules/shared/components/month-picker/month-picker.component.ts b/src/app/modules/shared/components/month-picker/month-picker.component.ts index ccea2b70d..ff37b39ae 100644 --- a/src/app/modules/shared/components/month-picker/month-picker.component.ts +++ b/src/app/modules/shared/components/month-picker/month-picker.component.ts @@ -19,38 +19,31 @@ export class MonthPickerComponent implements OnInit { selectedYearText: string; months: Array = []; - years: Array = []; constructor() { this.selectedYearMoment = moment(); this.selectedMonthMoment = moment(); this.months = moment.months(); this.selectedMonthIndex = this.selectedMonthMoment.month(); + this.selectedYearText = moment(this.selectedYearMoment).format('YYYY'); this.selectedYear = this.selectedYearMoment.year(); - this.updateYearText(); } ngOnInit() { this.selectDate(this.selectedMonthIndex, this.selectedYear); } - updateYearText() { - this.selectedYearText = moment(this.selectedYearMoment).format('YYYY'); - } - - increment() { - this.selectedYearMoment = this.selectedYearMoment.add(1, 'year'); - this.updateYearText(); - } + changeYear(changeAction: string) { + this.selectedYearMoment[changeAction](1, 'year'); + this.selectedYear = this.selectedYearMoment.year(); - decrement() { - this.selectedYearMoment = this.selectedYearMoment.subtract(1, 'year'); - this.updateYearText(); + const monthIndex = changeAction === 'add' ? 0 : 11; + this.selectMonth(monthIndex); + this.selectedYearText = moment(this.selectedYearMoment).format('YYYY'); } selectMonth(monthIndex: number) { this.selectedMonthIndex = monthIndex; - this.selectedYear = this.selectedYearMoment.year(); this.selectDate(this.selectedMonthIndex, this.selectedYear); } @@ -65,4 +58,3 @@ export class MonthPickerComponent implements OnInit { this.dateSelected.emit({ monthIndex, year }); } } -