@@ -24,7 +24,7 @@ import {
24
24
getMdInputContainerPlaceholderConflictError
25
25
} from './input-container-errors' ;
26
26
import { MD_PLACEHOLDER_GLOBAL_OPTIONS } from '../core/placeholder/placeholder-options' ;
27
- import { MD_ERROR_GLOBAL_OPTIONS } from '../core/error/error-options' ;
27
+ import { MD_ERROR_GLOBAL_OPTIONS , ShowOnDirtyErrorStateMatcher } from '../core/error/error-options' ;
28
28
29
29
describe ( 'MdInputContainer' , function ( ) {
30
30
beforeEach ( async ( ( ) => {
@@ -708,32 +708,74 @@ describe('MdInputContainer', function () {
708
708
} ) ;
709
709
} ) ) ;
710
710
711
- it ( 'should display an error message when a custom error matcher returns true' , async ( ( ) => {
712
- fixture . destroy ( ) ;
711
+ it ( 'should hide the errors and show the hints once the input becomes valid' , async ( ( ) => {
712
+ testComponent . formControl . markAsTouched ( ) ;
713
+ fixture . detectChanges ( ) ;
714
+
715
+ fixture . whenStable ( ) . then ( ( ) => {
716
+ expect ( containerEl . classList )
717
+ . toContain ( 'mat-input-invalid' , 'Expected container to have the invalid CSS class.' ) ;
718
+ expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
719
+ . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
720
+ expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
721
+ . toBe ( 0 , 'Expected no hints to be shown.' ) ;
722
+
723
+ testComponent . formControl . setValue ( 'something' ) ;
724
+ fixture . detectChanges ( ) ;
725
+
726
+ fixture . whenStable ( ) . then ( ( ) => {
727
+ expect ( containerEl . classList ) . not . toContain ( 'mat-input-invalid' ,
728
+ 'Expected container not to have the invalid class when valid.' ) ;
729
+ expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
730
+ . toBe ( 0 , 'Expected no error messages when the input is valid.' ) ;
731
+ expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
732
+ . toBe ( 1 , 'Expected one hint to be shown once the input is valid.' ) ;
733
+ } ) ;
734
+ } ) ;
735
+ } ) ) ;
736
+
737
+ it ( 'should not hide the hint if there are no error messages' , async ( ( ) => {
738
+ testComponent . renderError = false ;
739
+ fixture . detectChanges ( ) ;
740
+
741
+ expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
742
+ . toBe ( 1 , 'Expected one hint to be shown on load.' ) ;
743
+
744
+ testComponent . formControl . markAsTouched ( ) ;
745
+ fixture . detectChanges ( ) ;
746
+
747
+ fixture . whenStable ( ) . then ( ( ) => {
748
+ expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
749
+ . toBe ( 1 , 'Expected one hint to still be shown.' ) ;
750
+ } ) ;
751
+ } ) ) ;
752
+
753
+ } ) ;
713
754
714
- let customFixture = TestBed . createComponent ( MdInputContainerWithCustomErrorStateMatcher ) ;
715
- let component : MdInputContainerWithCustomErrorStateMatcher ;
755
+ describe ( 'custom error behavior' , ( ) => {
756
+ it ( 'should display an error message when a custom error matcher returns true' , async ( ( ) => {
757
+ let fixture = TestBed . createComponent ( MdInputContainerWithCustomErrorStateMatcher ) ;
758
+ fixture . detectChanges ( ) ;
716
759
717
- customFixture . detectChanges ( ) ;
718
- component = customFixture . componentInstance ;
719
- containerEl = customFixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
760
+ let component = fixture . componentInstance ;
761
+ let containerEl = fixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
720
762
721
763
expect ( component . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
722
764
expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 0 , 'Expected no error messages' ) ;
723
765
724
766
component . formControl . markAsTouched ( ) ;
725
- customFixture . detectChanges ( ) ;
767
+ fixture . detectChanges ( ) ;
726
768
727
- customFixture . whenStable ( ) . then ( ( ) => {
769
+ fixture . whenStable ( ) . then ( ( ) => {
728
770
expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
729
- . toBe ( 0 , 'Expected no error messages after being touched.' ) ;
771
+ . toBe ( 0 , 'Expected no error messages after being touched.' ) ;
730
772
731
773
component . errorState = true ;
732
- customFixture . detectChanges ( ) ;
774
+ fixture . detectChanges ( ) ;
733
775
734
- customFixture . whenStable ( ) . then ( ( ) => {
776
+ fixture . whenStable ( ) . then ( ( ) => {
735
777
expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
736
- . toBe ( 1 , 'Expected one error messages to have been rendered.' ) ;
778
+ . toBe ( 1 , 'Expected one error messages to have been rendered.' ) ;
737
779
} ) ;
738
780
} ) ;
739
781
} ) ) ;
@@ -763,18 +805,19 @@ describe('MdInputContainer', function () {
763
805
]
764
806
} ) ;
765
807
766
- let customFixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
767
- customFixture . detectChanges ( ) ;
808
+ let fixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
768
809
769
- containerEl = customFixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
770
- testComponent = customFixture . componentInstance ;
810
+ fixture . detectChanges ( ) ;
811
+
812
+ let containerEl = fixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
813
+ let testComponent = fixture . componentInstance ;
771
814
772
815
// Expect the control to still be untouched but the error to show due to the global setting
773
816
expect ( testComponent . formControl . untouched ) . toBe ( true , 'Expected untouched form control' ) ;
774
817
expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 1 , 'Expected an error message' ) ;
775
818
} ) ;
776
819
777
- it ( 'should display an error message when global setting shows errors on dirty ' , async ( ) => {
820
+ it ( 'should display an error message when using ShowOnDirtyErrorStateMatcher ' , async ( ( ) => {
778
821
TestBed . resetTestingModule ( ) ;
779
822
TestBed . configureTestingModule ( {
780
823
imports : [
@@ -787,79 +830,36 @@ describe('MdInputContainer', function () {
787
830
MdInputContainerWithFormErrorMessages
788
831
] ,
789
832
providers : [
790
- { provide : MD_ERROR_GLOBAL_OPTIONS , useValue : { showOnDirty : true } }
833
+ { provide : MD_ERROR_GLOBAL_OPTIONS , useClass : ShowOnDirtyErrorStateMatcher }
791
834
]
792
835
} ) ;
793
836
794
- let customFixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
795
- customFixture . detectChanges ( ) ;
837
+ let fixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
838
+ fixture . detectChanges ( ) ;
796
839
797
- containerEl = customFixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
798
- testComponent = customFixture . componentInstance ;
840
+ let containerEl = fixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
841
+ let testComponent = fixture . componentInstance ;
799
842
800
843
expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
801
844
expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 0 , 'Expected no error messages' ) ;
802
845
803
- testComponent . formControl . markAsTouched ( ) ;
804
- customFixture . detectChanges ( ) ;
805
-
806
- customFixture . whenStable ( ) . then ( ( ) => {
807
- expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
808
- . toBe ( 0 , 'Expected no error messages when touched' ) ;
809
-
810
- testComponent . formControl . markAsDirty ( ) ;
811
- customFixture . detectChanges ( ) ;
812
-
813
- customFixture . whenStable ( ) . then ( ( ) => {
814
- expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
815
- . toBe ( 1 , 'Expected one error message when dirty' ) ;
816
- } ) ;
817
- } ) ;
818
-
819
- } ) ;
820
-
821
- it ( 'should hide the errors and show the hints once the input becomes valid' , async ( ( ) => {
822
846
testComponent . formControl . markAsTouched ( ) ;
823
847
fixture . detectChanges ( ) ;
824
848
825
849
fixture . whenStable ( ) . then ( ( ) => {
826
- expect ( containerEl . classList )
827
- . toContain ( 'mat-input-invalid' , 'Expected container to have the invalid CSS class.' ) ;
828
850
expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
829
- . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
830
- expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
831
- . toBe ( 0 , 'Expected no hints to be shown.' ) ;
851
+ . toBe ( 0 , 'Expected no error messages when touched' ) ;
832
852
833
- testComponent . formControl . setValue ( 'something' ) ;
853
+ testComponent . formControl . markAsDirty ( ) ;
834
854
fixture . detectChanges ( ) ;
835
855
836
856
fixture . whenStable ( ) . then ( ( ) => {
837
- expect ( containerEl . classList ) . not . toContain ( 'mat-input-invalid' ,
838
- 'Expected container not to have the invalid class when valid.' ) ;
839
857
expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
840
- . toBe ( 0 , 'Expected no error messages when the input is valid.' ) ;
841
- expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
842
- . toBe ( 1 , 'Expected one hint to be shown once the input is valid.' ) ;
858
+ . toBe ( 1 , 'Expected one error message when dirty' ) ;
843
859
} ) ;
844
860
} ) ;
845
- } ) ) ;
846
-
847
- it ( 'should not hide the hint if there are no error messages' , async ( ( ) => {
848
- testComponent . renderError = false ;
849
- fixture . detectChanges ( ) ;
850
861
851
- expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
852
- . toBe ( 1 , 'Expected one hint to be shown on load.' ) ;
853
-
854
- testComponent . formControl . markAsTouched ( ) ;
855
- fixture . detectChanges ( ) ;
856
-
857
- fixture . whenStable ( ) . then ( ( ) => {
858
- expect ( containerEl . querySelectorAll ( 'md-hint' ) . length )
859
- . toBe ( 1 , 'Expected one hint to still be shown.' ) ;
860
- } ) ;
861
862
} ) ) ;
862
-
863
863
} ) ;
864
864
865
865
it ( 'should not have prefix and suffix elements when none are specified' , ( ) => {
0 commit comments