@@ -24,6 +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' ;
2728
2829
2930describe ( 'MdInputContainer' , function ( ) {
@@ -738,6 +739,86 @@ describe('MdInputContainer', function () {
738739 } ) ;
739740 } ) ) ;
740741
742+ it ( 'should display an error message when global error matcher returns true' , ( ) => {
743+
744+ // Global error state matcher that will always cause errors to show
745+ function globalErrorStateMatcher ( ) {
746+ return true ;
747+ }
748+
749+ TestBed . resetTestingModule ( ) ;
750+ TestBed . configureTestingModule ( {
751+ imports : [
752+ FormsModule ,
753+ MdInputModule ,
754+ NoopAnimationsModule ,
755+ ReactiveFormsModule ,
756+ ] ,
757+ declarations : [
758+ MdInputContainerWithFormErrorMessages
759+ ] ,
760+ providers : [
761+ {
762+ provide : MD_ERROR_GLOBAL_OPTIONS ,
763+ useValue : { errorStateMatcher : globalErrorStateMatcher } }
764+ ]
765+ } ) ;
766+
767+ let customFixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
768+ customFixture . detectChanges ( ) ;
769+
770+ containerEl = customFixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
771+ testComponent = customFixture . componentInstance ;
772+
773+ // Expect the control to still be untouched but the error to show due to the global setting
774+ expect ( testComponent . formControl . untouched ) . toBe ( true , 'Expected untouched form control' ) ;
775+ expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 1 , 'Expected an error message' ) ;
776+ } ) ;
777+
778+ it ( 'should display an error message when global setting shows errors on dirty' , async ( ) => {
779+ TestBed . resetTestingModule ( ) ;
780+ TestBed . configureTestingModule ( {
781+ imports : [
782+ FormsModule ,
783+ MdInputModule ,
784+ NoopAnimationsModule ,
785+ ReactiveFormsModule ,
786+ ] ,
787+ declarations : [
788+ MdInputContainerWithFormErrorMessages
789+ ] ,
790+ providers : [
791+ { provide : MD_ERROR_GLOBAL_OPTIONS , useValue : { showOnDirty : true } }
792+ ]
793+ } ) ;
794+
795+ let customFixture = TestBed . createComponent ( MdInputContainerWithFormErrorMessages ) ;
796+ customFixture . detectChanges ( ) ;
797+
798+ containerEl = customFixture . debugElement . query ( By . css ( 'md-input-container' ) ) . nativeElement ;
799+ testComponent = customFixture . componentInstance ;
800+
801+ expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
802+ expect ( containerEl . querySelectorAll ( 'md-error' ) . length ) . toBe ( 0 , 'Expected no error messages' ) ;
803+
804+ testComponent . formControl . markAsTouched ( ) ;
805+ customFixture . detectChanges ( ) ;
806+
807+ customFixture . whenStable ( ) . then ( ( ) => {
808+ expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
809+ . toBe ( 0 , 'Expected no error messages when touched' ) ;
810+
811+ testComponent . formControl . markAsDirty ( ) ;
812+ customFixture . detectChanges ( ) ;
813+
814+ customFixture . whenStable ( ) . then ( ( ) => {
815+ expect ( containerEl . querySelectorAll ( 'md-error' ) . length )
816+ . toBe ( 1 , 'Expected one error message when dirty' ) ;
817+ } ) ;
818+ } ) ;
819+
820+ } ) ;
821+
741822 it ( 'should hide the errors and show the hints once the input becomes valid' , async ( ( ) => {
742823 testComponent . formControl . markAsTouched ( ) ;
743824 fixture . detectChanges ( ) ;
@@ -1069,7 +1150,7 @@ class MdInputContainerWithCustomErrorStateMatcher {
10691150 formControl = new FormControl ( '' , Validators . required ) ;
10701151 errorState = false ;
10711152
1072- customErrorStateMatcher ( c : NgControl ) : boolean {
1153+ customErrorStateMatcher ( ) : boolean {
10731154 return this . errorState ;
10741155 }
10751156}
0 commit comments