11import { TestBed , inject } from "@angular/core/testing" ;
2- import { ReactiveFormsModule , FormControl , NG_VALIDATORS , NG_ASYNC_VALIDATORS , ValidationErrors } from "@angular/forms" ;
2+ import { ReactiveFormsModule , FormControl , NG_VALIDATORS , NG_ASYNC_VALIDATORS , ValidationErrors , Validators } from "@angular/forms" ;
33import { DynamicFormValidationService } from "./dynamic-form-validation.service" ;
44import { DYNAMIC_VALIDATORS , Validator , ValidatorFactory } from "./dynamic-form-validators" ;
55import { DynamicFormControlModel } from "../model/dynamic-form-control.model" ;
@@ -70,35 +70,35 @@ describe("DynamicFormValidationService test suite", () => {
7070
7171 it ( "should resolve validators from config" , ( ) => {
7272
73- let config : any = { required : null , maxLength : 7 , minLength : 3 } ,
74- validators = service . getValidators ( config ) ;
73+ const config : any = { required : null , maxLength : 7 , minLength : 3 } ;
74+ const validators = service . getValidators ( config ) ;
7575
7676 expect ( validators . length === Object . keys ( config ) . length ) . toBe ( true ) ;
7777 } ) ;
7878
7979
8080 it ( "should resolve custom validators from config" , ( ) => {
8181
82- let config : any = { required : null , maxLength : 7 , testValidator : null , testValidatorFactory : "test" } ,
83- validators = service . getValidators ( config ) ;
82+ const config : any = { required : null , maxLength : 7 , testValidator : null , testValidatorFactory : "test" } ;
83+ const validators = service . getValidators ( config ) ;
8484
8585 expect ( validators . length === Object . keys ( config ) . length ) . toBe ( true ) ;
8686 } ) ;
8787
8888
8989 it ( "should resolve custom validators from detailed config" , ( ) => {
9090
91- let config : any = { testValidator : { name : testValidator . name , args : null } } ,
92- validators = service . getValidators ( config ) ;
91+ const config : any = { testValidator : { name : testValidator . name , args : null } } ;
92+ const validators = service . getValidators ( config ) ;
9393
9494 expect ( validators . length === Object . keys ( config ) . length ) . toBe ( true ) ;
9595 } ) ;
9696
9797
9898 it ( "should resolve custom async validators from config" , ( ) => {
9999
100- let config : any = { testAsyncValidator : null } ,
101- validators = service . getAsyncValidators ( config ) ;
100+ const config : any = { testAsyncValidator : null } ;
101+ const validators = service . getAsyncValidators ( config ) ;
102102
103103 expect ( validators . length === Object . keys ( config ) . length ) . toBe ( true ) ;
104104 } ) ;
@@ -113,60 +113,61 @@ describe("DynamicFormValidationService test suite", () => {
113113
114114 it ( "should update validators on control and model" , ( ) => {
115115
116- let config : any = { testValidator : null } ,
117- control : FormControl = new FormControl ( ) ,
118- model : DynamicFormControlModel = new DynamicInputModel ( { id : "input" } ) ;
116+ const config : any = { testValidator : null } ;
117+ const control : FormControl = new FormControl ( ) ;
118+ const model : DynamicFormControlModel = new DynamicInputModel ( { id : "input" } ) ;
119119
120- expect ( control [ " validator" ] ) . toBeNull ( ) ;
120+ expect ( control . validator ) . toBeNull ( ) ;
121121 expect ( model . validators ) . toBeNull ( ) ;
122122
123123 service . updateValidators ( config , control , model ) ;
124124
125- expect ( isFunction ( control [ " validator" ] ) ) . toBe ( true ) ;
125+ expect ( isFunction ( control . validator ) ) . toBe ( true ) ;
126126 expect ( ( model . validators as object ) . hasOwnProperty ( "testValidator" ) ) . toBe ( true ) ;
127127
128128 service . updateValidators ( null , control , model ) ;
129129
130- expect ( control [ " validator" ] ) . toBeNull ( ) ;
130+ expect ( control . validator ) . toBeNull ( ) ;
131131 expect ( model . validators ) . toBeNull ( ) ;
132132 } ) ;
133133
134134
135135 it ( "should update async validators on control and model" , ( ) => {
136136
137- let config : any = { testAsyncValidator : null } ,
138- control : FormControl = new FormControl ( ) ,
139- model : DynamicFormControlModel = new DynamicInputModel ( { id : "input" } ) ;
137+ const config : any = { testAsyncValidator : null } ;
138+ const control : FormControl = new FormControl ( ) ;
139+ const model : DynamicFormControlModel = new DynamicInputModel ( { id : "input" } ) ;
140140
141- expect ( control [ " asyncValidator" ] ) . toBeNull ( ) ;
141+ expect ( control . asyncValidator ) . toBeNull ( ) ;
142142 expect ( model . asyncValidators ) . toBeNull ( ) ;
143143
144144 service . updateAsyncValidators ( config , control , model ) ;
145145
146- expect ( isFunction ( control [ " asyncValidator" ] ) ) . toBe ( true ) ;
146+ expect ( isFunction ( control . asyncValidator ) ) . toBe ( true ) ;
147147 expect ( ( model . asyncValidators as object ) . hasOwnProperty ( "testAsyncValidator" ) ) . toBe ( true ) ;
148148
149149 service . updateAsyncValidators ( null , control , model ) ;
150150
151- expect ( control [ " asyncValidator" ] ) . toBeNull ( ) ;
151+ expect ( control . asyncValidator ) . toBeNull ( ) ;
152152 expect ( model . asyncValidators ) . toBeNull ( ) ;
153153 } ) ;
154154
155155
156156 it ( "should create error messages" , ( ) => {
157157
158- let errorMessages ,
159- testControl : FormControl = new FormControl ( ) ,
160- testModel : DynamicFormControlModel = new DynamicInputModel ( {
161- id : "testModel" ,
162- minLength : 5 ,
163- errorMessages : {
164- required : "Field is required" ,
165- minLength : "Field must contain at least {{ minLength }} characters" ,
166- custom1 : "Field {{ id }} has a custom error" ,
167- custom2 : "Field has a custom error: {{ validator.param }}"
168- }
169- } ) ;
158+ const testControl : FormControl = new FormControl ( ) ;
159+ const testModel : DynamicFormControlModel = new DynamicInputModel ( {
160+ id : "testModel" ,
161+ minLength : 5 ,
162+ errorMessages : {
163+ required : "Field is required" ,
164+ minLength : "Field must contain at least {{ minLength }} characters" ,
165+ custom1 : "Field {{ id }} has a custom error" ,
166+ custom2 : "Field has a custom error: {{ validator.param }}"
167+ }
168+ } ) ;
169+
170+ let errorMessages ;
170171
171172 errorMessages = service . createErrorMessages ( testControl , testModel ) ;
172173 expect ( errorMessages . length ) . toBe ( 0 ) ;
@@ -175,7 +176,7 @@ describe("DynamicFormValidationService test suite", () => {
175176
176177 errorMessages = service . createErrorMessages ( testControl , testModel ) ;
177178 expect ( errorMessages . length ) . toBe ( 2 ) ;
178- expect ( errorMessages [ 0 ] ) . toEqual ( ( testModel . errorMessages as any ) [ " required" ] ) ;
179+ expect ( errorMessages [ 0 ] ) . toEqual ( ( testModel . errorMessages as any ) . required ) ;
179180 expect ( errorMessages [ 1 ] ) . toEqual ( "Field must contain at least 5 characters" ) ;
180181
181182 testControl . setErrors ( { custom1 : true } ) ;
@@ -192,6 +193,28 @@ describe("DynamicFormValidationService test suite", () => {
192193 } ) ;
193194
194195
196+ it ( "should check if error messages should be shown" , ( ) => {
197+
198+ const control : FormControl = new FormControl ( ) ;
199+ const model : DynamicFormControlModel = new DynamicInputModel ( {
200+ id : "testModel" ,
201+ errorMessages : {
202+ required : "Field is required"
203+ }
204+ } ) ;
205+
206+ expect ( service . showErrorMessages ( control , model , false ) ) . toBe ( false ) ;
207+ expect ( service . showErrorMessages ( control , model , true ) ) . toBe ( false ) ;
208+
209+ control . markAsTouched ( ) ;
210+ control . setValidators ( Validators . required ) ;
211+ control . updateValueAndValidity ( ) ;
212+
213+ expect ( service . showErrorMessages ( control , model , true ) ) . toBe ( false ) ;
214+ expect ( service . showErrorMessages ( control , model , false ) ) . toBe ( true ) ;
215+ } ) ;
216+
217+
195218 it ( "should check form hooks" , ( ) => {
196219
197220 expect ( service . isFormHook ( null ) ) . toBe ( false ) ;
0 commit comments