Skip to content

Commit 5fa4045

Browse files
author
JupiterJS
committed
Merged pull request jupiterjs#25 from mowen/mowen.
Tests for validatesInclusionOf, validatesLengthOf, and validatesPresenceOf
2 parents 4e4bce6 + afd8007 commit 5fa4045

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

model/validations/qunit/validations_test.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,42 @@ test("validatesFormatOf", function(){
6161
});
6262

6363
test("validatesInclusionOf", function(){
64+
Person.validateInclusionOf("thing", ["yes", "no", "maybe"]);
65+
66+
ok(!new Person({thing: "yes"}).errors(),"no errors");
67+
68+
var errors = new Person({thing: "foobar"}).errors();
69+
70+
ok(errors, "there are errors");
71+
equals(errors.thing.length,1,"one error on thing");
72+
73+
equals(errors.thing[0],"is not a valid option (perhaps out of range)","basic message");
74+
75+
Person.validateInclusionOf("otherThing", ["yes", "no", "maybe"],{message: "not a valid option"});
6476

77+
var errors2 = new Person({thing: "yes", otherThing: "maybe not"}).errors();
6578

66-
})
79+
equals(errors2.otherThing[0],"not a valid option", "can supply a custom message");
80+
});
6781

6882
test("validatesLengthOf", function(){
83+
Person.validateLengthOf("thing", 2, 5);
84+
85+
ok(!new Person({thing: "yes"}).errors(),"no errors");
6986

70-
})
87+
var errors = new Person({thing: "foobar"}).errors();
88+
89+
ok(errors, "there are errors");
90+
equals(errors.thing.length,1,"one error on thing");
91+
92+
equals(errors.thing[0],"is too long (max=5)","basic message");
93+
94+
Person.validateLengthOf("otherThing", 2, 5, {message: "invalid length"});
95+
96+
var errors2 = new Person({thing: "yes", otherThing: "too long"}).errors();
97+
98+
equals(errors2.otherThing[0],"invalid length", "can supply a custom message");
99+
});
71100

72101
test("validatesPresenceOf", function(){
73102
$.Model.extend("Task",{
@@ -101,7 +130,22 @@ test("validatesPresenceOf", function(){
101130
})
102131

103132
test("validatesRangeOf", function(){
104-
105-
})
133+
Person.validateRangeOf("thing", 2, 5);
134+
135+
ok(!new Person({thing: 4}).errors(),"no errors");
136+
137+
var errors = new Person({thing: 6}).errors();
138+
139+
ok(errors, "there are errors")
140+
equals(errors.thing.length,1,"one error on thing");
141+
142+
equals(errors.thing[0],"is out of range [2,5]","basic message");
143+
144+
Person.validateRangeOf("otherThing", 2, 5, {message: "value out of range"});
145+
146+
var errors2 = new Person({thing: 4, otherThing: 6}).errors();
147+
148+
equals(errors2.otherThing[0],"value out of range", "can supply a custom message");
149+
});
106150

107151
})

0 commit comments

Comments
 (0)