@@ -896,7 +896,7 @@ describe('MatMdcInput with forms', () => {
896
896
let fixture : ComponentFixture < MatInputWithFormErrorMessages > ;
897
897
let testComponent : MatInputWithFormErrorMessages ;
898
898
let containerEl : HTMLElement ;
899
- let inputEl : HTMLElement ;
899
+ let inputEl : HTMLInputElement ;
900
900
901
901
beforeEach ( fakeAsync ( ( ) => {
902
902
fixture = createComponent ( MatInputWithFormErrorMessages ) ;
@@ -917,6 +917,7 @@ describe('MatMdcInput with forms', () => {
917
917
expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
918
918
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
919
919
920
+ inputEl . value = 'not valid' ;
920
921
testComponent . formControl . markAsTouched ( ) ;
921
922
fixture . detectChanges ( ) ;
922
923
flush ( ) ;
@@ -949,6 +950,7 @@ describe('MatMdcInput with forms', () => {
949
950
expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
950
951
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
951
952
953
+ inputEl . value = 'not valid' ;
952
954
dispatchFakeEvent ( fixture . debugElement . query ( By . css ( 'form' ) ) ! . nativeElement , 'submit' ) ;
953
955
fixture . detectChanges ( ) ;
954
956
flush ( ) ;
@@ -981,6 +983,7 @@ describe('MatMdcInput with forms', () => {
981
983
expect ( component . formGroupDirective . submitted )
982
984
. toBe ( false , 'Expected form not to have been submitted' ) ;
983
985
986
+ inputEl . value = 'not valid' ;
984
987
dispatchFakeEvent ( groupFixture . debugElement . query ( By . css ( 'form' ) ) ! . nativeElement , 'submit' ) ;
985
988
groupFixture . detectChanges ( ) ;
986
989
flush ( ) ;
@@ -1007,7 +1010,7 @@ describe('MatMdcInput with forms', () => {
1007
1010
expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
1008
1011
. toBe ( 0 , 'Expected no hints to be shown.' ) ;
1009
1012
1010
- testComponent . formControl . setValue ( 'something ' ) ;
1013
+ testComponent . formControl . setValue ( 'valid value ' ) ;
1011
1014
fixture . detectChanges ( ) ;
1012
1015
flush ( ) ;
1013
1016
@@ -1059,6 +1062,26 @@ describe('MatMdcInput with forms', () => {
1059
1062
expect ( errorIds ) . toBeTruthy ( 'errors should be shown' ) ;
1060
1063
expect ( describedBy ) . toBe ( errorIds ) ;
1061
1064
} ) ) ;
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
+
1062
1085
} ) ;
1063
1086
1064
1087
describe ( 'custom error behavior' , ( ) => {
@@ -1524,7 +1547,7 @@ class MatInputMissingMatInputTestController {}
1524
1547
} )
1525
1548
class MatInputWithFormErrorMessages {
1526
1549
@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 / ) ] ) ;
1528
1551
renderError = true ;
1529
1552
}
1530
1553
@@ -1543,7 +1566,7 @@ class MatInputWithFormErrorMessages {
1543
1566
} )
1544
1567
class MatInputWithCustomErrorStateMatcher {
1545
1568
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 / ) ] )
1547
1570
} ) ;
1548
1571
1549
1572
errorState = false ;
@@ -1567,7 +1590,7 @@ class MatInputWithCustomErrorStateMatcher {
1567
1590
class MatInputWithFormGroupErrorMessages {
1568
1591
@ViewChild ( FormGroupDirective ) formGroupDirective : FormGroupDirective ;
1569
1592
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 / ) ] )
1571
1594
} ) ;
1572
1595
}
1573
1596
0 commit comments