forked from thedersen/backbone.validation
/
events.js
141 lines (119 loc) · 4.3 KB
/
events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
buster.testCase("Backbone.Validation events", {
setUp: function() {
var Model = Backbone.Model.extend({
validation: {
age: function(val){
if(!val) return 'age';
},
name: function(val){
if(!val) return 'name';
}
}
});
this.model = new Model();
this.view = new Backbone.View({
model: this.model
});
Backbone.Validation.bind(this.view);
},
"model is updated before the events are raised": function() {
this.model.bind('change', function(){
assert.equals(1, this.model.get('age'));
}, this);
this.model.bind('validated', function(){
assert.equals(1, this.model.get('age'));
}, this);
this.model.bind('validated:valid', function(){
assert.equals(1, this.model.get('age'));
}, this);
this.model.set({
age: 1,
name: 'name'
});
},
"when model is valid": {
"validated event is triggered with true and model": function(done) {
this.model.bind('validated', function(valid, model){
assert(valid);
assert.same(this.model, model);
done();
}, this);
this.model.set({
age: 1,
name: 'name'
});
},
"validated:valid event is triggered with model": function(done) {
this.model.bind('validated:valid', function(model){
assert.same(this.model, model);
done();
}, this);
this.model.set({
age: 1,
name: 'name'
});
}
},
"when one invalid value is set": {
"validated event is triggered with false, model, and name of attribute with error": function(done) {
this.model.bind('validated', function(valid, model, attr){
refute(valid);
assert.same(this.model, model);
assert.equals(['age'], attr);
done();
}, this);
this.model.set({age:0});
},
"validated:invalid event is triggered with model and name of attribute with error": function(done) {
this.model.bind('validated:invalid', function(model, attr){
assert.same(this.model, model);
assert.equals(['age'], attr);
done();
}, this);
this.model.set({age:0});
},
"error event is triggered with model and error as a string": function(done) {
this.model.bind('error', function(model, error) {
assert.same(this.model, model);
assert.equals('age', error);
done();
}, this);
this.model.set({age:0});
}
},
"when two invalid values is set": {
"validated event is triggered with false, model, and an array with the names of the attributes with error": function(done) {
this.model.bind('validated', function(valid, model, attrs){
refute(valid);
assert.same(this.model, model);
assert.equals(['age', 'name'], attrs);
done();
}, this);
this.model.set({
age: 0,
name: ''
});
},
"validated:invalid event is triggered with model and an array with the names of the attributes with error": function(done) {
this.model.bind('validated:invalid', function(model, attrs){
assert.same(this.model, model);
assert.equals(['age', 'name'], attrs);
done();
}, this);
this.model.set({
age: 0,
name: ''
}, this);
},
"error event is triggered with model and error as an array": function(done) {
this.model.bind('error', function(model, error) {
assert.equals(['age', 'name'], error);
done();
}, this);
this.model.set({
age: 0,
name: ''
});
}
}
});