Skip to content

Commit 3a17cc3

Browse files
johnsmathewvalorkin
authored andcommitted
fix(datepicker): select correct month and year from month picker view… (#4501)
* fix(datepicker): select correct month and year from month picker view when displayMonths > 1 * test(datepicker): select correct year on month selection * test(datepicker): add test for show and hide * fix(tslint): fix tslint errors * feat(common): clean up
1 parent b2bd459 commit 3a17cc3

File tree

2 files changed

+100
-14
lines changed

2 files changed

+100
-14
lines changed

src/datepicker/bs-datepicker.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { Component, ViewChild } from '@angular/core';
2+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3+
4+
import { BsDatepickerConfig, BsDatepickerDirective, BsDatepickerModule } from '.';
5+
import { CalendarCellViewModel } from './models';
6+
import { BsDatepickerContainerComponent } from './themes/bs/bs-datepicker-container.component';
7+
8+
@Component({
9+
selector: 'test-cmp',
10+
template: `<input type="text" bsDatepicker [bsConfig]="bsConfig">`
11+
})
12+
class TestComponent {
13+
@ViewChild(BsDatepickerDirective) datepicker: BsDatepickerDirective;
14+
bsConfig: Partial<BsDatepickerConfig> = {
15+
displayMonths: 2
16+
};
17+
}
18+
19+
type TestFixture = ComponentFixture<TestComponent>;
20+
21+
function getDatepickerDirective(fixture: TestFixture): BsDatepickerDirective {
22+
const datepicker: BsDatepickerDirective = fixture.componentInstance.datepicker;
23+
24+
return datepicker;
25+
}
26+
27+
function showDatepicker(fixture: TestFixture): BsDatepickerDirective {
28+
const datepicker = getDatepickerDirective(fixture);
29+
datepicker.show();
30+
fixture.detectChanges();
31+
32+
return datepicker;
33+
}
34+
35+
function hideDatepicker(fixture: TestFixture): BsDatepickerDirective {
36+
const datepicker = getDatepickerDirective(fixture);
37+
datepicker.hide();
38+
fixture.detectChanges();
39+
40+
return datepicker;
41+
}
42+
43+
function getDatepickerContainer(datepicker: BsDatepickerDirective): BsDatepickerContainerComponent | null {
44+
return datepicker[`_datepickerRef`] ? datepicker[`_datepickerRef`].instance : null;
45+
}
46+
47+
describe('datepicker:', () => {
48+
let fixture: TestFixture;
49+
beforeEach(
50+
async(() => TestBed.configureTestingModule({
51+
declarations: [TestComponent],
52+
imports: [BsDatepickerModule.forRoot()]
53+
}).compileComponents()
54+
));
55+
beforeEach(() => {
56+
fixture = TestBed.createComponent(TestComponent);
57+
fixture.detectChanges();
58+
});
59+
60+
it('should display datepicker on show', () => {
61+
const datepicker = showDatepicker(fixture);
62+
expect(getDatepickerContainer(datepicker)).toBeDefined();
63+
});
64+
65+
it('should hide datepicker on hide', () => {
66+
const datepicker = hideDatepicker(fixture);
67+
expect(getDatepickerContainer(datepicker)).toBeNull();
68+
});
69+
70+
it('should select correct year when a month other than selected year is chosen', () => {
71+
const datepicker = showDatepicker(fixture);
72+
const datepickerContainerInstance = getDatepickerContainer(datepicker);
73+
const yearSelection: CalendarCellViewModel = { date: new Date(2017, 1, 1), label: 'label' };
74+
const monthSelection: CalendarCellViewModel = { date: new Date(2018, 1, 1), label: 'label' };
75+
datepickerContainerInstance.yearSelectHandler(yearSelection);
76+
datepickerContainerInstance.monthSelectHandler(monthSelection);
77+
fixture.detectChanges();
78+
datepickerContainerInstance[`_store`]
79+
.select(state => state.view)
80+
.subscribe(view => {
81+
expect(view.date.getFullYear()).toEqual(monthSelection.date.getFullYear());
82+
});
83+
});
84+
});

src/datepicker/reducer/bs-datepicker.effects.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { Injectable } from '@angular/core';
2+
23
import { Observable, Subscription } from 'rxjs';
4+
import { filter, map } from 'rxjs/operators';
5+
36
import { getFullYear, getMonth } from 'ngx-bootstrap/chronos';
7+
48
import { BsDatepickerAbstractComponent } from '../base/bs-datepicker-container';
9+
import { BsDatepickerActions } from './bs-datepicker.actions';
510
import { BsDatepickerConfig } from '../bs-datepicker.config';
11+
import { BsDatepickerStore } from './bs-datepicker.store';
12+
import { BsLocaleService } from '../bs-locale.service';
13+
614
import {
715
BsDatepickerViewMode,
816
BsNavigationEvent,
@@ -14,10 +22,7 @@ import {
1422
MonthsCalendarViewModel,
1523
YearsCalendarViewModel
1624
} from '../models';
17-
import { BsDatepickerActions } from './bs-datepicker.actions';
18-
import { BsDatepickerStore } from './bs-datepicker.store';
19-
import { BsLocaleService } from '../bs-locale.service';
20-
import { filter, map } from 'rxjs/operators';
25+
2126

2227
@Injectable()
2328
export class BsDatepickerEffects {
@@ -136,21 +141,16 @@ export class BsDatepickerEffects {
136141
event.cell.isHovered = event.isHovered;
137142
};
138143

139-
/** select handlers */
140-
// container.daySelectHandler = (day: DayViewModel): void => {
141-
// if (day.isOtherMonth || day.isDisabled) {
142-
// return;
143-
// }
144-
// this._store.dispatch(this._actions.select(day.date));
145-
// };
146-
147144
container.monthSelectHandler = (event: CalendarCellViewModel): void => {
148145
if (event.isDisabled) {
149146
return;
150147
}
151148
this._store.dispatch(
152149
this._actions.navigateTo({
153-
unit: {month: getMonth(event.date)},
150+
unit: {
151+
month: getMonth(event.date),
152+
year: getFullYear(event.date)
153+
},
154154
viewMode: 'day'
155155
})
156156
);
@@ -162,7 +162,9 @@ export class BsDatepickerEffects {
162162
}
163163
this._store.dispatch(
164164
this._actions.navigateTo({
165-
unit: {year: getFullYear(event.date)},
165+
unit: {
166+
year: getFullYear(event.date)
167+
},
166168
viewMode: 'month'
167169
})
168170
);

0 commit comments

Comments
 (0)