@@ -24,7 +24,7 @@ import {
2424 getMdInputContainerPlaceholderConflictError
2525} from './input-container-errors' ;
2626import { 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' ;
2828
2929describe ( 'MdInputContainer' , function ( ) {
3030 beforeEach ( async ( ( ) => {
@@ -708,32 +708,74 @@ describe('MdInputContainer', function () {
708708 } ) ;
709709 } ) ) ;
710710
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+ } ) ;
713754
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 ( ) ;
716759
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 ;
720762
721763 expect ( component . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
722764 expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 0 , 'Expected no error messages' ) ;
723765
724766 component . formControl . markAsTouched ( ) ;
725- customFixture . detectChanges ( ) ;
767+ fixture . detectChanges ( ) ;
726768
727- customFixture . whenStable ( ) . then ( ( ) => {
769+ fixture . whenStable ( ) . then ( ( ) => {
728770 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.' ) ;
730772
731773 component . errorState = true ;
732- customFixture . detectChanges ( ) ;
774+ fixture . detectChanges ( ) ;
733775
734- customFixture . whenStable ( ) . then ( ( ) => {
776+ fixture . whenStable ( ) . then ( ( ) => {
735777 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.' ) ;
737779 } ) ;
738780 } ) ;
739781 } ) ) ;
@@ -763,18 +805,19 @@ describe('MdInputContainer', function () {
763805 ]
764806 } ) ;
765807
766- let customFixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
767- customFixture . detectChanges ( ) ;
808+ let fixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
768809
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 ;
771814
772815 // Expect the control to still be untouched but the error to show due to the global setting
773816 expect ( testComponent . formControl . untouched ) . toBe ( true , 'Expected untouched form control' ) ;
774817 expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 1 , 'Expected an error message' ) ;
775818 } ) ;
776819
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 ( ( ) => {
778821 TestBed . resetTestingModule ( ) ;
779822 TestBed . configureTestingModule ( {
780823 imports : [
@@ -787,79 +830,36 @@ describe('MdInputContainer', function () {
787830 MdInputContainerWithFormErrorMessages
788831 ] ,
789832 providers : [
790- { provide : MD_ERROR_GLOBAL_OPTIONS , useValue : { showOnDirty : true } }
833+ { provide : MD_ERROR_GLOBAL_OPTIONS , useClass : ShowOnDirtyErrorStateMatcher }
791834 ]
792835 } ) ;
793836
794- let customFixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
795- customFixture . detectChanges ( ) ;
837+ let fixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
838+ fixture . detectChanges ( ) ;
796839
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 ;
799842
800843 expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
801844 expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 0 , 'Expected no error messages' ) ;
802845
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 ( ( ) => {
822846 testComponent . formControl . markAsTouched ( ) ;
823847 fixture . detectChanges ( ) ;
824848
825849 fixture . whenStable ( ) . then ( ( ) => {
826- expect ( containerEl . classList )
827- . toContain ( 'mat-input-invalid' , 'Expected container to have the invalid CSS class.' ) ;
828850 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' ) ;
832852
833- testComponent . formControl . setValue ( 'something' ) ;
853+ testComponent . formControl . markAsDirty ( ) ;
834854 fixture . detectChanges ( ) ;
835855
836856 fixture . whenStable ( ) . then ( ( ) => {
837- expect ( containerEl . classList ) . not . toContain ( 'mat-input-invalid' ,
838- 'Expected container not to have the invalid class when valid.' ) ;
839857 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' ) ;
843859 } ) ;
844860 } ) ;
845- } ) ) ;
846-
847- it ( 'should not hide the hint if there are no error messages' , async ( ( ) => {
848- testComponent . renderError = false ;
849- fixture . detectChanges ( ) ;
850861
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- } ) ;
861862 } ) ) ;
862-
863863 } ) ;
864864
865865 it ( 'should not have prefix and suffix elements when none are specified' , ( ) => {
0 commit comments