@@ -11,7 +11,7 @@ import {
11
11
} from '@angular/cdk/testing' ;
12
12
import { Component , FactoryProvider , Type , ValueProvider , ViewChild } from '@angular/core' ;
13
13
import { 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' ;
15
15
import { MAT_DATE_LOCALE , MatNativeDateModule , NativeDateModule } from '@angular/material/core' ;
16
16
import { MatFormField , MatFormFieldModule } from '@angular/material/form-field' ;
17
17
import { DEC , JAN , JUL , JUN , SEP } from '@angular/material/testing' ;
@@ -1189,6 +1189,36 @@ describe('MatDatepicker', () => {
1189
1189
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement . classList )
1190
1190
. not . toContain ( 'ng-invalid' ) ;
1191
1191
} ) ) ;
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
+ } ) ) ;
1192
1222
} ) ;
1193
1223
1194
1224
describe ( 'datepicker with filter and validation' , ( ) => {
@@ -1805,6 +1835,7 @@ class FormFieldDatepicker {
1805
1835
} )
1806
1836
class DatepickerWithMinAndMaxValidation {
1807
1837
@ViewChild ( 'd' ) datepicker : MatDatepicker < Date > ;
1838
+ @ViewChild ( NgModel ) model : NgModel ;
1808
1839
date : Date | null ;
1809
1840
minDate = new Date ( 2010 , JAN , 1 ) ;
1810
1841
maxDate = new Date ( 2020 , JAN , 1 ) ;
0 commit comments