Skip to content

Commit ef66524

Browse files
author
Guido Quezada
committed
TT-39 fix: default month when changing year
1 parent 62227a6 commit ef66524

File tree

3 files changed

+18
-28
lines changed

3 files changed

+18
-28
lines changed

src/app/modules/shared/components/month-picker/month-picker.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<div class="card-header">
22
<div class="d-flex align-items-center">
3-
<button class="btn mr-auto" (click)="decrement()">
3+
<button class="btn mr-auto" (click)="changeYear('subtract')">
44
<i class="fa fa-chevron-left"></i>
55
</button>
66
<p>
77
{{selectedYearText}}
88
</p>
9-
<button class="btn ml-auto" (click)="increment()">
9+
<button class="btn ml-auto" (click)="changeYear('add')">
1010
<i class="fa fa-chevron-right"></i>
1111
</button>
1212
</div>

src/app/modules/shared/components/month-picker/month-picker.component.spec.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ describe('MonthPickerComponent', () => {
3737
it('should add a year to current year', () => {
3838
component.selectedYearMoment = moment();
3939
const incrementYear = moment().add(1, 'year').format('Y');
40-
spyOn(component, 'increment').and.callThrough();
40+
spyOn(component, 'changeYear').and.callThrough();
4141

42-
component.increment();
42+
component.changeYear('add');
4343

44-
expect(component.increment).toHaveBeenCalled();
44+
expect(component.changeYear).toHaveBeenCalled();
4545
expect(component.selectedYearText).toEqual(incrementYear);
4646
expect(component.selectedYearMoment.format('Y')).toEqual(incrementYear);
4747
});
@@ -50,21 +50,19 @@ describe('MonthPickerComponent', () => {
5050
it('should subtract a year to current year', () => {
5151
component.selectedYearMoment = moment();
5252
const decrementYear = moment().subtract(1, 'year').format('Y');
53-
spyOn(component, 'decrement').and.callThrough();
53+
spyOn(component, 'changeYear').and.callThrough();
5454

55-
component.decrement();
55+
component.changeYear('subtract');
5656

57-
expect(component.decrement).toHaveBeenCalled();
57+
expect(component.changeYear).toHaveBeenCalled();
5858
expect(component.selectedYearMoment.format('Y')).toEqual(decrementYear);
5959
});
6060

6161

62-
it('selectMonth should call selectDates', () => {
62+
it('selectMonth should call selectDate', () => {
6363
spyOn(component, 'selectDate');
64-
65-
component.selectMonth(8);
66-
67-
expect(component.selectDate).toHaveBeenCalledWith(8, 2020);
64+
component.selectMonth(10);
65+
expect(component.selectDate).toHaveBeenCalledWith(10, 2020);
6866
});
6967

7068
});

src/app/modules/shared/components/month-picker/month-picker.component.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,31 @@ export class MonthPickerComponent implements OnInit {
1919

2020
selectedYearText: string;
2121
months: Array<string> = [];
22-
years: Array<number> = [];
2322

2423
constructor() {
2524
this.selectedYearMoment = moment();
2625
this.selectedMonthMoment = moment();
2726
this.months = moment.months();
2827
this.selectedMonthIndex = this.selectedMonthMoment.month();
28+
this.selectedYearText = moment(this.selectedYearMoment).format('YYYY');
2929
this.selectedYear = this.selectedYearMoment.year();
30-
this.updateYearText();
3130
}
3231

3332
ngOnInit() {
3433
this.selectDate(this.selectedMonthIndex, this.selectedYear);
3534
}
3635

37-
updateYearText() {
38-
this.selectedYearText = moment(this.selectedYearMoment).format('YYYY');
39-
}
40-
41-
increment() {
42-
this.selectedYearMoment = this.selectedYearMoment.add(1, 'year');
43-
this.updateYearText();
44-
}
36+
changeYear(changeAction: string) {
37+
this.selectedYearMoment[changeAction](1, 'year');
38+
this.selectedYear = this.selectedYearMoment.year();
4539

46-
decrement() {
47-
this.selectedYearMoment = this.selectedYearMoment.subtract(1, 'year');
48-
this.updateYearText();
40+
const monthIndex = changeAction === 'add' ? 0 : 11;
41+
this.selectMonth(monthIndex);
42+
this.selectedYearText = moment(this.selectedYearMoment).format('YYYY');
4943
}
5044

5145
selectMonth(monthIndex: number) {
5246
this.selectedMonthIndex = monthIndex;
53-
this.selectedYear = this.selectedYearMoment.year();
5447
this.selectDate(this.selectedMonthIndex, this.selectedYear);
5548
}
5649

@@ -65,4 +58,3 @@ export class MonthPickerComponent implements OnInit {
6558
this.dateSelected.emit({ monthIndex, year });
6659
}
6760
}
68-

0 commit comments

Comments
 (0)