Skip to content

Commit 52525a6

Browse files
committed
change value setting logic + add test
1 parent 4016c96 commit 52525a6

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/lib/datepicker/datepicker-input.ts

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
286286
let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
287287
this._lastValueValid = !date || this._dateAdapter.isValid(date);
288288
date = this._getValidDateOrNull(date);
289+
this._value = date;
289290
this._cvaOnChange(date);
290291
this._valueChange.emit(date);
291292
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement));

src/lib/datepicker/datepicker.spec.ts

+54-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import {MdDatepicker} from './datepicker';
1111
import {MdDatepickerInput} from './datepicker-input';
1212
import {MdInputModule} from '../input/index';
1313
import {MdNativeDateModule} from '../core/datetime/index';
14-
import {DEC, JAN} from '../core/testing/month-constants';
14+
import {DEC, JAN, SEP} from '../core/testing/month-constants';
1515
import {createKeyboardEvent, dispatchEvent} from '@angular/cdk/testing';
1616
import {MdFormFieldModule} from '../form-field/index';
17+
import {MAT_DATE_LOCALE} from '../core/datetime/date-adapter';
18+
import {NativeDateModule} from '../core/datetime/index';
1719

1820
describe('MdDatepicker', () => {
1921
afterEach(inject([OverlayContainer], (container: OverlayContainer) => {
@@ -943,6 +945,45 @@ describe('MdDatepicker', () => {
943945
});
944946

945947
});
948+
949+
describe('internationalization', () => {
950+
let fixture: ComponentFixture<DatepickerWithi18n>;
951+
let testComponent: DatepickerWithi18n;
952+
let input: HTMLInputElement;
953+
954+
beforeEach(async(() => {
955+
TestBed.configureTestingModule({
956+
imports: [
957+
MdDatepickerModule,
958+
MdFormFieldModule,
959+
MdInputModule,
960+
MdNativeDateModule,
961+
NoopAnimationsModule,
962+
NativeDateModule,
963+
FormsModule
964+
],
965+
providers: [{provide: MAT_DATE_LOCALE, useValue: 'de-DE'}],
966+
declarations: [DatepickerWithi18n],
967+
}).compileComponents();
968+
969+
fixture = TestBed.createComponent(DatepickerWithi18n);
970+
fixture.detectChanges();
971+
testComponent = fixture.componentInstance;
972+
input = fixture.nativeElement.querySelector('input') as HTMLInputElement;
973+
}));
974+
975+
it('should have the correct input value even when inverted date format', () => {
976+
let selected = new Date(2017, SEP, 1);
977+
testComponent.date = selected;
978+
fixture.detectChanges();
979+
980+
fixture.whenStable().then(() => {
981+
fixture.detectChanges();
982+
expect(input.value).toBe('01.09.2017');
983+
expect(testComponent.datepickerInput.value).toBe(selected);
984+
});
985+
});
986+
});
946987
});
947988

948989

@@ -1099,3 +1140,15 @@ class DatepickerWithChangeAndInputEvents {
10991140

11001141
onDateInput() {}
11011142
}
1143+
1144+
@Component({
1145+
template: `
1146+
<input [mdDatepicker]="d" [(ngModel)]="date">
1147+
<md-datepicker #d></md-datepicker>
1148+
`
1149+
})
1150+
class DatepickerWithi18n {
1151+
date: Date | null = new Date(2010, JAN, 1);
1152+
@ViewChild('d') datepicker: MdDatepicker<Date>;
1153+
@ViewChild(MdDatepickerInput) datepickerInput: MdDatepickerInput<Date>;
1154+
}

0 commit comments

Comments
 (0)