@@ -6,6 +6,8 @@ import { Component } from '@angular/core';
66import { ValidationMessageComponent } from '../validation-message/validation-message.component' ;
77import { Validators } from '../validators' ;
88import { Subject } from 'rxjs/Subject' ;
9+ import { ReactiveValidationModule } from '../reactive-validation.module' ;
10+ import { ReactiveValidationModuleConfiguration } from '../public_api' ;
911
1012describe ( 'ValidationMessagesComponent' , ( ) => {
1113 describe ( 'properties and functions' , ( ) => {
@@ -62,22 +64,22 @@ describe('ValidationMessagesComponent', () => {
6264 component . for = [ firstNameControl , 'middleName' , lastNameControl ] ;
6365 } ) ;
6466
65- it ( `isValid returns true when there are no controls with ValidationErrors and they are touched` , ( ) => {
67+ it ( `isValid returns true when there are no controls with ValidationErrors and they are touched (default configuration) ` , ( ) => {
6668 component . for = firstNameControl ;
6769 firstNameControl . setValue ( 'firstName' ) ;
6870 firstNameControl . markAsTouched ( ) ;
6971
7072 expect ( component . isValid ( ) ) . toEqual ( true ) ;
7173 } ) ;
7274
73- it ( `isValid returns false when there are controls with ValidationErrors and they are touched` , ( ) => {
75+ it ( `isValid returns false when there are controls with ValidationErrors and they are touched (default configuration) ` , ( ) => {
7476 component . for = [ firstNameControl ] ;
7577 firstNameControl . markAsTouched ( ) ;
7678
7779 expect ( component . isValid ( ) ) . toEqual ( false ) ;
7880 } ) ;
7981
80- it ( `getErrorMessages returns the first error message per touched control` , ( ) => {
82+ it ( `getErrorMessages returns the first error message per touched control (default configuration) ` , ( ) => {
8183 component . for = [ firstNameControl , middleNameControl , lastNameControl ] ;
8284 firstNameControl . markAsTouched ( ) ;
8385 // We skip middleNameControl on purpose, to ensure that it doesn't return it's error.
@@ -88,39 +90,72 @@ describe('ValidationMessagesComponent', () => {
8890 } ) ;
8991 } ) ;
9092
91- describe ( 'validation is shown when' , ( ) => {
92- let submittedSubject : Subject < { } > ;
93+ describe ( 'when in' , ( ) => {
9394 let fixture : ComponentFixture < TestHostComponent > ;
9495
95- beforeEach ( ( ) => {
96- submittedSubject = new Subject < { } > ( ) ;
97- TestBed . configureTestingModule ( {
98- imports : [ ReactiveFormsModule ] ,
99- declarations : [ ValidationMessagesComponent , ValidationMessageComponent , TestHostComponent ] ,
100- providers : [ {
101- provide : FormDirective , useValue : {
102- submitted : submittedSubject
103- }
104- } ]
96+ describe ( 'the default configuration validation is shown when' , ( ) => {
97+ let submittedSubject : Subject < { } > ;
98+
99+ beforeEach ( ( ) => {
100+ submittedSubject = new Subject < { } > ( ) ;
101+ TestBed . configureTestingModule ( {
102+ imports : [ ReactiveFormsModule ] ,
103+ declarations : [ ValidationMessagesComponent , ValidationMessageComponent , TestHostComponent ] ,
104+ providers : [ {
105+ provide : FormDirective , useValue : {
106+ submitted : submittedSubject
107+ }
108+ } ]
109+ } ) ;
110+
111+ fixture = TestBed . createComponent ( TestHostComponent ) ;
112+ fixture . detectChanges ( ) ;
105113 } ) ;
106114
107- fixture = TestBed . createComponent ( TestHostComponent ) ;
108- fixture . detectChanges ( ) ;
109- } ) ;
115+ it ( `the associated control is touched` , ( ) => {
116+ fixture . componentInstance . firstNameControl . markAsTouched ( ) ;
117+ fixture . componentInstance . lastNameControl . markAsTouched ( ) ;
118+ fixture . detectChanges ( ) ;
119+
120+ expectValidationIsShown ( ) ;
121+ } ) ;
110122
111- it ( `the associated control is touched` , ( ) => {
112- fixture . componentInstance . firstNameControl . markAsTouched ( ) ;
113- fixture . componentInstance . lastNameControl . markAsTouched ( ) ;
114- fixture . detectChanges ( ) ;
123+ it ( `the form has been submitted` , ( ) => {
124+ submittedSubject . next ( ) ;
125+ fixture . detectChanges ( ) ;
115126
116- expectValidationIsShown ( ) ;
127+ expectValidationIsShown ( ) ;
128+ } ) ;
117129 } ) ;
118130
119- it ( `the form has been submitted` , ( ) => {
120- submittedSubject . next ( ) ;
121- fixture . detectChanges ( ) ;
131+ describe ( 'an alternative configuration' , ( ) => {
132+ const configuration : ReactiveValidationModuleConfiguration = {
133+ displayValidationMessageWhen : ( control , formSubmitted ) => true
134+ } ;
135+
136+ beforeEach ( ( ) => {
137+ spyOn ( configuration , 'displayValidationMessageWhen' ) . and . callThrough ( ) ;
122138
123- expectValidationIsShown ( ) ;
139+ TestBed . configureTestingModule ( {
140+ imports : [ ReactiveFormsModule , ReactiveValidationModule . forRoot ( configuration ) ] ,
141+ declarations : [ TestHostComponent ] ,
142+ } ) ;
143+
144+ fixture = TestBed . createComponent ( TestHostComponent ) ;
145+ } ) ;
146+
147+ it ( 'validation is shown when displayValidationMessageWhen returns true' , ( ) => {
148+ expect ( configuration . displayValidationMessageWhen ) . not . toHaveBeenCalled ( ) ;
149+ fixture . detectChanges ( ) ;
150+ expect ( configuration . displayValidationMessageWhen ) . toHaveBeenCalled ( ) ;
151+
152+ expectValidationIsShown ( ) ;
153+ } ) ;
154+
155+ it ( `displayValidationMessageWhen's formSubmitted is undefined when a FormDirective is not provided` , ( ) => {
156+ fixture . detectChanges ( ) ;
157+ expect ( configuration . displayValidationMessageWhen ) . toHaveBeenCalledWith ( jasmine . any ( FormControl ) , undefined ) ;
158+ } ) ;
124159 } ) ;
125160
126161 function expectValidationIsShown ( ) {
0 commit comments