@@ -36,6 +36,73 @@ module.exports = {
3636 }
3737 } ,
3838
39+ 'Defining a custom validator as a string' : {
40+ beforeEach : function ( ) {
41+ var Model = Backbone . Model . extend ( {
42+ validation : {
43+ age : 'validateAge'
44+ } ,
45+
46+ validateAge ( value , attr , computedState ) {
47+ if ( value != 1 ) return 'Age invalid'
48+ }
49+ } ) ;
50+ this . model = new Model ( ) ;
51+ _ . extend ( this . model , Backbone . Validation . mixin ) ;
52+ this . validateAgeSpy = sinon . spy ( this . model , 'validateAge' ) ;
53+ } ,
54+
55+ 'should execute corresponding method in model' : function ( ) {
56+ assert ( this . model . set ( {
57+ age : 1
58+ } , { validate : true } ) ) ;
59+ sinon . assert . calledOnce ( this . validateAgeSpy ) ;
60+ assert ( this . model . set ( {
61+ age : '1'
62+ } , { validate : true } ) ) ;
63+ sinon . assert . calledTwice ( this . validateAgeSpy ) ;
64+ refute ( this . model . set ( {
65+ age : 2
66+ } , { validate : true } ) ) ;
67+ sinon . assert . calledThrice ( this . validateAgeSpy ) ;
68+ }
69+ } ,
70+
71+ 'Defining a custom validator as a string array' : {
72+ beforeEach : function ( ) {
73+ var Model = Backbone . Model . extend ( {
74+ validation : {
75+ age : [ 'validateAge' , 'validateNumber' ]
76+ } ,
77+
78+ validateAge ( value , attr , computedState ) {
79+ if ( value != 1 ) return 'Age invalid'
80+ } ,
81+
82+ validateNumber ( value , attr , computedState ) {
83+ if ( typeof value !== 'number' ) return 'Not a number'
84+ }
85+ } ) ;
86+ this . model = new Model ( ) ;
87+ _ . extend ( this . model , Backbone . Validation . mixin ) ;
88+ this . validateAgeSpy = sinon . spy ( this . model , 'validateAge' ) ;
89+ this . validateNumberSpy = sinon . spy ( this . model , 'validateNumber' ) ;
90+ } ,
91+
92+ 'should use corresponding methods in model' : function ( ) {
93+ assert ( this . model . set ( {
94+ age : 1
95+ } , { validate : true } ) ) ;
96+ sinon . assert . calledOnce ( this . validateAgeSpy ) ;
97+ sinon . assert . calledOnce ( this . validateNumberSpy ) ;
98+ refute ( this . model . set ( {
99+ age : '1'
100+ } , { validate : true } ) ) ;
101+ sinon . assert . calledTwice ( this . validateAgeSpy ) ;
102+ sinon . assert . calledTwice ( this . validateNumberSpy ) ;
103+ }
104+ } ,
105+
39106 'Overriding built-in validator in Backbone.Validation' : {
40107 beforeEach : function ( ) {
41108 this . builtinMin = Backbone . Validation . validators . min ;
0 commit comments