Skip to content

Instantly share code, notes, and snippets.

@jashkenas
Created December 21, 2012 17:22
Show Gist options
  • Save jashkenas/4354228 to your computer and use it in GitHub Desktop.
Save jashkenas/4354228 to your computer and use it in GitHub Desktop.
diff --git a/test/model.js b/test/model.js
index ba5e1eb..a6c5609 100644
--- a/test/model.js
+++ b/test/model.js
@@ -223,15 +223,16 @@ $(document).ready(function() {
equal(value, 'last');
});
- test("set falsy values in the correct order", 1, function() {
+ test("set falsy values in the correct order", 2, function() {
var model = new Backbone.Model({result: 'result'});
model.on('change', function() {
- equal(model.changed.result, false);
+ equal(model.changed.result, void 0);
+ equal(model.previous('result'), false);
});
model.set({result: void 0}, {silent: true});
model.set({result: null}, {silent: true});
model.set({result: false}, {silent: true});
- model.change();
+ model.set({result: void 0});
});
test("multiple unsets", 1, function() {
@@ -245,14 +246,12 @@ $(document).ready(function() {
equal(i, 2, 'Unset does not fire an event for missing attributes.');
});
- test("unset and changedAttributes", 2, function() {
+ test("unset and changedAttributes", 1, function() {
var model = new Backbone.Model({a: 1});
- model.unset('a', {silent: true});
- var changedAttributes = model.changedAttributes();
- ok('a' in changedAttributes, 'changedAttributes should contain unset properties');
-
- changedAttributes = model.changedAttributes();
- ok('a' in changedAttributes, 'changedAttributes should contain unset properties when running changedAttributes again after an unset.');
+ model.on('change', function() {
+ ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties');
+ });
+ model.unset('a');
});
test("using a non-default id attribute.", 5, function() {
@@ -308,9 +307,9 @@ $(document).ready(function() {
equal(model.get('two'), 4);
});
- test("change, hasChanged, changedAttributes, previous, previousAttributes", 12, function() {
- var model = new Backbone.Model({name : "Tim", age : 10});
- equal(model.changedAttributes(), false);
+ test("change, hasChanged, changedAttributes, previous, previousAttributes", 9, function() {
+ var model = new Backbone.Model({name: "Tim", age: 10});
+ deepEqual(model.changedAttributes(), false);
model.on('change', function() {
ok(model.hasChanged('name'), 'name changed');
ok(!model.hasChanged('age'), 'age did not');
@@ -320,17 +319,13 @@ $(document).ready(function() {
});
equal(model.hasChanged(), false);
equal(model.hasChanged(undefined), false);
- model.set({name : 'Rob'}, {silent : true});
- equal(model.hasChanged(), true);
- equal(model.hasChanged(undefined), true);
- equal(model.hasChanged('name'), true);
- model.change();
+ model.set({name : 'Rob'});
equal(model.get('name'), 'Rob');
});
test("changedAttributes", 3, function() {
var model = new Backbone.Model({a: 'a', b: 'b'});
- equal(model.changedAttributes(), false);
+ deepEqual(model.changedAttributes(), false);
equal(model.changedAttributes({a: 'a'}), false);
equal(model.changedAttributes({a: 'b'}).a, 'b');
});
@@ -341,8 +336,7 @@ $(document).ready(function() {
model.on('change', function(model, options) {
value = options.prefix + model.get('name');
});
- model.set({name: 'Bob'}, {silent: true});
- model.change({prefix: 'Mr. '});
+ model.set({name: 'Bob'}, {prefix: 'Mr. '});
equal(value, 'Mr. Bob');
model.set({name: 'Sue'}, {prefix: 'Ms. '});
equal(value, 'Ms. Sue');
@@ -414,8 +408,6 @@ $(document).ready(function() {
equal(model.get('title'), 'Twelfth Night');
});
-
-
test("fetch", 2, function() {
doc.fetch();
equal(this.syncArgs.method, 'read');
@@ -604,19 +596,29 @@ $(document).ready(function() {
ok('x' in model.attributes);
});
- test("change fires change:attr", 1, function() {
+ test("hasChanged works outside of change events, and true within", 6, function() {
var model = new Backbone.Model({x: 1});
+ model.on('change:x', function() {
+ ok(model.hasChanged('x'));
+ equal(model.get('x'), 1);
+ });
model.set({x: 2}, {silent: true});
- model.on('change:x', function(){ ok(true); });
- model.change();
+ ok(model.hasChanged());
+ equal(model.hasChanged('x'), true);
+ model.set({x: 1});
+ ok(model.hasChanged());
+ equal(model.hasChanged('x'), true);
});
- test("hasChanged is false after original values are set", 2, function() {
- var model = new Backbone.Model({x: 1});
- model.on('change:x', function(){ ok(false); });
- model.set({x: 2}, {silent: true});
+ test("hasChanged gets cleared on the following set", 4, function() {
+ var model = new Backbone.Model;
+ model.set({x: 1});
ok(model.hasChanged());
- model.set({x: 1}, {silent: true});
+ model.set({x: 1});
+ ok(!model.hasChanged());
+ model.set({x: 2});
+ ok(model.hasChanged());
+ model.set({});
ok(!model.hasChanged());
});
@@ -690,27 +692,21 @@ $(document).ready(function() {
model.set({y: true});
});
model.set({x: true});
- deepEqual(events, ['change:y', 'change:x', 'change']);
+ deepEqual(events, ['change:y', 'change:x', 'change', 'change']);
events = [];
- model.change();
- deepEqual(events, ['change:z', 'change']);
+ model.set({z: true});
+ deepEqual(events, []);
});
test("nested `change` only fires once", 1, function() {
var model = new Backbone.Model();
model.on('change', function() {
ok(true);
- model.change();
+ model.set({x: true});
});
model.set({x: true});
});
- test("no `'change'` event if no changes", 0, function() {
- var model = new Backbone.Model();
- model.on('change', function() { ok(false); });
- model.change();
- });
-
test("nested `set` during `'change'`", 6, function() {
var count = 0;
var model = new Backbone.Model();
@@ -722,13 +718,13 @@ $(document).ready(function() {
model.set({y: true});
break;
case 1:
- deepEqual(this.changedAttributes(), {y: true});
- equal(model.previous('x'), true);
+ deepEqual(this.changedAttributes(), {x: true, y: true});
+ equal(model.previous('x'), undefined);
model.set({z: true});
break;
case 2:
- deepEqual(this.changedAttributes(), {z: true});
- equal(model.previous('y'), true);
+ deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
+ equal(model.previous('y'), undefined);
break;
default:
ok(false);
@@ -740,7 +736,7 @@ $(document).ready(function() {
test("nested `'change'` with silent", 3, function() {
var count = 0;
var model = new Backbone.Model();
- model.on('change:y', function() { ok(true); });
+ model.on('change:y', function() { ok(false); });
model.on('change', function() {
switch(count++) {
case 0:
@@ -748,7 +744,10 @@ $(document).ready(function() {
model.set({y: true}, {silent: true});
break;
case 1:
- deepEqual(this.changedAttributes(), {y: true, z: true});
+ deepEqual(this.changedAttributes(), {x: true, y: true});
+ break;
+ case 2:
+ deepEqual(this.changedAttributes(), {z: true});
break;
default:
ok(false);
@@ -758,9 +757,9 @@ $(document).ready(function() {
model.set({z: true});
});
- test("nested `'change:attr'` with silent", 1, function() {
+ test("nested `'change:attr'` with silent", 0, function() {
var model = new Backbone.Model();
- model.on('change:y', function(){ ok(true); });
+ model.on('change:y', function(){ ok(false); });
model.on('change', function() {
model.set({y: true}, {silent: true});
model.set({z: true});
@@ -778,21 +777,25 @@ $(document).ready(function() {
equal(val, 2);
});
model.set({x: true});
- model.change();
});
- test("multiple nested changes with silent", 2, function() {
+ test("multiple nested changes with silent", 1, function() {
var changes = [];
var model = new Backbone.Model();
model.on('change:b', function(model, val) { changes.push(val); });
model.on('change', function() {
model.set({b: 1});
- model.set({b: 2}, {silent: true});
});
model.set({b: 0});
deepEqual(changes, [0, 1]);
- model.change();
- deepEqual(changes, [0, 1, 2, 1]);
+ });
+
+ test("basic silent change semantics", 1, function() {
+ var model = new Backbone.Model;
+ model.set({x: 1});
+ model.on('change', function(){ ok(true); });
+ model.set({x: 2}, {silent: true});
+ model.set({x: 1});
});
test("nested set multiple times", 1, function() {
@@ -896,9 +899,9 @@ $(document).ready(function() {
.save(null, {wait: true});
});
- test("#1664 - Changing from one value, silently to another, back to original does not trigger change.", 0, function() {
+ test("#1664 - Changing from one value, silently to another, back to original triggers a change.", 1, function() {
var model = new Backbone.Model({x:1});
- model.on('change:x', function() { ok(false); });
+ model.on('change:x', function() { ok(true); });
model.set({x:2},{silent:true});
model.set({x:3},{silent:true});
model.set({x:1});
@@ -911,15 +914,11 @@ $(document).ready(function() {
model.set({a:'c'}, {silent:true});
model.set({b:2}, {silent:true});
model.unset('c', {silent:true});
- model.set({a:'a'}, {silent:true});
- model.set({b:1}, {silent:true});
- model.set({c:'item'}, {silent:true});
});
model.on('change:a change:b change:c', function(model, val) { changes.push(val); });
model.set({a:'a', b:1, c:'item'});
deepEqual(changes, ['a',1,'item']);
- model.change();
- deepEqual(changes, ['a',1,'item']);
+ deepEqual(model.attributes, {a: 'c', b: 2});
});
test("#1791 - `attributes` is available for `parse`", function() {
@@ -930,7 +929,7 @@ $(document).ready(function() {
expect(0);
});
- test("silent changes in last `change` event back to original does not trigger change", 2, function() {
+ test("silent changes in last `change` event back to original triggers change", 2, function() {
var changes = [];
var model = new Backbone.Model();
model.on('change:a change:b change:c', function(model, val) { changes.push(val); });
@@ -939,9 +938,8 @@ $(document).ready(function() {
});
model.set({a:'a'});
deepEqual(changes, ['a']);
- model.set({a:'a'}, {silent:true});
- model.change();
- deepEqual(changes, ['a']);
+ model.set({a:'a'});
+ deepEqual(changes, ['a', 'a']);
});
test("#1943 change calculations should use _.isEqual", function() {
@@ -990,4 +988,16 @@ $(document).ready(function() {
equal(model.validationError, "This shouldn't happen");
});
+ test("toJSON receives attrs during save(..., {wait: true})", 1, function() {
+ var Model = Backbone.Model.extend({
+ url: '/test',
+ toJSON: function() {
+ strictEqual(this.attributes.x, 1);
+ return _.clone(this.attributes);
+ }
+ });
+ var model = new Model;
+ model.save({x: 1}, {wait: true});
+ });
+
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment