@@ -1241,6 +1241,12 @@ describe('MatDatepicker', () => {
1241
1241
testComponent = fixture . componentInstance ;
1242
1242
} ) ) ;
1243
1243
1244
+ function revalidate ( ) {
1245
+ fixture . detectChanges ( ) ;
1246
+ flush ( ) ;
1247
+ fixture . detectChanges ( ) ;
1248
+ }
1249
+
1244
1250
afterEach ( fakeAsync ( ( ) => {
1245
1251
testComponent . datepicker . close ( ) ;
1246
1252
fixture . detectChanges ( ) ;
@@ -1253,50 +1259,39 @@ describe('MatDatepicker', () => {
1253
1259
1254
1260
it ( 'should mark invalid when value is before min' , fakeAsync ( ( ) => {
1255
1261
testComponent . date = new Date ( 2009 , DEC , 31 ) ;
1256
- fixture . detectChanges ( ) ;
1257
- flush ( ) ;
1258
- fixture . detectChanges ( ) ;
1262
+ revalidate ( ) ;
1259
1263
1260
1264
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement . classList )
1261
1265
. toContain ( 'ng-invalid' ) ;
1262
1266
} ) ) ;
1263
1267
1264
1268
it ( 'should mark invalid when value is after max' , fakeAsync ( ( ) => {
1265
1269
testComponent . date = new Date ( 2020 , JAN , 2 ) ;
1266
- fixture . detectChanges ( ) ;
1267
- flush ( ) ;
1268
-
1269
- fixture . detectChanges ( ) ;
1270
+ revalidate ( ) ;
1270
1271
1271
1272
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement . classList )
1272
1273
. toContain ( 'ng-invalid' ) ;
1273
1274
} ) ) ;
1274
1275
1275
1276
it ( 'should not mark invalid when value equals min' , fakeAsync ( ( ) => {
1276
1277
testComponent . date = testComponent . datepicker . _minDate ;
1277
- fixture . detectChanges ( ) ;
1278
- flush ( ) ;
1279
- fixture . detectChanges ( ) ;
1278
+ revalidate ( ) ;
1280
1279
1281
1280
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement . classList )
1282
1281
. not . toContain ( 'ng-invalid' ) ;
1283
1282
} ) ) ;
1284
1283
1285
1284
it ( 'should not mark invalid when value equals max' , fakeAsync ( ( ) => {
1286
1285
testComponent . date = testComponent . datepicker . _maxDate ;
1287
- fixture . detectChanges ( ) ;
1288
- flush ( ) ;
1289
- fixture . detectChanges ( ) ;
1286
+ revalidate ( ) ;
1290
1287
1291
1288
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement . classList )
1292
1289
. not . toContain ( 'ng-invalid' ) ;
1293
1290
} ) ) ;
1294
1291
1295
1292
it ( 'should not mark invalid when value is between min and max' , fakeAsync ( ( ) => {
1296
1293
testComponent . date = new Date ( 2010 , JAN , 2 ) ;
1297
- fixture . detectChanges ( ) ;
1298
- flush ( ) ;
1299
- fixture . detectChanges ( ) ;
1294
+ revalidate ( ) ;
1300
1295
1301
1296
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement . classList )
1302
1297
. not . toContain ( 'ng-invalid' ) ;
@@ -1306,31 +1301,49 @@ describe('MatDatepicker', () => {
1306
1301
const inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement ;
1307
1302
inputEl . value = '' ;
1308
1303
dispatchFakeEvent ( inputEl , 'input' ) ;
1309
-
1310
- fixture . detectChanges ( ) ;
1311
- flush ( ) ;
1312
- fixture . detectChanges ( ) ;
1304
+ revalidate ( ) ;
1313
1305
1314
1306
expect ( testComponent . model . valid ) . toBe ( true ) ;
1315
1307
1316
1308
inputEl . value = 'abcdefg' ;
1317
1309
dispatchFakeEvent ( inputEl , 'input' ) ;
1318
-
1319
- fixture . detectChanges ( ) ;
1320
- flush ( ) ;
1321
- fixture . detectChanges ( ) ;
1310
+ revalidate ( ) ;
1322
1311
1323
1312
expect ( testComponent . model . valid ) . toBe ( false ) ;
1324
1313
1325
1314
inputEl . value = '' ;
1326
1315
dispatchFakeEvent ( inputEl , 'input' ) ;
1316
+ revalidate ( ) ;
1327
1317
1328
- fixture . detectChanges ( ) ;
1329
- flush ( ) ;
1330
- fixture . detectChanges ( ) ;
1318
+ expect ( testComponent . model . valid ) . toBe ( true ) ;
1319
+ } ) ) ;
1320
+
1321
+ it ( 'should update validity when a value is assigned' , fakeAsync ( ( ) => {
1322
+ const inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) ! . nativeElement ;
1323
+ inputEl . value = '' ;
1324
+ dispatchFakeEvent ( inputEl , 'input' ) ;
1325
+ revalidate ( ) ;
1326
+
1327
+ expect ( testComponent . model . valid ) . toBe ( true ) ;
1328
+
1329
+ inputEl . value = 'abcdefg' ;
1330
+ dispatchFakeEvent ( inputEl , 'input' ) ;
1331
+ revalidate ( ) ;
1332
+
1333
+ expect ( testComponent . model . valid ) . toBe ( false ) ;
1334
+
1335
+ const validDate = new Date ( 2010 , JAN , 2 ) ;
1336
+
1337
+ // Assigning through the selection model simulates the user doing it via the calendar.
1338
+ const model = fixture . debugElement . query ( By . directive ( MatDatepicker ) )
1339
+ . injector . get < MatDateSelectionModel < Date > > ( MatDateSelectionModel ) ;
1340
+ model . updateSelection ( validDate , null ) ;
1341
+ revalidate ( ) ;
1331
1342
1332
1343
expect ( testComponent . model . valid ) . toBe ( true ) ;
1344
+ expect ( testComponent . date ) . toBe ( validDate ) ;
1333
1345
} ) ) ;
1346
+
1334
1347
} ) ;
1335
1348
1336
1349
describe ( 'datepicker with filter and validation' , ( ) => {
0 commit comments