Skip to content

Instantly share code, notes, and snippets.

@eastridge
Created August 14, 2012 19:19
Show Gist options
  • Save eastridge/3351929 to your computer and use it in GitHub Desktop.
Save eastridge/3351929 to your computer and use it in GitHub Desktop.
Thorax validation tests
test("validations", function() {
var lastErrors;
var regexValidationViewErrorCount = 0;
var regexValidationView = new Application.View({
events: {
error: function(errors) {
++regexValidationViewErrorCount;
lastErrors = errors;
}
},
template: '<form><input name="a" data-validate-regex="^a"></form>'
});
regexValidationView.render();
regexValidationView.populate({
a: 'a'
});
equal(regexValidationView.serialize().a, 'a');
equal(regexValidationViewErrorCount, 0);
regexValidationView.populate({
a: 'b'
});
ok(!regexValidationView.serialize());
equal(regexValidationViewErrorCount, 1);
equal(lastErrors[0].label, 'a');
var methodValidationView = new Application.View({
events: {
error: function(errors) {
lastErrors = errors;
}
},
template: '<form><label for="a">label<input id="a" name="a" data-validate-method="myMethod"></label></form>',
myMethod: function(value) {
return value !== 'a' ? 'test {{label}}' : true;
}
});
methodValidationView.render();
methodValidationView.populate({
a: 'a'
});
ok(methodValidationView.serialize());
methodValidationView.populate({
a: 'b'
});
ok(!methodValidationView.serialize());
equal(lastErrors[0].message, 'test label');
methodValidationView.populate({
a: 'a'
});
ok(methodValidationView.serialize());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment