Skip to content

Commit

Permalink
fix(datepicker): fix error when selecting month with fewer days in ye…
Browse files Browse the repository at this point in the history
…ar (#6129)

view
  • Loading branch information
mmalerba authored and tinayuangao committed Jul 31, 2017
1 parent 94bf5e9 commit 9cff8c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/lib/datepicker/year-view.spec.ts
@@ -1,10 +1,10 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Component} from '@angular/core';
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdYearView} from './year-view';
import {MdCalendarBody} from './calendar-body';
import {MdNativeDateModule} from '../core/datetime/index';
import {FEB, JAN, MAR} from '../core/testing/month-constants';
import {FEB, JAN, JUL, JUN, MAR} from '../core/testing/month-constants';

describe('MdYearView', () => {
beforeEach(async(() => {
Expand Down Expand Up @@ -76,6 +76,16 @@ describe('MdYearView', () => {
expect((cellEls[0] as HTMLElement).innerText.trim()).toBe('JAN');
expect(cellEls[0].classList).toContain('mat-calendar-body-active');
});

it('should allow selection of month with less days than current active date', () => {
testComponent.date = new Date(2017, JUL, 31);
fixture.detectChanges();

expect(testComponent.yearView._monthSelected(JUN));
fixture.detectChanges();

expect(testComponent.selected).toEqual(new Date(2017, JUN, 30));
});
});

describe('year view with date filter', () => {
Expand Down Expand Up @@ -108,6 +118,8 @@ describe('MdYearView', () => {
class StandardYearView {
date = new Date(2017, JAN, 5);
selected = new Date(2017, MAR, 10);

@ViewChild(MdYearView) yearView: MdYearView<Date>;
}


Expand Down
4 changes: 3 additions & 1 deletion src/lib/datepicker/year-view.ts
Expand Up @@ -95,9 +95,11 @@ export class MdYearView<D> implements AfterContentInit {

/** Handles when a new month is selected. */
_monthSelected(month: number) {
let daysInMonth = this._dateAdapter.getNumDaysInMonth(
this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), month, 1));
this.selectedChange.emit(this._dateAdapter.createDate(
this._dateAdapter.getYear(this.activeDate), month,
this._dateAdapter.getDate(this.activeDate)));
Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth)));
}

/** Initializes this month view. */
Expand Down

0 comments on commit 9cff8c7

Please sign in to comment.