Skip to content

Commit 52c7cc5

Browse files
committed
add tests
1 parent 6a7d7a8 commit 52c7cc5

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

src/lib/core/datetime/native-date-adapter.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,14 @@ describe('NativeDateAdapter', () => {
331331
let d = '1/1/2017';
332332
expect(adapter.isDateInstance(d)).toBe(false);
333333
});
334+
335+
it('should create dates from valid ISO strings', () => {
336+
expect(adapter.fromISODateString('1985-04-12T23:20:50.52Z')).not.toBeNull();
337+
expect(adapter.fromISODateString('1996-12-19T16:39:57-08:00')).not.toBeNull();
338+
expect(adapter.fromISODateString('1937-01-01T12:00:27.87+00:20')).not.toBeNull();
339+
expect(adapter.fromISODateString('1990-13-31T23:59:00Z')).toBeNull();
340+
expect(adapter.fromISODateString('1/1/2017')).toBeNull();
341+
});
334342
});
335343

336344

src/lib/datepicker/datepicker-input.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,24 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
200200

201201
/** The form control validator for the min date. */
202202
private _minValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
203-
return (!this.min || !control.value ||
204-
this._dateAdapter.compareDate(this.min, control.value) <= 0) ?
203+
const controlValue = this._coerceDateProperty(control.value);
204+
return (!this.min || !controlValue ||
205+
this._dateAdapter.compareDate(this.min, controlValue) <= 0) ?
205206
null : {'mdDatepickerMin': {'min': this.min, 'actual': control.value}};
206207
}
207208

208209
/** The form control validator for the max date. */
209210
private _maxValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
210-
return (!this.max || !control.value ||
211-
this._dateAdapter.compareDate(this.max, control.value) >= 0) ?
211+
const controlValue = this._coerceDateProperty(control.value);
212+
return (!this.max || !controlValue ||
213+
this._dateAdapter.compareDate(this.max, controlValue) >= 0) ?
212214
null : {'mdDatepickerMax': {'max': this.max, 'actual': control.value}};
213215
}
214216

215217
/** The form control validator for the date filter. */
216218
private _filterValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
217-
return !this._dateFilter || !control.value || this._dateFilter(control.value) ?
219+
const controlValue = this._coerceDateProperty(control.value);
220+
return !this._dateFilter || !controlValue || this._dateFilter(controlValue) ?
218221
null : {'mdDatepickerFilter': true};
219222
}
220223

src/lib/datepicker/datepicker.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,32 @@ describe('MdDatepicker', () => {
865865
expect(testComponent.onDateInput).toHaveBeenCalled();
866866
});
867867
});
868+
869+
describe('with ISO 8601 strings as input', () => {
870+
let fixture: ComponentFixture<DatepickerWithISOStrings>;
871+
let testComponent: DatepickerWithISOStrings;
872+
873+
beforeEach(async(() => {
874+
fixture = TestBed.createComponent(DatepickerWithISOStrings);
875+
testComponent = fixture.componentInstance;
876+
}));
877+
878+
afterEach(async(() => {
879+
testComponent.datepicker.close();
880+
fixture.detectChanges();
881+
}));
882+
883+
it('should coerce ISO strings', async(() => {
884+
expect(() => fixture.detectChanges()).not.toThrow();
885+
fixture.whenStable().then(() => {
886+
fixture.detectChanges();
887+
expect(testComponent.datepicker.startAt).toEqual(new Date(2017, JUL, 1));
888+
expect(testComponent.datepickerInput.value).toEqual(new Date(2017, JUN, 1));
889+
expect(testComponent.datepickerInput.min).toEqual(new Date(2017, JAN, 1));
890+
expect(testComponent.datepickerInput.max).toEqual(new Date(2017, DEC, 31));
891+
});
892+
}));
893+
});
868894
});
869895

870896
describe('with missing DateAdapter and MD_DATE_FORMATS', () => {
@@ -1179,3 +1205,18 @@ class DatepickerWithi18n {
11791205
@ViewChild('d') datepicker: MdDatepicker<Date>;
11801206
@ViewChild(MdDatepickerInput) datepickerInput: MdDatepickerInput<Date>;
11811207
}
1208+
1209+
@Component({
1210+
template: `
1211+
<input [mdDatepicker]="d" [(ngModel)]="value" [min]="min" [max]="max">
1212+
<md-datepicker #d [startAt]="startAt"></md-datepicker>
1213+
`
1214+
})
1215+
class DatepickerWithISOStrings {
1216+
value = new Date(2017, JUN, 1).toISOString();
1217+
min = new Date(2017, JAN, 1).toISOString();
1218+
max = new Date (2017, DEC, 31).toISOString();
1219+
startAt = new Date(2017, JUL, 1).toISOString();
1220+
@ViewChild('d') datepicker: MdDatepicker<Date>;
1221+
@ViewChild(MdDatepickerInput) datepickerInput: MdDatepickerInput<Date>;
1222+
}

src/material-moment-adapter/adapter/moment-date-adapter.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ describe('MomentDateAdapter', () => {
309309
expect(adapter.isDateInstance(d)).toBe(false);
310310
});
311311

312+
it('should create dates from valid ISO strings', () => {
313+
expect(adapter.fromISODateString('1985-04-12T23:20:50.52Z')).not.toBeNull();
314+
expect(adapter.fromISODateString('1996-12-19T16:39:57-08:00')).not.toBeNull();
315+
expect(adapter.fromISODateString('1937-01-01T12:00:27.87+00:20')).not.toBeNull();
316+
expect(adapter.fromISODateString('1990-13-31T23:59:00Z')).toBeNull();
317+
expect(adapter.fromISODateString('1/1/2017')).toBeNull();
318+
});
319+
312320
it('setLocale should not modify global moment locale', () => {
313321
expect(moment.locale()).toBe('en');
314322
adapter.setLocale('ja-JP');

0 commit comments

Comments
 (0)