Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added more tests for nested validation

  • Loading branch information...
commit 7446eecc93e7a1afaf4163cf5563ae14d3e927f0 1 parent becdd9c
@thedersen authored
Showing with 252 additions and 61 deletions.
  1. +252 −61 tests/nestedValidation.js
View
313 tests/nestedValidation.js
@@ -1,82 +1,273 @@
buster.testCase('Nested validation', {
- setUp: function() {
- this.valid = this.spy();
- this.invalid = this.spy();
-
- var Model = Backbone.Model.extend({
- validation: {
- 'address.street': {
- required: true
+ "one level": {
+ setUp: function() {
+ this.valid = this.spy();
+ this.invalid = this.spy();
+
+ var Model = Backbone.Model.extend({
+ validation: {
+ 'address.street': {
+ required: true,
+ msg: 'error'
+ }
}
+ });
+
+ this.model = new Model();
+ this.view = new Backbone.View({model: this.model});
+
+ Backbone.Validation.bind(this.view, {
+ invalid: this.invalid,
+ valid: this.valid
+ });
+ },
+
+ "invalid": {
+ setUp: function () {
+ this.result = this.model.set({
+ address: {
+ street:''
+ }
+ });
+ },
+
+ "refutes setting invalid values": function() {
+ refute(this.result);
+ },
+
+ "calls the invalid callback": function() {
+ assert.calledWith(this.invalid, this.view, 'address.street', 'error');
+ },
+
+ "is valid returns false for the specified attribute name": function () {
+ refute(this.model.isValid('address.street'));
+ },
+
+ "is valid returns false for the specified attribute names": function () {
+ refute(this.model.isValid(['address.street', 'address.street']));
+ },
+
+ "pre validate returns error message for the specified attribute name": function () {
+ assert(this.model.preValidate('address.street', ''));
}
- });
-
- this.model = new Model();
- this.view = new Backbone.View({model: this.model});
-
- Backbone.Validation.bind(this.view, {
- invalid: this.invalid,
- valid: this.valid
- });
+ },
+
+ "valid": {
+ setUp: function () {
+ this.result = this.model.set({
+ address: {
+ street: 'name'
+ }
+ });
+ },
+
+ "sets the value": function() {
+ assert(this.result);
+ },
+
+ "calls the valid callback": function() {
+ assert.calledWith(this.valid, this.view, 'address.street');
+ },
+
+ "is valid returns true for the specified attribute name": function () {
+ assert(this.model.isValid('address.street'));
+ },
+
+ "is valid returns true for the specified attribute names": function () {
+ assert(this.model.isValid(['address.street', 'address.street']));
+ },
+
+ "pre validate returns no error message for the specified attribute name": function () {
+ refute(this.model.preValidate('address.street', 'street'));
+ }
+ }
},
- "invalid": {
- setUp: function () {
- this.result = this.model.set({
- address: {
- street:''
+ "two levels": {
+ setUp: function() {
+ this.valid = this.spy();
+ this.invalid = this.spy();
+
+ var Model = Backbone.Model.extend({
+ validation: {
+ 'foo.bar.baz': {
+ required: true,
+ msg: 'error'
+ }
}
});
+
+ this.model = new Model();
+ this.view = new Backbone.View({model: this.model});
+
+ Backbone.Validation.bind(this.view, {
+ invalid: this.invalid,
+ valid: this.valid
+ });
},
- "refutes setting invalid values": function() {
- refute(this.result);
- },
-
- "calls the invalid callback": function() {
- assert.calledWith(this.invalid, this.view, 'address.street', 'Address. street is required');
- },
-
- "is valid returns false for the specified attribute name": function () {
- refute(this.model.isValid('address.street'));
- },
-
- "is valid returns false for the specified attribute names": function () {
- refute(this.model.isValid(['address.street', 'address.street']));
+ "invalid": {
+ setUp: function () {
+ this.result = this.model.set({
+ foo: {
+ bar: {
+ baz: ''
+ }
+ }
+ });
+ },
+
+ "refutes setting invalid values": function() {
+ refute(this.result);
+ },
+
+ "calls the invalid callback": function() {
+ assert.calledWith(this.invalid, this.view, 'foo.bar.baz', 'error');
+ },
+
+ "is valid returns false for the specified attribute name": function () {
+ refute(this.model.isValid('foo.bar.baz'));
+ },
+
+ "is valid returns false for the specified attribute names": function () {
+ refute(this.model.isValid(['foo.bar.baz', 'foo.bar.baz']));
+ },
+
+ "pre validate returns error message for the specified attribute name": function () {
+ assert(this.model.preValidate('foo.bar.baz', ''));
+ }
},
-
- "pre validate returns error message for the specified attribute name": function () {
- assert(this.model.preValidate('address.street', ''));
+
+ "valid": {
+ setUp: function () {
+ this.result = this.model.set({
+ foo: {
+ bar: {
+ baz: 'val'
+ }
+ }
+ });
+ },
+
+ "sets the value": function() {
+ assert(this.result);
+ },
+
+ "calls the valid callback": function() {
+ assert.calledWith(this.valid, this.view, 'foo.bar.baz');
+ },
+
+ "is valid returns true for the specified attribute name": function () {
+ assert(this.model.isValid('foo.bar.baz'));
+ },
+
+ "is valid returns true for the specified attribute names": function () {
+ assert(this.model.isValid(['foo.bar.baz', 'foo.bar.baz']));
+ },
+
+ "pre validate returns no error message for the specified attribute name": function () {
+ refute(this.model.preValidate('foo.bar.baz', 'val'));
+ }
}
},
- "valid": {
- setUp: function () {
- this.result = this.model.set({
- address: {
- street: 'name'
+ "complex nesting": {
+ setUp: function() {
+ this.valid = this.spy();
+ this.invalid = this.spy();
+
+ var Model = Backbone.Model.extend({
+ validation: {
+ 'foo.bar.baz': {
+ required: true,
+ msg: 'error'
+ },
+ 'foo.foo': {
+ required: true,
+ msg: 'error'
+ }
}
});
+
+ this.model = new Model();
+ this.view = new Backbone.View({model: this.model});
+
+ Backbone.Validation.bind(this.view, {
+ invalid: this.invalid,
+ valid: this.valid
+ });
},
- "sets the value": function() {
- assert(this.result);
- },
-
- "calls the valid callback": function() {
- assert.calledWith(this.valid, this.view, 'address.street');
- },
-
- "is valid returns true for the specified attribute name": function () {
- assert(this.model.isValid('address.street'));
- },
-
- "is valid returns true for the specified attribute names": function () {
- assert(this.model.isValid(['address.street', 'address.street']));
+ "invalid": {
+ setUp: function () {
+ this.result = this.model.set({
+ foo: {
+ foo: '',
+ bar: {
+ baz: ''
+ }
+ }
+ });
+ },
+
+ "refutes setting invalid values": function() {
+ refute(this.result);
+ },
+
+ "calls the invalid callback": function() {
+ assert.calledWith(this.invalid, this.view, 'foo.bar.baz', 'error');
+ assert.calledWith(this.invalid, this.view, 'foo.foo', 'error');
+ },
+
+ "is valid returns false for the specified attribute name": function () {
+ refute(this.model.isValid('foo.bar.baz'));
+ refute(this.model.isValid('foo.foo'));
+ },
+
+ "is valid returns false for the specified attribute names": function () {
+ refute(this.model.isValid(['foo.foo', 'foo.bar.baz']));
+ },
+
+ "pre validate returns error message for the specified attribute name": function () {
+ assert(this.model.preValidate('foo.bar.baz', ''));
+ assert(this.model.preValidate('foo.foo', ''));
+ }
},
-
- "pre validate returns no error message for the specified attribute name": function () {
- refute(this.model.preValidate('address.street', 'street'));
+
+ "valid": {
+ setUp: function () {
+ this.result = this.model.set({
+ foo: {
+ foo: 'val',
+ bar: {
+ baz: 'val'
+ }
+ }
+ });
+ },
+
+ "sets the value": function() {
+ assert(this.result);
+ },
+
+ "calls the valid callback": function() {
+ assert.calledWith(this.valid, this.view, 'foo.bar.baz');
+ assert.calledWith(this.valid, this.view, 'foo.foo');
+ },
+
+ "is valid returns true for the specified attribute name": function () {
+ assert(this.model.isValid('foo.bar.baz'));
+ assert(this.model.isValid('foo.foo'));
+ },
+
+ "is valid returns true for the specified attribute names": function () {
+ assert(this.model.isValid(['foo.bar.baz', 'foo.foo']));
+ },
+
+ "pre validate returns no error message for the specified attribute name": function () {
+ refute(this.model.preValidate('foo.bar.baz', 'val'));
+ refute(this.model.preValidate('foo.foo', 'val'));
+ }
}
}
});

0 comments on commit 7446eec

Please sign in to comment.
Something went wrong with that request. Please try again.