@@ -928,7 +928,7 @@ describe('MatMdcInput with forms', () => {
928
928
expect ( testComponent . formControl . untouched ) . toBe ( true , 'Expected untouched form control' ) ;
929
929
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
930
930
expect ( inputEl . getAttribute ( 'aria-invalid' ) )
931
- . toBe ( null , 'Expected aria-invalid not to be present .' ) ;
931
+ . toBe ( 'false' , 'Expected aria-invalid to be set to "false" .' ) ;
932
932
} ) ) ;
933
933
934
934
it ( 'should display an error message when the input is touched and invalid' , fakeAsync ( ( ) => {
@@ -997,7 +997,7 @@ describe('MatMdcInput with forms', () => {
997
997
expect ( component . formGroup . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
998
998
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
999
999
expect ( inputEl . getAttribute ( 'aria-invalid' ) )
1000
- . toBe ( null , 'Expected aria-invalid not to be present ' ) ;
1000
+ . toBe ( 'false' , 'Expected aria-invalid to be set to "false". ' ) ;
1001
1001
expect ( component . formGroupDirective . submitted )
1002
1002
. toBe ( false , 'Expected form not to have been submitted' ) ;
1003
1003
@@ -1081,7 +1081,7 @@ describe('MatMdcInput with forms', () => {
1081
1081
expect ( describedBy ) . toBe ( errorIds ) ;
1082
1082
} ) ) ;
1083
1083
1084
- it ( 'should not set `aria-invalid` if the input is empty' , fakeAsync ( ( ) => {
1084
+ it ( 'should set `aria-invalid` to true if the input is empty' , fakeAsync ( ( ) => {
1085
1085
// Submit the form since it's the one that triggers the default error state matcher.
1086
1086
dispatchFakeEvent ( fixture . nativeElement . querySelector ( 'form' ) , 'submit' ) ;
1087
1087
fixture . detectChanges ( ) ;
@@ -1090,7 +1090,7 @@ describe('MatMdcInput with forms', () => {
1090
1090
expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
1091
1091
expect ( inputEl . value ) . toBeFalsy ( ) ;
1092
1092
expect ( inputEl . getAttribute ( 'aria-invalid' ) )
1093
- . toBe ( null , 'Expected aria-invalid not to be present ' ) ;
1093
+ . toBe ( 'true' , 'Expected aria-invalid to be set to "true" ' ) ;
1094
1094
1095
1095
inputEl . value = 'not valid' ;
1096
1096
fixture . detectChanges ( ) ;
@@ -1300,6 +1300,48 @@ describe('MatFormField default options', () => {
1300
1300
1301
1301
} ) ;
1302
1302
1303
+ describe ( 'MatInput that is requried with a formControl' , ( ) => {
1304
+ let fixture : ComponentFixture < MatInputWithRequiredAndFormControl > ;
1305
+ let input : HTMLInputElement ;
1306
+ let formControl : FormControl ;
1307
+
1308
+ beforeEach ( ( ) => {
1309
+ fixture = createComponent ( MatInputWithRequiredAndFormControl ) ;
1310
+ formControl = fixture . componentInstance . formControl ;
1311
+ input = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ! ;
1312
+ fixture . detectChanges ( ) ;
1313
+ } ) ;
1314
+
1315
+ it ( 'should set aria-required.' , ( ) => {
1316
+ expect ( input . getAttribute ( 'aria-required' ) )
1317
+ . toBe ( 'true' , 'Expected "aria-required" to be "true"' ) ;
1318
+ } ) ;
1319
+
1320
+ it ( 'should not set any value for aria-invalid.' , ( ) => {
1321
+ expect ( input . getAttribute ( 'aria-invalid' ) )
1322
+ . toBe ( null , 'Expected "aria-invalid" not to be set' ) ;
1323
+ } ) ;
1324
+
1325
+ it ( 'should not set aria-invalid when empty and invalid.' , ( ) => {
1326
+ formControl . setErrors ( { error : 'True!' } ) ;
1327
+ formControl . markAsTouched ( ) ;
1328
+ fixture . detectChanges ( ) ;
1329
+
1330
+ expect ( input . getAttribute ( 'aria-invalid' ) )
1331
+ . toBe ( null , 'Expected "aria-invalid" not to be set' ) ;
1332
+ } ) ;
1333
+
1334
+ it ( 'should set aria-invalid when not empty and invalid.' , ( ) => {
1335
+ input . value = 'Some value' ;
1336
+ formControl . setErrors ( { error : 'True!' } ) ;
1337
+ formControl . markAsTouched ( ) ;
1338
+ fixture . detectChanges ( ) ;
1339
+
1340
+ expect ( input . getAttribute ( 'aria-invalid' ) )
1341
+ . toBe ( 'true' , 'Expected "aria-invalid" to be "true"' ) ;
1342
+ } ) ;
1343
+ } ) ;
1344
+
1303
1345
function configureTestingModule ( component : Type < any > , options :
1304
1346
{ providers ?: Provider [ ] , imports ?: any [ ] , declarations ?: any [ ] , animations ?: boolean } = { } ) {
1305
1347
const { providers = [ ] , imports = [ ] , declarations = [ ] , animations = true } = options ;
@@ -1823,3 +1865,14 @@ class MatInputWithColor {
1823
1865
`
1824
1866
} )
1825
1867
class MatInputInsideOutsideFormField { }
1868
+
1869
+ @Component ( {
1870
+ template : `
1871
+ <mat-form-field>
1872
+ <input matInput required [formControl]="formControl">
1873
+ </mat-form-field>
1874
+ `
1875
+ } )
1876
+ class MatInputWithRequiredAndFormControl {
1877
+ formControl = new FormControl ( ) ;
1878
+ }
0 commit comments