-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathrangeLength.js
More file actions
160 lines (136 loc) · 4.28 KB
/
Copy pathrangeLength.js
File metadata and controls
160 lines (136 loc) · 4.28 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
buster.testCase("rangeLength validator", {
setUp: function() {
var that = this;
var Model = Backbone.Model.extend({
validation: {
name: {
rangeLength: [2, 4]
}
}
});
this.model = new Model();
this.view = new Backbone.View({
model: this.model
});
Backbone.Validation.bind(this.view, {
valid: this.spy(),
invalid: this.spy()
});
},
"has default error message for strings": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must be between 2 and 4 characters'}, error);
done();
});
this.model.set({name:'a'}, {validate: true});
},
"has default error message for arrays": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must contain between 2 and 4 elements'}, error);
done();
});
this.model.set({name:['a']}, {validate: true});
},
"string with length shorter than first value is invalid": function() {
refute(this.model.set({
name: 'a'
}, {validate: true}));
},
"string with length equal to first value is valid": function() {
assert(this.model.set({
name: 'aa'
}, {validate: true}));
},
"string with length longer than last value is invalid": function() {
refute(this.model.set({
name: 'aaaaa'
}, {validate: true}));
},
"string with length equal to last value is valid": function() {
assert(this.model.set({
name: 'aaaa'
}, {validate: true}));
},
"string with length within range is valid": function() {
assert(this.model.set({
name: 'aaa'
}, {validate: true}));
},
"array with length shorter than first value is invalid": function() {
refute(this.model.set({
name: ['a']
}, {validate: true}));
},
"array with length equal to first value is valid": function() {
assert(this.model.set({
name: ['a','a']
}, {validate: true}));
},
"array with length longer than last value is invalid": function() {
refute(this.model.set({
name: ['a', 'a', 'a', 'a', 'a']
}, {validate: true}));
},
"array with length equal to last value is valid": function() {
assert(this.model.set({
name: ['a', 'a', 'a', 'a']
}, {validate: true}));
},
"array with length within range is valid": function() {
assert(this.model.set({
name: ['a', 'a', 'a']
}, {validate: true}));
},
"spaces are treated as part of the string (no trimming)": function() {
refute(this.model.set({
name: 'aaaa '
}, {validate: true}));
},
"non strings are treated as an error": function() {
refute(this.model.set({
name: 123
}, {validate: true}));
},
"when required is not specified": {
"undefined is invalid": function() {
refute(this.model.set({
name: undefined
}, {validate: true}));
},
"null is invalid": function() {
refute(this.model.set({
name: null
}, {validate: true}));
}
},
"when required:false": {
setUp: function() {
this.model.validation.name.required = false;
},
"null is valid": function() {
assert(this.model.set({
name: null
}, {validate: true}));
},
"undefined is valid": function() {
assert(this.model.set({
name: undefined
}, {validate: true}));
}
},
"when required:true": {
setUp: function() {
this.model.validation.name.required = true;
},
"undefined is invalid": function() {
refute(this.model.set({
name: undefined
}, {validate: true}));
},
"null is invalid": function() {
refute(this.model.set({
name: null
}, {validate: true}));
}
}
});