@@ -22,6 +22,22 @@ var DATEPICKER_TEMPLATE =
2222 'ng-disabled="isDisabled">' +
2323 '</md-datepicker>' ;
2424
25+ var DATEPICKER_FORM_TEMPLATE =
26+ '<form name="birthdayForm">' +
27+ ' <md-datepicker name="birthday" ' +
28+ ' md-max-date="maxDate" ' +
29+ ' md-min-date="minDate" ' +
30+ ' md-date-filter="dateFilter" ' +
31+ ' md-month-filter="monthFilter" ' +
32+ ' ng-model="myDate" ' +
33+ ' ng-change="dateChangedHandler()" ' +
34+ ' ng-focus="focusHandler()" ' +
35+ ' ng-blur="blurHandler()" ' +
36+ ' ng-required="isRequired" ' +
37+ ' ng-disabled="isDisabled">' +
38+ ' </md-datepicker>' +
39+ '</form>' ;
40+
2541/**
2642 * Compile and link the given template and store values for element, scope, and controller.
2743 * @param {string } template
@@ -135,6 +151,35 @@ describe('md-datepicker', function() {
135151 } ) . not . toThrow ( ) ;
136152 } ) ;
137153
154+ it ( 'should support null, undefined, and values that can be parsed into a date in a form' ,
155+ function ( ) {
156+ var formElement = createDatepickerInstance ( DATEPICKER_FORM_TEMPLATE ) ;
157+ var datepickerInputContainer =
158+ formElement [ 0 ] . querySelector ( 'md-datepicker .md-datepicker-input-container' ) ;
159+
160+ pageScope . myDate = null ;
161+ pageScope . $apply ( ) ;
162+ $timeout . flush ( ) ;
163+ expect ( datepickerInputContainer . classList . contains ( 'md-datepicker-invalid' ) ) . toBeFalsy ( ) ;
164+
165+ pageScope . myDate = undefined ;
166+ pageScope . $apply ( ) ;
167+ $timeout . flush ( ) ;
168+ expect ( datepickerInputContainer . classList . contains ( 'md-datepicker-invalid' ) ) . toBeFalsy ( ) ;
169+
170+ pageScope . myDate = '2016-09-08' ;
171+ pageScope . $apply ( ) ;
172+ $timeout . flush ( ) ;
173+ expect ( pageScope . myDate ) . toEqual ( '2016-09-08' ) ;
174+ expect ( datepickerInputContainer . classList . contains ( 'md-datepicker-invalid' ) ) . toBeFalsy ( ) ;
175+
176+ pageScope . myDate = '2021-01-20T07:00:00Z' ;
177+ pageScope . $apply ( ) ;
178+ $timeout . flush ( ) ;
179+ expect ( pageScope . myDate ) . toEqual ( '2021-01-20T07:00:00Z' ) ;
180+ expect ( datepickerInputContainer . classList . contains ( 'md-datepicker-invalid' ) ) . toBeFalsy ( ) ;
181+ } ) ;
182+
138183 it ( 'should set the element type as "date"' , function ( ) {
139184 expect ( ngElement . attr ( 'type' ) ) . toBe ( 'date' ) ;
140185 } ) ;
@@ -448,7 +493,7 @@ describe('md-datepicker', function() {
448493 expect ( controller . ngModelCtrl . $touched ) . toBe ( true ) ;
449494 } ) ;
450495
451- it ( 'should not update the input string is not "complete"' , function ( ) {
496+ it ( 'should not update the input string if not "complete"' , function ( ) {
452497 var date = new Date ( 2015 , DEC , 1 ) ;
453498 pageScope . myDate = date ;
454499
0 commit comments