Skip to content

Commit f7beb24

Browse files
committed
fix(calendar): handled case when viewDate and value are set and adjusted test
1 parent 8d5ca57 commit f7beb24

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

projects/igniteui-angular/src/lib/calendar/calendar-base.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
175175
* Sets the date that will be presented in the default view when the component renders.
176176
*/
177177
public set viewDate(value: Date) {
178-
this._viewDate = this.getDateOnly(value);
178+
if (this._viewDate) {
179+
this.selectedDatesWithoutFocus = value;
180+
}
181+
const date = this.getDateOnly(value).setDate(1);
182+
this._viewDate = new Date(date);
179183
}
180184

181185
/**

projects/igniteui-angular/src/lib/calendar/calendar.component.spec.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ describe('IgxCalendar - ', () => {
172172
configureTestSuite();
173173
beforeAll(async(() => {
174174
TestBed.configureTestingModule({
175-
declarations: [IgxCalendarSampleComponent, IgxCalendarRangeComponent, IgxCalendarDisabledSpecialDatesComponent],
175+
declarations: [IgxCalendarSampleComponent, IgxCalendarRangeComponent, IgxCalendarDisabledSpecialDatesComponent,
176+
IgxCalendarValueComponent],
176177
imports: [IgxCalendarModule, FormsModule, NoopAnimationsModule]
177178
}).compileComponents();
178179
}));
@@ -290,18 +291,23 @@ describe('IgxCalendar - ', () => {
290291
});
291292

292293
it('Should show right month when value is set', () => {
294+
fixture = TestBed.createComponent(IgxCalendarValueComponent);
295+
fixture.detectChanges();
296+
calendar = fixture.componentInstance.calendar;
297+
293298
expect(calendar.weekStart).toEqual(WEEKDAYS.SUNDAY);
294299
expect(calendar.selection).toEqual('single');
300+
expect(calendar.viewDate.getMonth()).toEqual(calendar.value.getMonth());
295301

296302
const date = new Date(2020, 8, 28);
297-
calendar.value = date;
303+
calendar.viewDate = date;
298304
fixture.detectChanges();
299-
expect(
300-
(fixture.componentInstance.model as Date).toDateString()
301-
).toMatch(date.toDateString());
302-
expect((calendar.value as Date).toDateString()).toMatch(
303-
date.toDateString()
304-
);
305+
306+
expect(calendar.viewDate.getMonth()).toEqual(date.getMonth());
307+
308+
calendar.value = new Date(2020, 9, 15);
309+
fixture.detectChanges();
310+
305311
expect(calendar.viewDate.getMonth()).toEqual(date.getMonth());
306312
});
307313

@@ -1948,6 +1954,16 @@ export class IgxCalendarDisabledSpecialDatesComponent {
19481954
@ViewChild(IgxCalendarComponent, { static: true }) public calendar: IgxCalendarComponent;
19491955
}
19501956

1957+
@Component({
1958+
template: `
1959+
<igx-calendar [value]="value"></igx-calendar>
1960+
`
1961+
})
1962+
export class IgxCalendarValueComponent {
1963+
public value = new Date(2020, 7, 13);
1964+
@ViewChild(IgxCalendarComponent, { static: true }) public calendar: IgxCalendarComponent;
1965+
}
1966+
19511967
class DateTester {
19521968
// tests whether a date is disabled or not
19531969
static testDatesAvailability(dates: IgxDayItemComponent[], disabled: boolean) {

0 commit comments

Comments
 (0)