Skip to content

Commit a82fadb

Browse files
committed
add tests
1 parent b810965 commit a82fadb

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

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

+8
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

+8-5
Original file line numberDiff line numberDiff line change
@@ -201,21 +201,24 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
201201

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

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

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

src/lib/datepicker/datepicker.spec.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, ViewChild} from '@angular/core';
2-
import {async, ComponentFixture, TestBed, inject} from '@angular/core/testing';
2+
import {async, ComponentFixture, TestBed, inject, fakeAsync} from '@angular/core/testing';
33
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
44
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
55
import {By} from '@angular/platform-browser';
@@ -11,7 +11,7 @@ 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, SEP} from '../core/testing/month-constants';
14+
import {DEC, JAN, JUL, JUN, SEP} from '../core/testing/month-constants';
1515
import {createKeyboardEvent, dispatchEvent} from '@angular/cdk/testing';
1616
import {MdFormFieldModule} from '../form-field/index';
1717
import {MAT_DATE_LOCALE} from '../core/datetime/date-adapter';
@@ -38,6 +38,7 @@ describe('MdDatepicker', () => {
3838
DatepickerWithChangeAndInputEvents,
3939
DatepickerWithFilterAndValidation,
4040
DatepickerWithFormControl,
41+
DatepickerWithISOStrings,
4142
DatepickerWithMinAndMaxValidation,
4243
DatepickerWithNgModel,
4344
DatepickerWithStartAt,
@@ -856,6 +857,32 @@ describe('MdDatepicker', () => {
856857
expect(testComponent.onDateInput).toHaveBeenCalled();
857858
});
858859
});
860+
861+
describe('with ISO 8601 strings as input', () => {
862+
let fixture: ComponentFixture<DatepickerWithISOStrings>;
863+
let testComponent: DatepickerWithISOStrings;
864+
865+
beforeEach(async(() => {
866+
fixture = TestBed.createComponent(DatepickerWithISOStrings);
867+
testComponent = fixture.componentInstance;
868+
}));
869+
870+
afterEach(async(() => {
871+
testComponent.datepicker.close();
872+
fixture.detectChanges();
873+
}));
874+
875+
it('should coerce ISO strings', async(() => {
876+
expect(() => fixture.detectChanges()).not.toThrow();
877+
fixture.whenStable().then(() => {
878+
fixture.detectChanges();
879+
expect(testComponent.datepicker.startAt).toEqual(new Date(2017, JUL, 1));
880+
expect(testComponent.datepickerInput.value).toEqual(new Date(2017, JUN, 1));
881+
expect(testComponent.datepickerInput.min).toEqual(new Date(2017, JAN, 1));
882+
expect(testComponent.datepickerInput.max).toEqual(new Date(2017, DEC, 31));
883+
});
884+
}));
885+
});
859886
});
860887

861888
describe('with missing DateAdapter and MD_DATE_FORMATS', () => {
@@ -1170,3 +1197,18 @@ class DatepickerWithi18n {
11701197
@ViewChild('d') datepicker: MdDatepicker<Date>;
11711198
@ViewChild(MdDatepickerInput) datepickerInput: MdDatepickerInput<Date>;
11721199
}
1200+
1201+
@Component({
1202+
template: `
1203+
<input [mdDatepicker]="d" [(ngModel)]="value" [min]="min" [max]="max">
1204+
<md-datepicker #d [startAt]="startAt"></md-datepicker>
1205+
`
1206+
})
1207+
class DatepickerWithISOStrings {
1208+
value = new Date(2017, JUN, 1).toISOString();
1209+
min = new Date(2017, JAN, 1).toISOString();
1210+
max = new Date (2017, DEC, 31).toISOString();
1211+
startAt = new Date(2017, JUL, 1).toISOString();
1212+
@ViewChild('d') datepicker: MdDatepicker<Date>;
1213+
@ViewChild(MdDatepickerInput) datepickerInput: MdDatepickerInput<Date>;
1214+
}

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

+8
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)