From 634291199f28bb9e5aa60771ba31bf999385ece8 Mon Sep 17 00:00:00 2001 From: Michael Cox Date: Thu, 14 Jun 2012 12:27:11 -0400 Subject: [PATCH 1/3] Added validation tests for top level and nested attributes. --- test/nested-model.js | 67 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/test/nested-model.js b/test/nested-model.js index 2306b7c..0039c84 100644 --- a/test/nested-model.js +++ b/test/nested-model.js @@ -378,6 +378,60 @@ $(document).ready(function() { sinon.assert.notCalled(changeNameMiddleInitial); }); + test("change event doesn't fire if validation fails on top level attribute", function() { + var change = sinon.spy(); + var changeName = sinon.spy(); + var changeNameFirst = sinon.spy(); + + doc.validate = function(attributes) { + if (attributes.gender.length > 1) { + return "Gender should be 'M' or 'F'"; + } + }; + + doc.bind('change', change); + doc.bind('change:name', changeName); + doc.bind('change:name.first', changeNameFirst); + + doc.set({'gender': 'Unknown'}); + + sinon.assert.notCalled(change); + sinon.assert.notCalled(changeName); + sinon.assert.notCalled(changeNameFirst); + + doc.validate = undefined; + }); + + test("change event doesn't fire if validation fails on deeply nested attribute", function() { + var change = sinon.spy(); + var changeName = sinon.spy(); + var changeNameMiddle = sinon.spy(); + var changeNameMiddleInitial = sinon.spy(); + + doc.validate = function(attributes) { + if (attributes.name.middle.initial.length > 1) { + return "Middle initial is too long"; + } + }; + + doc.bind('change', change); + doc.bind('change:name', changeName); + doc.bind('change:name.middle', changeNameMiddle); + doc.bind('change:name.middle.initial', changeNameMiddleInitial); + + doc.set({'name.middle': { + initial: 'ThisIsTooLong', + full: 'Lee' + }}); + + sinon.assert.notCalled(change); + sinon.assert.notCalled(changeName); + sinon.assert.notCalled(changeNameMiddle); + sinon.assert.notCalled(changeNameMiddleInitial); + + doc.validate = undefined; + }); + test("attribute change event receives new value", function() { doc.bind('change:name', function(model, newVal){ deepEqual(newVal, { @@ -617,12 +671,12 @@ $(document).ready(function() { test("#changedAttributes() should clear the nested attributes between change events with validation", function() { doc.validate = function(attributes) { - if (attributes.name.first.length > 15) { - return "First name is too long"; - } - }; + if (attributes.name.first.length > 15) { + return "First name is too long"; + } + }; - doc.set({'name.first': 'TooLongFirstName'}); + doc.set({'name.first': 'TooLongFirstName'}); doc.bind('change', function(){ deepEqual(this.changedAttributes(), { @@ -639,6 +693,8 @@ $(document).ready(function() { }); doc.set({'name.last': 'Dylan'}); + + doc.validate = undefined; }); // ----- UNSET -------- @@ -733,7 +789,6 @@ $(document).ready(function() { equal(model, doc); }); - // ----- REMOVE -------- test("#remove() on nested array should remove the element from the array", function() { From 01b0bc72f75d0e5712caa4fbd8590efee2c7b0f7 Mon Sep 17 00:00:00 2001 From: Michael Cox Date: Thu, 14 Jun 2012 12:39:56 -0400 Subject: [PATCH 2/3] Added test to confirm add() fails when validation fails. --- test/nested-model.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/nested-model.js b/test/nested-model.js index 0039c84..05ab07c 100644 --- a/test/nested-model.js +++ b/test/nested-model.js @@ -789,6 +789,34 @@ $(document).ready(function() { equal(model, doc); }); + test("#add() on nested array fails if validation fails", function() { + var addAddresses = sinon.spy(); + + equal(doc.get('addresses').length, 2); + + doc.bind('add:addresses', addAddresses); + + doc.validate = function(attributes) { + for (var i = attributes.addresses.length - 1; i >= 0; i--) { + if (attributes.addresses[i].state.length > 2) { + return "Must use 2 letter state abbreviation"; + } + }; + }; + + var attrs = { + city: 'Lincoln', + state: 'Nebraska' // Longer than 2 letters, validation should fail + }; + + doc.add('addresses', attrs); + + sinon.assert.notCalled(addAddresses); + equal(doc.get('addresses[2]'), undefined); + + doc.validate = undefined; + }); + // ----- REMOVE -------- test("#remove() on nested array should remove the element from the array", function() { From bc28216d092d44be2dbf15d225a1d39aae9252f7 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 14 Jun 2012 13:33:06 -0400 Subject: [PATCH 3/3] clean up validation tests --- README.md | 2 +- test/nested-model.js | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 710db11..e936099 100644 --- a/README.md +++ b/README.md @@ -198,4 +198,4 @@ Initial release! ## Contributing -Pull requests are more than welcome - please add tests, which can be run by opening test/index.html. +Pull requests are more than welcome - please add tests, which can be run by opening test/index.html. Please make sure the modifications pass [JSHint](http://jshint.com/). diff --git a/test/nested-model.js b/test/nested-model.js index 05ab07c..d7df932 100644 --- a/test/nested-model.js +++ b/test/nested-model.js @@ -398,8 +398,6 @@ $(document).ready(function() { sinon.assert.notCalled(change); sinon.assert.notCalled(changeName); sinon.assert.notCalled(changeNameFirst); - - doc.validate = undefined; }); test("change event doesn't fire if validation fails on deeply nested attribute", function() { @@ -428,8 +426,6 @@ $(document).ready(function() { sinon.assert.notCalled(changeName); sinon.assert.notCalled(changeNameMiddle); sinon.assert.notCalled(changeNameMiddleInitial); - - doc.validate = undefined; }); test("attribute change event receives new value", function() { @@ -693,10 +689,9 @@ $(document).ready(function() { }); doc.set({'name.last': 'Dylan'}); - - doc.validate = undefined; }); + // ----- UNSET -------- test("#unset() top-level attribute", function() { @@ -801,7 +796,7 @@ $(document).ready(function() { if (attributes.addresses[i].state.length > 2) { return "Must use 2 letter state abbreviation"; } - }; + } }; var attrs = { @@ -813,10 +808,9 @@ $(document).ready(function() { sinon.assert.notCalled(addAddresses); equal(doc.get('addresses[2]'), undefined); - - doc.validate = undefined; }); + // ----- REMOVE -------- test("#remove() on nested array should remove the element from the array", function() {