Skip to content

Commit

Permalink
change value setting logic + add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jwshinjwshin committed Sep 6, 2017
1 parent 4016c96 commit 52525a6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
this._lastValueValid = !date || this._dateAdapter.isValid(date);
date = this._getValidDateOrNull(date);
this._value = date;
this._cvaOnChange(date);
this._valueChange.emit(date);
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));
Expand Down
55 changes: 54 additions & 1 deletion src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {MdDatepicker} from './datepicker';
import {MdDatepickerInput} from './datepicker-input';
import {MdInputModule} from '../input/index';
import {MdNativeDateModule} from '../core/datetime/index';
import {DEC, JAN} from '../core/testing/month-constants';
import {DEC, JAN, SEP} from '../core/testing/month-constants';
import {createKeyboardEvent, dispatchEvent} from '@angular/cdk/testing';
import {MdFormFieldModule} from '../form-field/index';
import {MAT_DATE_LOCALE} from '../core/datetime/date-adapter';
import {NativeDateModule} from '../core/datetime/index';

describe('MdDatepicker', () => {
afterEach(inject([OverlayContainer], (container: OverlayContainer) => {
Expand Down Expand Up @@ -943,6 +945,45 @@ describe('MdDatepicker', () => {
});

});

describe('internationalization', () => {
let fixture: ComponentFixture<DatepickerWithi18n>;
let testComponent: DatepickerWithi18n;
let input: HTMLInputElement;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MdDatepickerModule,
MdFormFieldModule,
MdInputModule,
MdNativeDateModule,
NoopAnimationsModule,
NativeDateModule,
FormsModule
],
providers: [{provide: MAT_DATE_LOCALE, useValue: 'de-DE'}],
declarations: [DatepickerWithi18n],
}).compileComponents();

fixture = TestBed.createComponent(DatepickerWithi18n);
fixture.detectChanges();
testComponent = fixture.componentInstance;
input = fixture.nativeElement.querySelector('input') as HTMLInputElement;
}));

it('should have the correct input value even when inverted date format', () => {
let selected = new Date(2017, SEP, 1);
testComponent.date = selected;
fixture.detectChanges();

fixture.whenStable().then(() => {
fixture.detectChanges();
expect(input.value).toBe('01.09.2017');
expect(testComponent.datepickerInput.value).toBe(selected);
});
});
});
});


Expand Down Expand Up @@ -1099,3 +1140,15 @@ class DatepickerWithChangeAndInputEvents {

onDateInput() {}
}

@Component({
template: `
<input [mdDatepicker]="d" [(ngModel)]="date">
<md-datepicker #d></md-datepicker>
`
})
class DatepickerWithi18n {
date: Date | null = new Date(2010, JAN, 1);
@ViewChild('d') datepicker: MdDatepicker<Date>;
@ViewChild(MdDatepickerInput) datepickerInput: MdDatepickerInput<Date>;
}

0 comments on commit 52525a6

Please sign in to comment.