1
1
import { expect } from 'chai' ;
2
- import { validateProcess } from '../index' ;
2
+ import { interpolateErrors , validateProcess } from '../index' ;
3
3
import { rules } from "../rules" ;
4
4
import { simpleForm } from './fixtures/forms' ;
5
5
import { ProcessorType , ValidationScope } from 'types' ;
6
+ import { FieldError , InterpolateErrorFn } from 'error' ;
6
7
import get from 'lodash/get' ;
7
8
8
9
it ( 'Validator will throw the correct errors given a flat components array' , async ( ) => {
@@ -38,10 +39,16 @@ it('Validator will throw the correct errors given a flat components array', asyn
38
39
} ) ;
39
40
40
41
it ( 'Validation errors (FieldErrors) should include the rule name mapping in the "validator" param' , async ( ) => {
41
- let errors : string [ ] = [ ] ;
42
42
const data = {
43
- requiredField : ''
43
+ requiredField : '' ,
44
+ maximumWords : 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' ,
45
+ minimumWords : 'Hello' ,
46
+ email : 'brendanb' ,
47
+ url : 'htpigoogle' ,
48
+ inputMask : 'hello, world' ,
49
+ submit : false ,
44
50
} ;
51
+ const result : Map < string , ReturnType < typeof interpolateErrors > > = new Map ( ) ;
45
52
for ( let component of simpleForm . components ) {
46
53
const path = component . key ;
47
54
const scope : ValidationScope = { errors : [ ] } ;
@@ -55,9 +62,18 @@ it('Validation errors (FieldErrors) should include the rule name mapping in the
55
62
processor : ProcessorType . Validate ,
56
63
rules
57
64
} ) ;
58
- if ( scope . errors ) {
59
- errors = [ ...errors , ...scope . errors . map ( ( error ) => error . errorKeyOrMessage ) ] ;
60
- }
65
+ result . set ( path , interpolateErrors ( scope . errors ) ) ;
61
66
}
62
- console . log ( errors ) ;
67
+ expect ( result . get ( 'requiredField' ) ) . to . have . length ( 1 ) ;
68
+ expect ( result . get ( 'requiredField' ) ! [ 0 ] . context . validator ) . to . equal ( 'required' ) ;
69
+ expect ( result . get ( 'maximumWords' ) ) . to . have . length ( 1 ) ;
70
+ expect ( result . get ( 'maximumWords' ) ! [ 0 ] . context . validator ) . to . equal ( 'maxWords' ) ;
71
+ expect ( result . get ( 'minimumWords' ) ) . to . have . length ( 1 ) ;
72
+ expect ( result . get ( 'minimumWords' ) ! [ 0 ] . context . validator ) . to . equal ( 'minWords' ) ;
73
+ expect ( result . get ( 'email' ) ) . to . have . length ( 1 ) ;
74
+ expect ( result . get ( 'email' ) ! [ 0 ] . context . validator ) . to . equal ( 'email' ) ;
75
+ expect ( result . get ( 'url' ) ) . to . have . length ( 1 ) ;
76
+ expect ( result . get ( 'url' ) ! [ 0 ] . context . validator ) . to . equal ( 'url' ) ;
77
+ expect ( result . get ( 'inputMask' ) ) . to . have . length ( 1 ) ;
78
+ expect ( result . get ( 'inputMask' ) ! [ 0 ] . context . validator ) . to . equal ( 'inputMask' ) ;
63
79
} ) ;
0 commit comments