@@ -896,7 +896,7 @@ describe('MatMdcInput with forms', () => {
896896 let fixture : ComponentFixture < MatInputWithFormErrorMessages > ;
897897 let testComponent : MatInputWithFormErrorMessages ;
898898 let containerEl : HTMLElement ;
899- let inputEl : HTMLElement ;
899+ let inputEl : HTMLInputElement ;
900900
901901 beforeEach ( fakeAsync ( ( ) => {
902902 fixture = createComponent ( MatInputWithFormErrorMessages ) ;
@@ -917,6 +917,7 @@ describe('MatMdcInput with forms', () => {
917917 expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
918918 expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
919919
920+ inputEl . value = 'not valid' ;
920921 testComponent . formControl . markAsTouched ( ) ;
921922 fixture . detectChanges ( ) ;
922923 flush ( ) ;
@@ -949,6 +950,7 @@ describe('MatMdcInput with forms', () => {
949950 expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
950951 expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
951952
953+ inputEl . value = 'not valid' ;
952954 dispatchFakeEvent ( fixture . debugElement . query ( By . css ( 'form' ) ) ! . nativeElement , 'submit' ) ;
953955 fixture . detectChanges ( ) ;
954956 flush ( ) ;
@@ -981,6 +983,7 @@ describe('MatMdcInput with forms', () => {
981983 expect ( component . formGroupDirective . submitted )
982984 . toBe ( false , 'Expected form not to have been submitted' ) ;
983985
986+ inputEl . value = 'not valid' ;
984987 dispatchFakeEvent ( groupFixture . debugElement . query ( By . css ( 'form' ) ) ! . nativeElement , 'submit' ) ;
985988 groupFixture . detectChanges ( ) ;
986989 flush ( ) ;
@@ -1007,7 +1010,7 @@ describe('MatMdcInput with forms', () => {
10071010 expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
10081011 . toBe ( 0 , 'Expected no hints to be shown.' ) ;
10091012
1010- testComponent . formControl . setValue ( 'something ' ) ;
1013+ testComponent . formControl . setValue ( 'valid value ' ) ;
10111014 fixture . detectChanges ( ) ;
10121015 flush ( ) ;
10131016
@@ -1059,6 +1062,26 @@ describe('MatMdcInput with forms', () => {
10591062 expect ( errorIds ) . toBeTruthy ( 'errors should be shown' ) ;
10601063 expect ( describedBy ) . toBe ( errorIds ) ;
10611064 } ) ) ;
1065+
1066+ it ( 'should not set `aria-invalid` to true if the input is empty' , fakeAsync ( ( ) => {
1067+ // Submit the form since it's the one that triggers the default error state matcher.
1068+ dispatchFakeEvent ( fixture . nativeElement . querySelector ( 'form' ) , 'submit' ) ;
1069+ fixture . detectChanges ( ) ;
1070+ flush ( ) ;
1071+
1072+ expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
1073+ expect ( inputEl . value ) . toBeFalsy ( ) ;
1074+ expect ( inputEl . getAttribute ( 'aria-invalid' ) )
1075+ . toBe ( 'false' , 'Expected aria-invalid to be set to "false".' ) ;
1076+
1077+ inputEl . value = 'not valid' ;
1078+ fixture . detectChanges ( ) ;
1079+
1080+ expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
1081+ expect ( inputEl . getAttribute ( 'aria-invalid' ) )
1082+ . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
1083+ } ) ) ;
1084+
10621085 } ) ;
10631086
10641087 describe ( 'custom error behavior' , ( ) => {
@@ -1524,7 +1547,7 @@ class MatInputMissingMatInputTestController {}
15241547} )
15251548class MatInputWithFormErrorMessages {
15261549 @ViewChild ( 'form' ) form : NgForm ;
1527- formControl = new FormControl ( '' , Validators . required ) ;
1550+ formControl = new FormControl ( '' , [ Validators . required , Validators . pattern ( / v a l i d v a l u e / ) ] ) ;
15281551 renderError = true ;
15291552}
15301553
@@ -1543,7 +1566,7 @@ class MatInputWithFormErrorMessages {
15431566} )
15441567class MatInputWithCustomErrorStateMatcher {
15451568 formGroup = new FormGroup ( {
1546- name : new FormControl ( '' , Validators . required )
1569+ name : new FormControl ( '' , [ Validators . required , Validators . pattern ( / v a l i d v a l u e / ) ] )
15471570 } ) ;
15481571
15491572 errorState = false ;
@@ -1567,7 +1590,7 @@ class MatInputWithCustomErrorStateMatcher {
15671590class MatInputWithFormGroupErrorMessages {
15681591 @ViewChild ( FormGroupDirective ) formGroupDirective : FormGroupDirective ;
15691592 formGroup = new FormGroup ( {
1570- name : new FormControl ( '' , Validators . required )
1593+ name : new FormControl ( '' , [ Validators . required , Validators . pattern ( / v a l i d v a l u e / ) ] )
15711594 } ) ;
15721595}
15731596
0 commit comments