@@ -64,6 +64,9 @@ describe('ControlErrorDirective', () => {
6464
6565 <input formControlName="username" placeholder="Username" />
6666
67+ <input formControlName="onSubmitOnly" placeholder="On submit only" [controlErrorsOnBlur]="false" />
68+ <input formControlName="onEveryChange" placeholder="On every change" [controlErrorsOnChange]="true" />
69+
6770 <button type="submit">Submit</button>
6871 </form>
6972 `
@@ -75,7 +78,9 @@ describe('ControlErrorDirective', () => {
7578 ignored : [ '' , Validators . required ] ,
7679 explicit : [ '' ] ,
7780 names : this . builder . array ( [ this . createName ( ) , this . createName ( ) ] , this . validator ) ,
78- username : [ '' , null , this . usernameValidator . bind ( this ) ]
81+ username : [ '' , null , this . usernameValidator . bind ( this ) ] ,
82+ onSubmitOnly : [ '' , [ Validators . required ] ] ,
83+ onEveryChange : [ '' , [ Validators . required ] ]
7984 } ) ;
8085
8186 @ViewChild ( 'explicitErrorTailor' , { static : true } ) explicitErrorTailor : ControlErrorsDirective ;
@@ -131,6 +136,11 @@ describe('ControlErrorDirective', () => {
131136 const oneNameInput = spectator . query < HTMLInputElement > ( byPlaceholder ( 'Name 0' ) ) ;
132137 const oneNameInput1 = spectator . query < HTMLInputElement > ( byPlaceholder ( 'Name 1' ) ) ;
133138
139+ const onSubmitOnly = spectator . query < HTMLInputElement > ( byPlaceholder ( 'On submit only' ) ) ;
140+ const onEveryChange = spectator . query < HTMLInputElement > ( byPlaceholder ( 'On every change' ) ) ;
141+ typeInElementAndFocusOut ( spectator , 'test' , onSubmitOnly ) ;
142+ typeInElementAndFocusOut ( spectator , 'test' , onEveryChange ) ;
143+
134144 spectator . click ( 'button' ) ;
135145
136146 expect ( spectator . query ( byText ( 'required one error' ) ) ) . toBeTruthy ( ) ;
@@ -142,6 +152,33 @@ describe('ControlErrorDirective', () => {
142152 expect ( spectator . query ( byText ( / e r r o r / ) ) ) . toBeNull ( ) ;
143153 } ) ;
144154
155+ it ( 'should show errors only on submit when controlErrorsOnBlur is disabled' , ( ) => {
156+ const onSubmitOnly = spectator . query < HTMLInputElement > ( byPlaceholder ( 'On submit only' ) ) ;
157+
158+ typeInElementAndFocusOut ( spectator , 'test' , onSubmitOnly ) ;
159+
160+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeFalsy ( ) ;
161+
162+ spectator . click ( 'button' ) ;
163+
164+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeTruthy ( ) ;
165+ } ) ;
166+
167+ it ( 'should show errors on every change when controlErrorsOnChange is enabled' , ( ) => {
168+ const onEveryChange = spectator . query < HTMLInputElement > ( byPlaceholder ( 'On every change' ) ) ;
169+
170+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeFalsy ( ) ;
171+
172+ spectator . typeInElement ( 't' , onEveryChange ) ;
173+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeFalsy ( ) ;
174+
175+ spectator . typeInElement ( '' , onEveryChange ) ;
176+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeTruthy ( ) ;
177+
178+ spectator . typeInElement ( 't' , onEveryChange ) ;
179+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeFalsy ( ) ;
180+ } ) ;
181+
145182 it ( 'should not show errors on interactions' , ( ) => {
146183 const ignoredInput = spectator . query < HTMLInputElement > ( byPlaceholder ( 'Ignored' ) ) ;
147184
@@ -340,8 +377,6 @@ describe('ControlErrorDirective', () => {
340377 <div controlErrorAnchor>
341378 <input formControlName="withParentAnchor" placeholder="With parent anchor" />
342379 </div>
343-
344- <input formControlName="onEveryChange" placeholder="On every change" [controlErrorsOnBlur]="false" />
345380 </form>
346381 `
347382 } )
@@ -351,8 +386,7 @@ describe('ControlErrorDirective', () => {
351386 customTemplate : [ '' , Validators . required ] ,
352387 customClass : [ '' , Validators . required ] ,
353388 withAnchor : [ '' , Validators . required ] ,
354- withParentAnchor : [ '' , Validators . required ] ,
355- onEveryChange : [ '' , [ Validators . required , Validators . minLength ( 3 ) ] ]
389+ withParentAnchor : [ '' , Validators . required ]
356390 } ) ;
357391
358392 customErrors = {
@@ -466,7 +500,10 @@ describe('ControlErrorDirective', () => {
466500 }
467501 } ,
468502 controlErrorComponent : CustomControlErrorComponent ,
469- controlErrorComponentAnchorFn : controlErrorComponentAnchorFn
503+ controlErrorComponentAnchorFn : controlErrorComponentAnchorFn ,
504+ controlErrorsOn : {
505+ change : true
506+ }
470507 } )
471508 ]
472509 } ) ;
@@ -532,5 +569,27 @@ describe('ControlErrorDirective', () => {
532569 expect ( anchorFnDestroyCalled ) . toBeTruthy ( ) ;
533570 } ) ;
534571 } ) ;
572+
573+ describe ( 'controlErrorsOn' , ( ) => {
574+ let spectator : Spectator < CustomErrorFormGroupComponent > ;
575+ const createComponent = getCustomErrorComponentFactory ( CustomErrorFormGroupComponent ) ;
576+
577+ beforeEach ( ( ) => ( spectator = createComponent ( ) ) ) ;
578+
579+ it ( 'should override default behavior for showing errors' , ( ) => {
580+ const input = spectator . query < HTMLInputElement > ( byPlaceholder ( 'Name' ) ) ;
581+
582+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeFalsy ( ) ;
583+
584+ spectator . typeInElement ( 'test' , input ) ;
585+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeFalsy ( ) ;
586+
587+ spectator . typeInElement ( '' , input ) ;
588+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeTruthy ( ) ;
589+
590+ spectator . typeInElement ( 't' , input ) ;
591+ expect ( spectator . query ( byText ( 'required error' ) ) ) . toBeFalsy ( ) ;
592+ } ) ;
593+ } ) ;
535594 } ) ;
536595} ) ;
0 commit comments