@@ -3,28 +3,38 @@ const { validate, email, length, required } = require('../validate')
33const defaultRules = {
44 fields : {
55 email : {
6- validator : ( value ) => {
6+ validator : value => {
77 return length ( value , { max : 75 } ) && email ( value )
88 } ,
99 message : 'The given email address is invalid' ,
1010 } ,
1111 password : {
12- validator : ( value ) => {
12+ validator : value => {
1313 return length ( value , { min : 4 , max : 18 } )
1414 } ,
1515 } ,
1616 subscription : {
17- validator : ( value ) => {
17+ validator : value => {
1818 return required ( value )
19- }
20- }
21- }
19+ } ,
20+ } ,
21+ } ,
2222}
2323
2424it ( 'initializes' , ( ) => {
2525 const fields = { }
2626 const rules = { }
27- validate ( fields , { rules, } )
27+ validate ( fields , { rules } )
28+ } )
29+
30+ it ( 'returns invalid when there are no rules' , ( ) => {
31+ const fields = {
32+ password : 'homepage'
33+ }
34+ const rules = { }
35+
36+ const { valid } = validate ( fields , { rules } )
37+ expect ( valid ) . toEqual ( false )
2838} )
2939
3040it ( 'calls the correct validation checks and calls them with the current value' , ( ) => {
@@ -65,10 +75,43 @@ it('uses the given rules to validate against the fields', () => {
6575 const fields = {
66766777 password : 'password' ,
68- subscription : true
78+ subscription : true ,
6979 }
7080
7181 const rules = defaultRules
72- const { valid } = validate ( fields , { rules, } )
82+ const { valid } = validate ( fields , { rules } )
7383 expect ( valid ) . toEqual ( true )
7484} )
85+
86+ it ( 'validates to false when one one of the validation rules returns false' , ( ) => {
87+ const fields = {
88+ 89+ password : 'password' ,
90+ }
91+
92+ const rules = defaultRules
93+ const { valid, results } = validate ( fields , { rules } )
94+ expect ( valid ) . toEqual ( false )
95+ expect ( results ) . toMatchInlineSnapshot ( `
96+ Array [
97+ Object {
98+ "field": "email",
99+ "message": "",
100+ "valid": true,
101+ 102+ },
103+ Object {
104+ "field": "password",
105+ "message": "",
106+ "valid": true,
107+ "value": "password",
108+ },
109+ Object {
110+ "field": "subscription",
111+ "message": "Validation failed for field 'subscription'",
112+ "valid": false,
113+ "value": undefined,
114+ },
115+ ]
116+ ` )
117+ } )
0 commit comments