11import { TestBed , inject } from "@angular/core/testing" ;
22import { ReactiveFormsModule , FormControl , NG_VALIDATORS , NG_ASYNC_VALIDATORS , ValidationErrors , Validators } from "@angular/forms" ;
33import { DynamicFormValidationService } from "./dynamic-form-validation.service" ;
4- import { DYNAMIC_VALIDATORS , Validator , ValidatorFactory } from "./dynamic-form-validators" ;
4+ import {
5+ DYNAMIC_GLOBAL_ERROR_MESSAGES , DYNAMIC_VALIDATORS ,
6+ Validator , ValidatorErrorMessageFn ,
7+ ValidatorFactory ,
8+ } from "./dynamic-form-validators" ;
59import { DynamicFormControlModel } from "../model/dynamic-form-control.model" ;
610import { DynamicInputModel } from "../model/input/dynamic-input.model" ;
711import { isFunction } from "../utils/core.utils" ;
@@ -45,6 +49,14 @@ describe("DynamicFormValidationService test suite", () => {
4549 useValue : new Map < string , Validator | ValidatorFactory > ( [
4650 [ "testValidatorFactory" , testValidatorFactory ]
4751 ] )
52+ } ,
53+ {
54+ provide : DYNAMIC_GLOBAL_ERROR_MESSAGES ,
55+ useValue : new Map < string , string | ValidatorErrorMessageFn > ( [
56+ [ 'testDynamicError' , 'this is a test' ] ,
57+ [ 'testFunc' , ( model : DynamicFormControlModel , error : string ) => error ] ,
58+ [ '*' , 'this is a catch-all' ] ,
59+ ] ) ,
4860 }
4961 ]
5062 } ) ;
@@ -163,7 +175,9 @@ describe("DynamicFormValidationService test suite", () => {
163175 required : "Field is required" ,
164176 minLength : "Field must contain at least {{ minLength }} characters" ,
165177 custom1 : "Field {{ id }} has a custom error" ,
166- custom2 : "Field has a custom error: {{ validator.param }}"
178+ custom2 : "Field has a custom error: {{ validator.param }}" ,
179+ customFunc : ( model : DynamicFormControlModel , error : string ) => error ,
180+ '*' : 'catch-all' ,
167181 }
168182 } ) ;
169183
@@ -190,6 +204,18 @@ describe("DynamicFormValidationService test suite", () => {
190204 errorMessages = service . createErrorMessages ( testControl , testModel ) ;
191205 expect ( errorMessages . length ) . toBe ( 1 ) ;
192206 expect ( errorMessages [ 0 ] ) . toEqual ( "Field has a custom error: 42" ) ;
207+
208+ testControl . setErrors ( { customFunc : 'error message' } ) ;
209+
210+ errorMessages = service . createErrorMessages ( testControl , testModel ) ;
211+ expect ( errorMessages . length ) . toBe ( 1 ) ;
212+ expect ( errorMessages [ 0 ] ) . toEqual ( "error message" ) ;
213+
214+ testControl . setErrors ( { unknownToken : true } ) ;
215+
216+ errorMessages = service . createErrorMessages ( testControl , testModel ) ;
217+ expect ( errorMessages . length ) . toBe ( 1 ) ;
218+ expect ( errorMessages [ 0 ] ) . toEqual ( "catch-all" ) ;
193219 } ) ;
194220
195221
@@ -223,4 +249,70 @@ describe("DynamicFormValidationService test suite", () => {
223249 expect ( service . isFormHook ( "change" ) ) . toBe ( true ) ;
224250 expect ( service . isFormHook ( "submit" ) ) . toBe ( true ) ;
225251 } ) ;
252+
253+ it ( "can create global error messages" , ( ) => {
254+ inject ( [ DynamicFormValidationService ] ,
255+ ( validationService : DynamicFormValidationService ) => {
256+ const testControl : FormControl = new FormControl ( ) ;
257+ const testModel : DynamicFormControlModel = new DynamicInputModel ( {
258+ id : "testModel" ,
259+ minLength : 5 ,
260+ } ) ;
261+
262+ let errorMessages ;
263+
264+ errorMessages = validationService . createErrorMessages ( testControl , testModel ) ;
265+ expect ( errorMessages . length ) . toBe ( 0 ) ;
266+
267+ testControl . setErrors ( { testDynamicError : true } ) ;
268+
269+ errorMessages = validationService . createErrorMessages ( testControl , testModel ) ;
270+ expect ( errorMessages . length ) . toBe ( 1 ) ;
271+ expect ( errorMessages [ 0 ] ) . toEqual ( "this is a test" ) ;
272+ } ) ;
273+ } ) ;
274+
275+ it ( "error messages can be functions" , ( ) => {
276+ inject ( [ DynamicFormValidationService ] ,
277+ ( validationService : DynamicFormValidationService ) => {
278+ const testControl : FormControl = new FormControl ( ) ;
279+ const testModel : DynamicFormControlModel = new DynamicInputModel ( {
280+ id : "testModel" ,
281+ minLength : 5 ,
282+ } ) ;
283+
284+ let errorMessages ;
285+
286+ errorMessages = validationService . createErrorMessages ( testControl , testModel ) ;
287+ expect ( errorMessages . length ) . toBe ( 0 ) ;
288+
289+ testControl . setErrors ( { testFunc : 'this should echo' } ) ;
290+
291+ errorMessages = validationService . createErrorMessages ( testControl , testModel ) ;
292+ expect ( errorMessages . length ) . toBe ( 1 ) ;
293+ expect ( errorMessages [ 0 ] ) . toEqual ( "this should echo" ) ;
294+ } ) ;
295+ } ) ;
296+
297+ it ( "error messages can be catch-alls" , ( ) => {
298+ inject ( [ DynamicFormValidationService ] ,
299+ ( validationService : DynamicFormValidationService ) => {
300+ const testControl : FormControl = new FormControl ( ) ;
301+ const testModel : DynamicFormControlModel = new DynamicInputModel ( {
302+ id : "testModel" ,
303+ minLength : 5 ,
304+ } ) ;
305+
306+ let errorMessages ;
307+
308+ errorMessages = service . createErrorMessages ( testControl , testModel ) ;
309+ expect ( errorMessages . length ) . toBe ( 0 ) ;
310+
311+ testControl . setErrors ( { unknown : 'this should not echo' } ) ;
312+
313+ errorMessages = service . createErrorMessages ( testControl , testModel ) ;
314+ expect ( errorMessages . length ) . toBe ( 1 ) ;
315+ expect ( errorMessages [ 0 ] ) . toEqual ( "this is a catch-all" ) ;
316+ } ) ;
317+ } ) ;
226318} ) ;
0 commit comments