@@ -11,7 +11,7 @@ import {
1111} from '@angular/cdk/testing' ;
1212import { Component , FactoryProvider , Type , ValueProvider , ViewChild } from '@angular/core' ;
1313import { ComponentFixture , fakeAsync , flush , inject , TestBed , tick } from '@angular/core/testing' ;
14- import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
14+ import { FormControl , FormsModule , NgModel , ReactiveFormsModule } from '@angular/forms' ;
1515import { MAT_DATE_LOCALE , MatNativeDateModule , NativeDateModule } from '@angular/material/core' ;
1616import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
1717import { DEC , JAN , JUL , JUN , SEP } from '@angular/material/testing' ;
@@ -1189,6 +1189,36 @@ describe('MatDatepicker', () => {
11891189 expect ( fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement . classList )
11901190 . not . toContain ( 'ng-invalid' ) ;
11911191 } ) ) ;
1192+
1193+ it ( 'should update validity when switching between null and invalid' , fakeAsync ( ( ) => {
1194+ const inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
1195+ inputEl . value = '' ;
1196+ dispatchFakeEvent ( inputEl , 'input' ) ;
1197+
1198+ fixture . detectChanges ( ) ;
1199+ flush ( ) ;
1200+ fixture . detectChanges ( ) ;
1201+
1202+ expect ( testComponent . model . valid ) . toBe ( true ) ;
1203+
1204+ inputEl . value = 'abcdefg' ;
1205+ dispatchFakeEvent ( inputEl , 'input' ) ;
1206+
1207+ fixture . detectChanges ( ) ;
1208+ flush ( ) ;
1209+ fixture . detectChanges ( ) ;
1210+
1211+ expect ( testComponent . model . valid ) . toBe ( false ) ;
1212+
1213+ inputEl . value = '' ;
1214+ dispatchFakeEvent ( inputEl , 'input' ) ;
1215+
1216+ fixture . detectChanges ( ) ;
1217+ flush ( ) ;
1218+ fixture . detectChanges ( ) ;
1219+
1220+ expect ( testComponent . model . valid ) . toBe ( true ) ;
1221+ } ) ) ;
11921222 } ) ;
11931223
11941224 describe ( 'datepicker with filter and validation' , ( ) => {
@@ -1805,6 +1835,7 @@ class FormFieldDatepicker {
18051835} )
18061836class DatepickerWithMinAndMaxValidation {
18071837 @ViewChild ( 'd' ) datepicker : MatDatepicker < Date > ;
1838+ @ViewChild ( NgModel ) model : NgModel ;
18081839 date : Date | null ;
18091840 minDate = new Date ( 2010 , JAN , 1 ) ;
18101841 maxDate = new Date ( 2020 , JAN , 1 ) ;
0 commit comments