"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "vendor/backbone/test/model.js" between
lodash-3.0.0.tar.gz and lodash-4.0.0.tar.gz

About: lodash is a modern JavaScript utility library delivering modularity, performance, & extras.

model.js  (lodash-3.0.0):model.js  (lodash-4.0.0)
(function() { (function() {
var proxy = Backbone.Model.extend(); var ProxyModel = Backbone.Model.extend();
var klass = Backbone.Collection.extend({ var Klass = Backbone.Collection.extend({
url : function() { return '/collection'; } url: function() { return '/collection'; }
}); });
var doc, collection; var doc, collection;
module("Backbone.Model", { QUnit.module('Backbone.Model', {
setup: function() { beforeEach: function(assert) {
doc = new proxy({ doc = new ProxyModel({
id : '1-the-tempest', id: '1-the-tempest',
title : "The Tempest", title: 'The Tempest',
author : "Bill Shakespeare", author: 'Bill Shakespeare',
length : 123 length: 123
}); });
collection = new klass(); collection = new Klass();
collection.add(doc); collection.add(doc);
} }
}); });
test("initialize", 3, function() { QUnit.test('initialize', function(assert) {
assert.expect(3);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
initialize: function() { initialize: function() {
this.one = 1; this.one = 1;
equal(this.collection, collection); assert.equal(this.collection, collection);
} }
}); });
var model = new Model({}, {collection: collection}); var model = new Model({}, {collection: collection});
equal(model.one, 1); assert.equal(model.one, 1);
equal(model.collection, collection); assert.equal(model.collection, collection);
}); });
test("initialize with attributes and options", 1, function() { QUnit.test('initialize with attributes and options', function(assert) {
assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
initialize: function(attributes, options) { initialize: function(attributes, options) {
this.one = options.one; this.one = options.one;
} }
}); });
var model = new Model({}, {one: 1}); var model = new Model({}, {one: 1});
equal(model.one, 1); assert.equal(model.one, 1);
}); });
test("initialize with parsed attributes", 1, function() { QUnit.test('initialize with parsed attributes', function(assert) {
assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
parse: function(attrs) { parse: function(attrs) {
attrs.value += 1; attrs.value += 1;
return attrs; return attrs;
} }
}); });
var model = new Model({value: 1}, {parse: true}); var model = new Model({value: 1}, {parse: true});
equal(model.get('value'), 2); assert.equal(model.get('value'), 2);
}); });
test("initialize with defaults", 2, function() { QUnit.test('initialize with defaults', function(assert) {
assert.expect(2);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
defaults: { defaults: {
first_name: 'Unknown', firstName: 'Unknown',
last_name: 'Unknown' lastName: 'Unknown'
} }
}); });
var model = new Model({'first_name': 'John'}); var model = new Model({'firstName': 'John'});
equal(model.get('first_name'), 'John'); assert.equal(model.get('firstName'), 'John');
equal(model.get('last_name'), 'Unknown'); assert.equal(model.get('lastName'), 'Unknown');
}); });
test("parse can return null", 1, function() { QUnit.test('parse can return null', function(assert) {
assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
parse: function(attrs) { parse: function(attrs) {
attrs.value += 1; attrs.value += 1;
return null; return null;
} }
}); });
var model = new Model({value: 1}, {parse: true}); var model = new Model({value: 1}, {parse: true});
equal(JSON.stringify(model.toJSON()), "{}"); assert.equal(JSON.stringify(model.toJSON()), '{}');
}); });
test("url", 3, function() { QUnit.test('url', function(assert) {
assert.expect(3);
doc.urlRoot = null; doc.urlRoot = null;
equal(doc.url(), '/collection/1-the-tempest'); assert.equal(doc.url(), '/collection/1-the-tempest');
doc.collection.url = '/collection/'; doc.collection.url = '/collection/';
equal(doc.url(), '/collection/1-the-tempest'); assert.equal(doc.url(), '/collection/1-the-tempest');
doc.collection = null; doc.collection = null;
raises(function() { doc.url(); }); assert.throws(function() { doc.url(); });
doc.collection = collection; doc.collection = collection;
}); });
test("url when using urlRoot, and uri encoding", 2, function() { QUnit.test('url when using urlRoot, and uri encoding', function(assert) {
assert.expect(2);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
urlRoot: '/collection' urlRoot: '/collection'
}); });
var model = new Model(); var model = new Model();
equal(model.url(), '/collection'); assert.equal(model.url(), '/collection');
model.set({id: '+1+'}); model.set({id: '+1+'});
equal(model.url(), '/collection/%2B1%2B'); assert.equal(model.url(), '/collection/%2B1%2B');
}); });
test("url when using urlRoot as a function to determine urlRoot at runtime", 2 QUnit.test('url when using urlRoot as a function to determine urlRoot at runti
, function() { me', function(assert) {
assert.expect(2);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
urlRoot: function() { urlRoot: function() {
return '/nested/' + this.get('parent_id') + '/collection'; return '/nested/' + this.get('parentId') + '/collection';
} }
}); });
var model = new Model({parent_id: 1}); var model = new Model({parentId: 1});
equal(model.url(), '/nested/1/collection'); assert.equal(model.url(), '/nested/1/collection');
model.set({id: 2}); model.set({id: 2});
equal(model.url(), '/nested/1/collection/2'); assert.equal(model.url(), '/nested/1/collection/2');
}); });
test("underscore methods", 5, function() { QUnit.test('underscore methods', function(assert) {
var model = new Backbone.Model({ 'foo': 'a', 'bar': 'b', 'baz': 'c' }); assert.expect(5);
var model = new Backbone.Model({foo: 'a', bar: 'b', baz: 'c'});
var model2 = model.clone(); var model2 = model.clone();
deepEqual(model.keys(), ['foo', 'bar', 'baz']); assert.deepEqual(model.keys(), ['foo', 'bar', 'baz']);
deepEqual(model.values(), ['a', 'b', 'c']); assert.deepEqual(model.values(), ['a', 'b', 'c']);
deepEqual(model.invert(), { 'a': 'foo', 'b': 'bar', 'c': 'baz' }); assert.deepEqual(model.invert(), {a: 'foo', b: 'bar', c: 'baz'});
deepEqual(model.pick('foo', 'baz'), {'foo': 'a', 'baz': 'c'}); assert.deepEqual(model.pick('foo', 'baz'), {foo: 'a', baz: 'c'});
deepEqual(model.omit('foo', 'bar'), {'baz': 'c'}); assert.deepEqual(model.omit('foo', 'bar'), {baz: 'c'});
}); });
test("clone", 10, function() { QUnit.test('chain', function(assert) {
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3}); var model = new Backbone.Model({a: 0, b: 1, c: 2});
assert.deepEqual(model.chain().pick('a', 'b', 'c').values().compact().value(
), [1, 2]);
});
QUnit.test('clone', function(assert) {
assert.expect(10);
var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
var b = a.clone(); var b = a.clone();
equal(a.get('foo'), 1); assert.equal(a.get('foo'), 1);
equal(a.get('bar'), 2); assert.equal(a.get('bar'), 2);
equal(a.get('baz'), 3); assert.equal(a.get('baz'), 3);
equal(b.get('foo'), a.get('foo'), "Foo should be the same on the clone."); assert.equal(b.get('foo'), a.get('foo'), 'Foo should be the same on the clon
equal(b.get('bar'), a.get('bar'), "Bar should be the same on the clone."); e.');
equal(b.get('baz'), a.get('baz'), "Baz should be the same on the clone."); assert.equal(b.get('bar'), a.get('bar'), 'Bar should be the same on the clon
a.set({foo : 100}); e.');
equal(a.get('foo'), 100); assert.equal(b.get('baz'), a.get('baz'), 'Baz should be the same on the clon
equal(b.get('foo'), 1, "Changing a parent attribute does not change the clon e.');
e."); a.set({foo: 100});
assert.equal(a.get('foo'), 100);
assert.equal(b.get('foo'), 1, 'Changing a parent attribute does not change t
he clone.');
var foo = new Backbone.Model({p: 1}); var foo = new Backbone.Model({p: 1});
var bar = new Backbone.Model({p: 2}); var bar = new Backbone.Model({p: 2});
bar.set(foo.clone().attributes, {unset: true}); bar.set(foo.clone().attributes, {unset: true});
equal(foo.get('p'), 1); assert.equal(foo.get('p'), 1);
equal(bar.get('p'), undefined); assert.equal(bar.get('p'), undefined);
});
test("isNew", 6, function() {
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
ok(a.isNew(), "it should be new");
a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 });
ok(!a.isNew(), "any defined ID is legal, negative or positive");
a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 });
ok(!a.isNew(), "any defined ID is legal, including zero");
ok( new Backbone.Model({ }).isNew(), "is true when there is no id")
;
ok(!new Backbone.Model({ 'id': 2 }).isNew(), "is false for a positive integ
er");
ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integ
er");
});
test("get", 2, function() {
equal(doc.get('title'), 'The Tempest');
equal(doc.get('author'), 'Bill Shakespeare');
}); });
test("escape", 5, function() { QUnit.test('isNew', function(assert) {
equal(doc.escape('title'), 'The Tempest'); assert.expect(6);
var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
assert.ok(a.isNew(), 'it should be new');
a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: -5});
assert.ok(!a.isNew(), 'any defined ID is legal, negative or positive');
a = new Backbone.Model({foo: 1, bar: 2, baz: 3, id: 0});
assert.ok(!a.isNew(), 'any defined ID is legal, including zero');
assert.ok(new Backbone.Model().isNew(), 'is true when there is no id');
assert.ok(!new Backbone.Model({id: 2}).isNew(), 'is false for a positive int
eger');
assert.ok(!new Backbone.Model({id: -5}).isNew(), 'is false for a negative in
teger');
});
QUnit.test('get', function(assert) {
assert.expect(2);
assert.equal(doc.get('title'), 'The Tempest');
assert.equal(doc.get('author'), 'Bill Shakespeare');
});
QUnit.test('escape', function(assert) {
assert.expect(5);
assert.equal(doc.escape('title'), 'The Tempest');
doc.set({audience: 'Bill & Bob'}); doc.set({audience: 'Bill & Bob'});
equal(doc.escape('audience'), 'Bill & Bob'); assert.equal(doc.escape('audience'), 'Bill & Bob');
doc.set({audience: 'Tim > Joan'}); doc.set({audience: 'Tim > Joan'});
equal(doc.escape('audience'), 'Tim > Joan'); assert.equal(doc.escape('audience'), 'Tim > Joan');
doc.set({audience: 10101}); doc.set({audience: 10101});
equal(doc.escape('audience'), '10101'); assert.equal(doc.escape('audience'), '10101');
doc.unset('audience'); doc.unset('audience');
equal(doc.escape('audience'), ''); assert.equal(doc.escape('audience'), '');
}); });
test("has", 10, function() { QUnit.test('has', function(assert) {
assert.expect(10);
var model = new Backbone.Model(); var model = new Backbone.Model();
strictEqual(model.has('name'), false); assert.strictEqual(model.has('name'), false);
model.set({ model.set({
'0': 0, '0': 0,
'1': 1, '1': 1,
'true': true, 'true': true,
'false': false, 'false': false,
'empty': '', 'empty': '',
'name': 'name', 'name': 'name',
'null': null, 'null': null,
'undefined': undefined 'undefined': undefined
}); });
strictEqual(model.has('0'), true); assert.strictEqual(model.has('0'), true);
strictEqual(model.has('1'), true); assert.strictEqual(model.has('1'), true);
strictEqual(model.has('true'), true); assert.strictEqual(model.has('true'), true);
strictEqual(model.has('false'), true); assert.strictEqual(model.has('false'), true);
strictEqual(model.has('empty'), true); assert.strictEqual(model.has('empty'), true);
strictEqual(model.has('name'), true); assert.strictEqual(model.has('name'), true);
model.unset('name'); model.unset('name');
strictEqual(model.has('name'), false); assert.strictEqual(model.has('name'), false);
strictEqual(model.has('null'), false); assert.strictEqual(model.has('null'), false);
strictEqual(model.has('undefined'), false); assert.strictEqual(model.has('undefined'), false);
});
QUnit.test('matches', function(assert) {
assert.expect(4);
var model = new Backbone.Model();
assert.strictEqual(model.matches({name: 'Jonas', cool: true}), false);
model.set({name: 'Jonas', cool: true});
assert.strictEqual(model.matches({name: 'Jonas'}), true);
assert.strictEqual(model.matches({name: 'Jonas', cool: true}), true);
assert.strictEqual(model.matches({name: 'Jonas', cool: false}), false);
}); });
test("set and unset", 8, function() { QUnit.test('matches with predicate', function(assert) {
var model = new Backbone.Model({a: 0});
assert.strictEqual(model.matches(function(attr) {
return attr.a > 1 && attr.b != null;
}), false);
model.set({a: 3, b: true});
assert.strictEqual(model.matches(function(attr) {
return attr.a > 1 && attr.b != null;
}), true);
});
QUnit.test('set and unset', function(assert) {
assert.expect(8);
var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3}); var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
var changeCount = 0; var changeCount = 0;
a.on("change:foo", function() { changeCount += 1; }); a.on('change:foo', function() { changeCount += 1; });
a.set({'foo': 2}); a.set({foo: 2});
ok(a.get('foo') == 2, "Foo should have changed."); assert.ok(a.get('foo') == 2, 'Foo should have changed.');
ok(changeCount == 1, "Change count should have incremented."); assert.ok(changeCount == 1, 'Change count should have incremented.');
a.set({'foo': 2}); // set with value that is not new shouldn't fire change e // set with value that is not new shouldn't fire change event
vent a.set({foo: 2});
ok(a.get('foo') == 2, "Foo should NOT have changed, still 2"); assert.ok(a.get('foo') == 2, 'Foo should NOT have changed, still 2');
ok(changeCount == 1, "Change count should NOT have incremented."); assert.ok(changeCount == 1, 'Change count should NOT have incremented.');
a.validate = function(attrs) { a.validate = function(attrs) {
equal(attrs.foo, void 0, "validate:true passed while unsetting"); assert.equal(attrs.foo, void 0, 'validate:true passed while unsetting');
}; };
a.unset('foo', {validate: true}); a.unset('foo', {validate: true});
equal(a.get('foo'), void 0, "Foo should have changed"); assert.equal(a.get('foo'), void 0, 'Foo should have changed');
delete a.validate; delete a.validate;
ok(changeCount == 2, "Change count should have incremented for unset."); assert.ok(changeCount == 2, 'Change count should have incremented for unset. ');
a.unset('id'); a.unset('id');
equal(a.id, undefined, "Unsetting the id should remove the id property."); assert.equal(a.id, undefined, 'Unsetting the id should remove the id propert y.');
}); });
test("#2030 - set with failed validate, followed by another set triggers chang e", function () { QUnit.test('#2030 - set with failed validate, followed by another set triggers change', function(assert) {
var attr = 0, main = 0, error = 0; var attr = 0, main = 0, error = 0;
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
validate: function (attr) { validate: function(attr) {
if (attr.x > 1) { if (attr.x > 1) {
error++; error++;
return "this is an error"; return 'this is an error';
} }
} }
}); });
var model = new Model({x:0}); var model = new Model({x: 0});
model.on('change:x', function () { attr++; }); model.on('change:x', function() { attr++; });
model.on('change', function () { main++; }); model.on('change', function() { main++; });
model.set({x:2}, {validate:true}); model.set({x: 2}, {validate: true});
model.set({x:1}, {validate:true}); model.set({x: 1}, {validate: true});
deepEqual([attr, main, error], [1, 1, 1]); assert.deepEqual([attr, main, error], [1, 1, 1]);
}); });
test("set triggers changes in the correct order", function() { QUnit.test('set triggers changes in the correct order', function(assert) {
var value = null; var value = null;
var model = new Backbone.Model; var model = new Backbone.Model;
model.on('last', function(){ value = 'last'; }); model.on('last', function(){ value = 'last'; });
model.on('first', function(){ value = 'first'; }); model.on('first', function(){ value = 'first'; });
model.trigger('first'); model.trigger('first');
model.trigger('last'); model.trigger('last');
equal(value, 'last'); assert.equal(value, 'last');
}); });
test("set falsy values in the correct order", 2, function() { QUnit.test('set falsy values in the correct order', function(assert) {
assert.expect(2);
var model = new Backbone.Model({result: 'result'}); var model = new Backbone.Model({result: 'result'});
model.on('change', function() { model.on('change', function() {
equal(model.changed.result, void 0); assert.equal(model.changed.result, void 0);
equal(model.previous('result'), false); assert.equal(model.previous('result'), false);
}); });
model.set({result: void 0}, {silent: true}); model.set({result: void 0}, {silent: true});
model.set({result: null}, {silent: true}); model.set({result: null}, {silent: true});
model.set({result: false}, {silent: true}); model.set({result: false}, {silent: true});
model.set({result: void 0}); model.set({result: void 0});
}); });
test("nested set triggers with the correct options", function() { QUnit.test('nested set triggers with the correct options', function(assert) {
var model = new Backbone.Model(); var model = new Backbone.Model();
var o1 = {}; var o1 = {};
var o2 = {}; var o2 = {};
var o3 = {}; var o3 = {};
model.on('change', function(__, options) { model.on('change', function(__, options) {
switch (model.get('a')) { switch (model.get('a')) {
case 1: case 1:
equal(options, o1); assert.equal(options, o1);
return model.set('a', 2, o2); return model.set('a', 2, o2);
case 2: case 2:
equal(options, o2); assert.equal(options, o2);
return model.set('a', 3, o3); return model.set('a', 3, o3);
case 3: case 3:
equal(options, o3); assert.equal(options, o3);
} }
}); });
model.set('a', 1, o1); model.set('a', 1, o1);
}); });
test("multiple unsets", 1, function() { QUnit.test('multiple unsets', function(assert) {
assert.expect(1);
var i = 0; var i = 0;
var counter = function(){ i++; }; var counter = function(){ i++; };
var model = new Backbone.Model({a: 1}); var model = new Backbone.Model({a: 1});
model.on("change:a", counter); model.on('change:a', counter);
model.set({a: 2}); model.set({a: 2});
model.unset('a'); model.unset('a');
model.unset('a'); model.unset('a');
equal(i, 2, 'Unset does not fire an event for missing attributes.'); assert.equal(i, 2, 'Unset does not fire an event for missing attributes.');
}); });
test("unset and changedAttributes", 1, function() { QUnit.test('unset and changedAttributes', function(assert) {
assert.expect(1);
var model = new Backbone.Model({a: 1}); var model = new Backbone.Model({a: 1});
model.on('change', function() { model.on('change', function() {
ok('a' in model.changedAttributes(), 'changedAttributes should contain uns et properties'); assert.ok('a' in model.changedAttributes(), 'changedAttributes should cont ain unset properties');
}); });
model.unset('a'); model.unset('a');
}); });
test("using a non-default id attribute.", 5, function() { QUnit.test('using a non-default id attribute.', function(assert) {
var MongoModel = Backbone.Model.extend({idAttribute : '_id'}); assert.expect(5);
var MongoModel = Backbone.Model.extend({idAttribute: '_id'});
var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'}); var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
equal(model.get('id'), 'eye-dee'); assert.equal(model.get('id'), 'eye-dee');
equal(model.id, 25); assert.equal(model.id, 25);
equal(model.isNew(), false); assert.equal(model.isNew(), false);
model.unset('_id'); model.unset('_id');
equal(model.id, undefined); assert.equal(model.id, undefined);
equal(model.isNew(), true); assert.equal(model.isNew(), true);
}); });
test("set an empty string", 1, function() { QUnit.test('setting an alternative cid prefix', function(assert) {
var model = new Backbone.Model({name : "Model"}); assert.expect(4);
model.set({name : ''}); var Model = Backbone.Model.extend({
equal(model.get('name'), ''); cidPrefix: 'm'
});
var model = new Model();
assert.equal(model.cid.charAt(0), 'm');
model = new Backbone.Model();
assert.equal(model.cid.charAt(0), 'c');
var Collection = Backbone.Collection.extend({
model: Model
});
var collection = new Collection([{id: 'c5'}, {id: 'c6'}, {id: 'c7'}]);
assert.equal(collection.get('c6').cid.charAt(0), 'm');
collection.set([{id: 'c6', value: 'test'}], {
merge: true,
add: true,
remove: false
});
assert.ok(collection.get('c6').has('value'));
}); });
test("setting an object", 1, function() { QUnit.test('set an empty string', function(assert) {
assert.expect(1);
var model = new Backbone.Model({name: 'Model'});
model.set({name: ''});
assert.equal(model.get('name'), '');
});
QUnit.test('setting an object', function(assert) {
assert.expect(1);
var model = new Backbone.Model({ var model = new Backbone.Model({
custom: { foo: 1 } custom: {foo: 1}
}); });
model.on('change', function() { model.on('change', function() {
ok(1); assert.ok(1);
}); });
model.set({ model.set({
custom: { foo: 1 } // no change should be fired custom: {foo: 1} // no change should be fired
}); });
model.set({ model.set({
custom: { foo: 2 } // change event should be fired custom: {foo: 2} // change event should be fired
}); });
}); });
test("clear", 3, function() { QUnit.test('clear', function(assert) {
assert.expect(3);
var changed; var changed;
var model = new Backbone.Model({id: 1, name : "Model"}); var model = new Backbone.Model({id: 1, name: 'Model'});
model.on("change:name", function(){ changed = true; }); model.on('change:name', function(){ changed = true; });
model.on("change", function() { model.on('change', function() {
var changedAttrs = model.changedAttributes(); var changedAttrs = model.changedAttributes();
ok('name' in changedAttrs); assert.ok('name' in changedAttrs);
}); });
model.clear(); model.clear();
equal(changed, true); assert.equal(changed, true);
equal(model.get('name'), undefined); assert.equal(model.get('name'), undefined);
}); });
test("defaults", 4, function() { QUnit.test('defaults', function(assert) {
assert.expect(4);
var Defaulted = Backbone.Model.extend({ var Defaulted = Backbone.Model.extend({
defaults: { defaults: {
"one": 1, one: 1,
"two": 2 two: 2
} }
}); });
var model = new Defaulted({two: undefined}); var model = new Defaulted({two: undefined});
equal(model.get('one'), 1); assert.equal(model.get('one'), 1);
equal(model.get('two'), 2); assert.equal(model.get('two'), 2);
Defaulted = Backbone.Model.extend({ Defaulted = Backbone.Model.extend({
defaults: function() { defaults: function() {
return { return {
"one": 3, one: 3,
"two": 4 two: 4
}; };
} }
}); });
model = new Defaulted({two: undefined}); model = new Defaulted({two: undefined});
equal(model.get('one'), 3); assert.equal(model.get('one'), 3);
equal(model.get('two'), 4); assert.equal(model.get('two'), 4);
}); });
test("change, hasChanged, changedAttributes, previous, previousAttributes", 9, QUnit.test('change, hasChanged, changedAttributes, previous, previousAttribute
function() { s', function(assert) {
var model = new Backbone.Model({name: "Tim", age: 10}); assert.expect(9);
deepEqual(model.changedAttributes(), false); var model = new Backbone.Model({name: 'Tim', age: 10});
assert.deepEqual(model.changedAttributes(), false);
model.on('change', function() { model.on('change', function() {
ok(model.hasChanged('name'), 'name changed'); assert.ok(model.hasChanged('name'), 'name changed');
ok(!model.hasChanged('age'), 'age did not'); assert.ok(!model.hasChanged('age'), 'age did not');
ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttribute assert.ok(_.isEqual(model.changedAttributes(), {name: 'Rob'}), 'changedAtt
s returns the changed attrs'); ributes returns the changed attrs');
equal(model.previous('name'), 'Tim'); assert.equal(model.previous('name'), 'Tim');
ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previ assert.ok(_.isEqual(model.previousAttributes(), {name: 'Tim', age: 10}), '
ousAttributes is correct'); previousAttributes is correct');
}); });
equal(model.hasChanged(), false); assert.equal(model.hasChanged(), false);
equal(model.hasChanged(undefined), false); assert.equal(model.hasChanged(undefined), false);
model.set({name : 'Rob'}); model.set({name: 'Rob'});
equal(model.get('name'), 'Rob'); assert.equal(model.get('name'), 'Rob');
}); });
test("changedAttributes", 3, function() { QUnit.test('changedAttributes', function(assert) {
assert.expect(3);
var model = new Backbone.Model({a: 'a', b: 'b'}); var model = new Backbone.Model({a: 'a', b: 'b'});
deepEqual(model.changedAttributes(), false); assert.deepEqual(model.changedAttributes(), false);
equal(model.changedAttributes({a: 'a'}), false); assert.equal(model.changedAttributes({a: 'a'}), false);
equal(model.changedAttributes({a: 'b'}).a, 'b'); assert.equal(model.changedAttributes({a: 'b'}).a, 'b');
}); });
test("change with options", 2, function() { QUnit.test('change with options', function(assert) {
assert.expect(2);
var value; var value;
var model = new Backbone.Model({name: 'Rob'}); var model = new Backbone.Model({name: 'Rob'});
model.on('change', function(model, options) { model.on('change', function(model, options) {
value = options.prefix + model.get('name'); value = options.prefix + model.get('name');
}); });
model.set({name: 'Bob'}, {prefix: 'Mr. '}); model.set({name: 'Bob'}, {prefix: 'Mr. '});
equal(value, 'Mr. Bob'); assert.equal(value, 'Mr. Bob');
model.set({name: 'Sue'}, {prefix: 'Ms. '}); model.set({name: 'Sue'}, {prefix: 'Ms. '});
equal(value, 'Ms. Sue'); assert.equal(value, 'Ms. Sue');
}); });
test("change after initialize", 1, function () { QUnit.test('change after initialize', function(assert) {
assert.expect(1);
var changed = 0; var changed = 0;
var attrs = {id: 1, label: 'c'}; var attrs = {id: 1, label: 'c'};
var obj = new Backbone.Model(attrs); var obj = new Backbone.Model(attrs);
obj.on('change', function() { changed += 1; }); obj.on('change', function() { changed += 1; });
obj.set(attrs); obj.set(attrs);
equal(changed, 0); assert.equal(changed, 0);
}); });
test("save within change event", 1, function () { QUnit.test('save within change event', function(assert) {
assert.expect(1);
var env = this; var env = this;
var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"}); var model = new Backbone.Model({firstName: 'Taylor', lastName: 'Swift'});
model.url = '/test'; model.url = '/test';
model.on('change', function () { model.on('change', function() {
model.save(); model.save();
ok(_.isEqual(env.syncArgs.model, model)); assert.ok(_.isEqual(env.syncArgs.model, model));
}); });
model.set({lastName: 'Hicks'}); model.set({lastName: 'Hicks'});
}); });
test("validate after save", 2, function() { QUnit.test('validate after save', function(assert) {
assert.expect(2);
var lastError, model = new Backbone.Model(); var lastError, model = new Backbone.Model();
model.validate = function(attrs) { model.validate = function(attrs) {
if (attrs.admin) return "Can't change admin status."; if (attrs.admin) return "Can't change admin status.";
}; };
model.sync = function(method, model, options) { model.sync = function(method, model, options) {
options.success.call(this, {admin: true}); options.success.call(this, {admin: true});
}; };
model.on('invalid', function(model, error) { model.on('invalid', function(model, error) {
lastError = error; lastError = error;
}); });
model.save(null); model.save(null);
equal(lastError, "Can't change admin status."); assert.equal(lastError, "Can't change admin status.");
equal(model.validationError, "Can't change admin status."); assert.equal(model.validationError, "Can't change admin status.");
}); });
test("save", 2, function() { QUnit.test('save', function(assert) {
doc.save({title : "Henry V"}); assert.expect(2);
equal(this.syncArgs.method, 'update'); doc.save({title: 'Henry V'});
ok(_.isEqual(this.syncArgs.model, doc)); assert.equal(this.syncArgs.method, 'update');
assert.ok(_.isEqual(this.syncArgs.model, doc));
}); });
test("save, fetch, destroy triggers error event when an error occurs", 3, func QUnit.test('save, fetch, destroy triggers error event when an error occurs', f
tion () { unction(assert) {
assert.expect(3);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('error', function () { model.on('error', function() {
ok(true); assert.ok(true);
}); });
model.sync = function (method, model, options) { model.sync = function(method, model, options) {
options.error(); options.error();
}; };
model.save({data: 2, id: 1}); model.save({data: 2, id: 1});
model.fetch(); model.fetch();
model.destroy(); model.destroy();
}); });
test("save with PATCH", function() { QUnit.test('#3283 - save, fetch, destroy calls success with context', function
(assert) {
assert.expect(3);
var model = new Backbone.Model();
var obj = {};
var options = {
context: obj,
success: function() {
assert.equal(this, obj);
}
};
model.sync = function(method, model, options) {
options.success.call(options.context);
};
model.save({data: 2, id: 1}, options);
model.fetch(options);
model.destroy(options);
});
QUnit.test('#3283 - save, fetch, destroy calls error with context', function(a
ssert) {
assert.expect(3);
var model = new Backbone.Model();
var obj = {};
var options = {
context: obj,
error: function() {
assert.equal(this, obj);
}
};
model.sync = function(method, model, options) {
options.error.call(options.context);
};
model.save({data: 2, id: 1}, options);
model.fetch(options);
model.destroy(options);
});
QUnit.test('#3470 - save and fetch with parse false', function(assert) {
assert.expect(2);
var i = 0;
var model = new Backbone.Model();
model.parse = function() {
assert.ok(false);
};
model.sync = function(method, model, options) {
options.success({i: ++i});
};
model.fetch({parse: false});
assert.equal(model.get('i'), i);
model.save(null, {parse: false});
assert.equal(model.get('i'), i);
});
QUnit.test('save with PATCH', function(assert) {
doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4}); doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
doc.save(); doc.save();
equal(this.syncArgs.method, 'update'); assert.equal(this.syncArgs.method, 'update');
equal(this.syncArgs.options.attrs, undefined); assert.equal(this.syncArgs.options.attrs, undefined);
doc.save({b: 2, d: 4}, {patch: true}); doc.save({b: 2, d: 4}, {patch: true});
equal(this.syncArgs.method, 'patch'); assert.equal(this.syncArgs.method, 'patch');
equal(_.size(this.syncArgs.options.attrs), 2); assert.equal(_.size(this.syncArgs.options.attrs), 2);
equal(this.syncArgs.options.attrs.d, 4); assert.equal(this.syncArgs.options.attrs.d, 4);
equal(this.syncArgs.options.attrs.a, undefined); assert.equal(this.syncArgs.options.attrs.a, undefined);
equal(this.ajaxSettings.data, "{\"b\":2,\"d\":4}"); assert.equal(this.ajaxSettings.data, '{"b":2,"d":4}');
});
QUnit.test('save with PATCH and different attrs', function(assert) {
doc.clear().save({b: 2, d: 4}, {patch: true, attrs: {B: 1, D: 3}});
assert.equal(this.syncArgs.options.attrs.D, 3);
assert.equal(this.syncArgs.options.attrs.d, undefined);
assert.equal(this.ajaxSettings.data, '{"B":1,"D":3}');
assert.deepEqual(doc.attributes, {b: 2, d: 4});
}); });
test("save in positional style", 1, function() { QUnit.test('save in positional style', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.sync = function(method, model, options) { model.sync = function(method, model, options) {
options.success(); options.success();
}; };
model.save('title', 'Twelfth Night'); model.save('title', 'Twelfth Night');
equal(model.get('title'), 'Twelfth Night'); assert.equal(model.get('title'), 'Twelfth Night');
}); });
test("save with non-object success response", 2, function () { QUnit.test('save with non-object success response', function(assert) {
assert.expect(2);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.sync = function(method, model, options) { model.sync = function(method, model, options) {
options.success('', options); options.success('', options);
options.success(null, options); options.success(null, options);
}; };
model.save({testing:'empty'}, { model.save({testing: 'empty'}, {
success: function (model) { success: function(model) {
deepEqual(model.attributes, {testing:'empty'}); assert.deepEqual(model.attributes, {testing: 'empty'});
} }
}); });
}); });
test("fetch", 2, function() { QUnit.test('save with wait and supplied id', function(assert) {
var Model = Backbone.Model.extend({
urlRoot: '/collection'
});
var model = new Model();
model.save({id: 42}, {wait: true});
assert.equal(this.ajaxSettings.url, '/collection/42');
});
QUnit.test('save will pass extra options to success callback', function(assert
) {
assert.expect(1);
var SpecialSyncModel = Backbone.Model.extend({
sync: function(method, model, options) {
_.extend(options, {specialSync: true});
return Backbone.Model.prototype.sync.call(this, method, model, options);
},
urlRoot: '/test'
});
var model = new SpecialSyncModel();
var onSuccess = function(model, response, options) {
assert.ok(options.specialSync, 'Options were passed correctly to callback'
);
};
model.save(null, {success: onSuccess});
this.ajaxSettings.success();
});
QUnit.test('fetch', function(assert) {
assert.expect(2);
doc.fetch(); doc.fetch();
equal(this.syncArgs.method, 'read'); assert.equal(this.syncArgs.method, 'read');
ok(_.isEqual(this.syncArgs.model, doc)); assert.ok(_.isEqual(this.syncArgs.model, doc));
}); });
test("destroy", 3, function() { QUnit.test('fetch will pass extra options to success callback', function(asser
t) {
assert.expect(1);
var SpecialSyncModel = Backbone.Model.extend({
sync: function(method, model, options) {
_.extend(options, {specialSync: true});
return Backbone.Model.prototype.sync.call(this, method, model, options);
},
urlRoot: '/test'
});
var model = new SpecialSyncModel();
var onSuccess = function(model, response, options) {
assert.ok(options.specialSync, 'Options were passed correctly to callback'
);
};
model.fetch({success: onSuccess});
this.ajaxSettings.success();
});
QUnit.test('destroy', function(assert) {
assert.expect(3);
doc.destroy(); doc.destroy();
equal(this.syncArgs.method, 'delete'); assert.equal(this.syncArgs.method, 'delete');
ok(_.isEqual(this.syncArgs.model, doc)); assert.ok(_.isEqual(this.syncArgs.model, doc));
var newModel = new Backbone.Model; var newModel = new Backbone.Model;
equal(newModel.destroy(), false); assert.equal(newModel.destroy(), false);
});
QUnit.test('destroy will pass extra options to success callback', function(ass
ert) {
assert.expect(1);
var SpecialSyncModel = Backbone.Model.extend({
sync: function(method, model, options) {
_.extend(options, {specialSync: true});
return Backbone.Model.prototype.sync.call(this, method, model, options);
},
urlRoot: '/test'
});
var model = new SpecialSyncModel({id: 'id'});
var onSuccess = function(model, response, options) {
assert.ok(options.specialSync, 'Options were passed correctly to callback'
);
};
model.destroy({success: onSuccess});
this.ajaxSettings.success();
}); });
test("non-persisted destroy", 1, function() { QUnit.test('non-persisted destroy', function(assert) {
var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3}); assert.expect(1);
a.sync = function() { throw "should not be called"; }; var a = new Backbone.Model({foo: 1, bar: 2, baz: 3});
a.sync = function() { throw 'should not be called'; };
a.destroy(); a.destroy();
ok(true, "non-persisted model should not call sync"); assert.ok(true, 'non-persisted model should not call sync');
}); });
test("validate", function() { QUnit.test('validate', function(assert) {
var lastError; var lastError;
var model = new Backbone.Model(); var model = new Backbone.Model();
model.validate = function(attrs) { model.validate = function(attrs) {
if (attrs.admin != this.get('admin')) return "Can't change admin status."; if (attrs.admin != this.get('admin')) return "Can't change admin status.";
}; };
model.on('invalid', function(model, error) { model.on('invalid', function(model, error) {
lastError = error; lastError = error;
}); });
var result = model.set({a: 100}); var result = model.set({a: 100});
equal(result, model); assert.equal(result, model);
equal(model.get('a'), 100); assert.equal(model.get('a'), 100);
equal(lastError, undefined); assert.equal(lastError, undefined);
result = model.set({admin: true}); result = model.set({admin: true});
equal(model.get('admin'), true); assert.equal(model.get('admin'), true);
result = model.set({a: 200, admin: false}, {validate:true}); result = model.set({a: 200, admin: false}, {validate: true});
equal(lastError, "Can't change admin status."); assert.equal(lastError, "Can't change admin status.");
equal(result, false); assert.equal(result, false);
equal(model.get('a'), 100); assert.equal(model.get('a'), 100);
}); });
test("validate on unset and clear", 6, function() { QUnit.test('validate on unset and clear', function(assert) {
assert.expect(6);
var error; var error;
var model = new Backbone.Model({name: "One"}); var model = new Backbone.Model({name: 'One'});
model.validate = function(attrs) { model.validate = function(attrs) {
if (!attrs.name) { if (!attrs.name) {
error = true; error = true;
return "No thanks."; return 'No thanks.';
} }
}; };
model.set({name: "Two"}); model.set({name: 'Two'});
equal(model.get('name'), 'Two'); assert.equal(model.get('name'), 'Two');
equal(error, undefined); assert.equal(error, undefined);
model.unset('name', {validate: true}); model.unset('name', {validate: true});
equal(error, true); assert.equal(error, true);
equal(model.get('name'), 'Two'); assert.equal(model.get('name'), 'Two');
model.clear({validate:true}); model.clear({validate: true});
equal(model.get('name'), 'Two'); assert.equal(model.get('name'), 'Two');
delete model.validate; delete model.validate;
model.clear(); model.clear();
equal(model.get('name'), undefined); assert.equal(model.get('name'), undefined);
}); });
test("validate with error callback", 8, function() { QUnit.test('validate with error callback', function(assert) {
assert.expect(8);
var lastError, boundError; var lastError, boundError;
var model = new Backbone.Model(); var model = new Backbone.Model();
model.validate = function(attrs) { model.validate = function(attrs) {
if (attrs.admin) return "Can't change admin status."; if (attrs.admin) return "Can't change admin status.";
}; };
model.on('invalid', function(model, error) { model.on('invalid', function(model, error) {
boundError = true; boundError = true;
}); });
var result = model.set({a: 100}, {validate:true}); var result = model.set({a: 100}, {validate: true});
equal(result, model); assert.equal(result, model);
equal(model.get('a'), 100); assert.equal(model.get('a'), 100);
equal(model.validationError, null); assert.equal(model.validationError, null);
equal(boundError, undefined); assert.equal(boundError, undefined);
result = model.set({a: 200, admin: true}, {validate:true}); result = model.set({a: 200, admin: true}, {validate: true});
equal(result, false); assert.equal(result, false);
equal(model.get('a'), 100); assert.equal(model.get('a'), 100);
equal(model.validationError, "Can't change admin status."); assert.equal(model.validationError, "Can't change admin status.");
equal(boundError, true); assert.equal(boundError, true);
}); });
test("defaults always extend attrs (#459)", 2, function() { QUnit.test('defaults always extend attrs (#459)', function(assert) {
assert.expect(2);
var Defaulted = Backbone.Model.extend({ var Defaulted = Backbone.Model.extend({
defaults: {one: 1}, defaults: {one: 1},
initialize : function(attrs, opts) { initialize: function(attrs, opts) {
equal(this.attributes.one, 1); assert.equal(this.attributes.one, 1);
} }
}); });
var providedattrs = new Defaulted({}); var providedattrs = new Defaulted({});
var emptyattrs = new Defaulted(); var emptyattrs = new Defaulted();
}); });
test("Inherit class properties", 6, function() { QUnit.test('Inherit class properties', function(assert) {
assert.expect(6);
var Parent = Backbone.Model.extend({ var Parent = Backbone.Model.extend({
instancePropSame: function() {}, instancePropSame: function() {},
instancePropDiff: function() {} instancePropDiff: function() {}
}, { }, {
classProp: function() {} classProp: function() {}
}); });
var Child = Parent.extend({ var Child = Parent.extend({
instancePropDiff: function() {} instancePropDiff: function() {}
}); });
var adult = new Parent; var adult = new Parent;
var kid = new Child; var kid = new Child;
equal(Child.classProp, Parent.classProp); assert.equal(Child.classProp, Parent.classProp);
notEqual(Child.classProp, undefined); assert.notEqual(Child.classProp, undefined);
equal(kid.instancePropSame, adult.instancePropSame); assert.equal(kid.instancePropSame, adult.instancePropSame);
notEqual(kid.instancePropSame, undefined); assert.notEqual(kid.instancePropSame, undefined);
notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff assert.notEqual(Child.prototype.instancePropDiff, Parent.prototype.instanceP
); ropDiff);
notEqual(Child.prototype.instancePropDiff, undefined); assert.notEqual(Child.prototype.instancePropDiff, undefined);
}); });
test("Nested change events don't clobber previous attributes", 4, function() { QUnit.test("Nested change events don't clobber previous attributes", function(
assert) {
assert.expect(4);
new Backbone.Model() new Backbone.Model()
.on('change:state', function(model, newState) { .on('change:state', function(model, newState) {
equal(model.previous('state'), undefined); assert.equal(model.previous('state'), undefined);
equal(newState, 'hello'); assert.equal(newState, 'hello');
// Fire a nested change event. // Fire a nested change event.
model.set({other: 'whatever'}); model.set({other: 'whatever'});
}) })
.on('change:state', function(model, newState) { .on('change:state', function(model, newState) {
equal(model.previous('state'), undefined); assert.equal(model.previous('state'), undefined);
equal(newState, 'hello'); assert.equal(newState, 'hello');
}) })
.set({state: 'hello'}); .set({state: 'hello'});
}); });
test("hasChanged/set should use same comparison", 2, function() { QUnit.test('hasChanged/set should use same comparison', function(assert) {
assert.expect(2);
var changed = 0, model = new Backbone.Model({a: null}); var changed = 0, model = new Backbone.Model({a: null});
model.on('change', function() { model.on('change', function() {
ok(this.hasChanged('a')); assert.ok(this.hasChanged('a'));
}) })
.on('change:a', function() { .on('change:a', function() {
changed++; changed++;
}) })
.set({a: undefined}); .set({a: undefined});
equal(changed, 1); assert.equal(changed, 1);
}); });
test("#582, #425, change:attribute callbacks should fire after all changes hav QUnit.test('#582, #425, change:attribute callbacks should fire after all chang
e occurred", 9, function() { es have occurred', function(assert) {
assert.expect(9);
var model = new Backbone.Model; var model = new Backbone.Model;
var assertion = function() { var assertion = function() {
equal(model.get('a'), 'a'); assert.equal(model.get('a'), 'a');
equal(model.get('b'), 'b'); assert.equal(model.get('b'), 'b');
equal(model.get('c'), 'c'); assert.equal(model.get('c'), 'c');
}; };
model.on('change:a', assertion); model.on('change:a', assertion);
model.on('change:b', assertion); model.on('change:b', assertion);
model.on('change:c', assertion); model.on('change:c', assertion);
model.set({a: 'a', b: 'b', c: 'c'}); model.set({a: 'a', b: 'b', c: 'c'});
}); });
test("#871, set with attributes property", 1, function() { QUnit.test('#871, set with attributes property', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.set({attributes: true}); model.set({attributes: true});
ok(model.has('attributes')); assert.ok(model.has('attributes'));
}); });
test("set value regardless of equality/change", 1, function() { QUnit.test('set value regardless of equality/change', function(assert) {
assert.expect(1);
var model = new Backbone.Model({x: []}); var model = new Backbone.Model({x: []});
var a = []; var a = [];
model.set({x: a}); model.set({x: a});
ok(model.get('x') === a); assert.ok(model.get('x') === a);
}); });
test("set same value does not trigger change", 0, function() { QUnit.test('set same value does not trigger change', function(assert) {
assert.expect(0);
var model = new Backbone.Model({x: 1}); var model = new Backbone.Model({x: 1});
model.on('change change:x', function() { ok(false); }); model.on('change change:x', function() { assert.ok(false); });
model.set({x: 1}); model.set({x: 1});
model.set({x: 1}); model.set({x: 1});
}); });
test("unset does not fire a change for undefined attributes", 0, function() { QUnit.test('unset does not fire a change for undefined attributes', function(a
ssert) {
assert.expect(0);
var model = new Backbone.Model({x: undefined}); var model = new Backbone.Model({x: undefined});
model.on('change:x', function(){ ok(false); }); model.on('change:x', function(){ assert.ok(false); });
model.unset('x'); model.unset('x');
}); });
test("set: undefined values", 1, function() { QUnit.test('set: undefined values', function(assert) {
assert.expect(1);
var model = new Backbone.Model({x: undefined}); var model = new Backbone.Model({x: undefined});
ok('x' in model.attributes); assert.ok('x' in model.attributes);
}); });
test("hasChanged works outside of change events, and true within", 6, function QUnit.test('hasChanged works outside of change events, and true within', funct
() { ion(assert) {
assert.expect(6);
var model = new Backbone.Model({x: 1}); var model = new Backbone.Model({x: 1});
model.on('change:x', function() { model.on('change:x', function() {
ok(model.hasChanged('x')); assert.ok(model.hasChanged('x'));
equal(model.get('x'), 1); assert.equal(model.get('x'), 1);
}); });
model.set({x: 2}, {silent: true}); model.set({x: 2}, {silent: true});
ok(model.hasChanged()); assert.ok(model.hasChanged());
equal(model.hasChanged('x'), true); assert.equal(model.hasChanged('x'), true);
model.set({x: 1}); model.set({x: 1});
ok(model.hasChanged()); assert.ok(model.hasChanged());
equal(model.hasChanged('x'), true); assert.equal(model.hasChanged('x'), true);
}); });
test("hasChanged gets cleared on the following set", 4, function() { QUnit.test('hasChanged gets cleared on the following set', function(assert) {
assert.expect(4);
var model = new Backbone.Model; var model = new Backbone.Model;
model.set({x: 1}); model.set({x: 1});
ok(model.hasChanged()); assert.ok(model.hasChanged());
model.set({x: 1}); model.set({x: 1});
ok(!model.hasChanged()); assert.ok(!model.hasChanged());
model.set({x: 2}); model.set({x: 2});
ok(model.hasChanged()); assert.ok(model.hasChanged());
model.set({}); model.set({});
ok(!model.hasChanged()); assert.ok(!model.hasChanged());
}); });
test("save with `wait` succeeds without `validate`", 1, function() { QUnit.test('save with `wait` succeeds without `validate`', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.url = '/test'; model.url = '/test';
model.save({x: 1}, {wait: true}); model.save({x: 1}, {wait: true});
ok(this.syncArgs.model === model); assert.ok(this.syncArgs.model === model);
}); });
test("save without `wait` doesn't set invalid attributes", function () { QUnit.test("save without `wait` doesn't set invalid attributes", function(asse rt) {
var model = new Backbone.Model(); var model = new Backbone.Model();
model.validate = function () { return 1; } model.validate = function() { return 1; };
model.save({a: 1}); model.save({a: 1});
equal(model.get('a'), void 0); assert.equal(model.get('a'), void 0);
}); });
test("save doesn't validate twice", function () { QUnit.test("save doesn't validate twice", function(assert) {
var model = new Backbone.Model(); var model = new Backbone.Model();
var times = 0; var times = 0;
model.sync = function () {}; model.sync = function() {};
model.validate = function () { ++times; } model.validate = function() { ++times; };
model.save({}); model.save({});
equal(times, 1); assert.equal(times, 1);
}); });
test("`hasChanged` for falsey keys", 2, function() { QUnit.test('`hasChanged` for falsey keys', function(assert) {
assert.expect(2);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.set({x: true}, {silent: true}); model.set({x: true}, {silent: true});
ok(!model.hasChanged(0)); assert.ok(!model.hasChanged(0));
ok(!model.hasChanged('')); assert.ok(!model.hasChanged(''));
}); });
test("`previous` for falsey keys", 2, function() { QUnit.test('`previous` for falsey keys', function(assert) {
assert.expect(2);
var model = new Backbone.Model({0: true, '': true}); var model = new Backbone.Model({0: true, '': true});
model.set({0: false, '': false}, {silent: true}); model.set({0: false, '': false}, {silent: true});
equal(model.previous(0), true); assert.equal(model.previous(0), true);
equal(model.previous(''), true); assert.equal(model.previous(''), true);
}); });
test("`save` with `wait` sends correct attributes", 5, function() { QUnit.test('`save` with `wait` sends correct attributes', function(assert) {
assert.expect(5);
var changed = 0; var changed = 0;
var model = new Backbone.Model({x: 1, y: 2}); var model = new Backbone.Model({x: 1, y: 2});
model.url = '/test'; model.url = '/test';
model.on('change:x', function() { changed++; }); model.on('change:x', function() { changed++; });
model.save({x: 3}, {wait: true}); model.save({x: 3}, {wait: true});
deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2}); assert.deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
equal(model.get('x'), 1); assert.equal(model.get('x'), 1);
equal(changed, 0); assert.equal(changed, 0);
this.syncArgs.options.success({}); this.syncArgs.options.success({});
equal(model.get('x'), 3); assert.equal(model.get('x'), 3);
equal(changed, 1); assert.equal(changed, 1);
}); });
test("a failed `save` with `wait` doesn't leave attributes behind", 1, functio QUnit.test("a failed `save` with `wait` doesn't leave attributes behind", func
n() { tion(assert) {
assert.expect(1);
var model = new Backbone.Model; var model = new Backbone.Model;
model.url = '/test'; model.url = '/test';
model.save({x: 1}, {wait: true}); model.save({x: 1}, {wait: true});
equal(model.get('x'), void 0); assert.equal(model.get('x'), void 0);
}); });
test("#1030 - `save` with `wait` results in correct attributes if success is c QUnit.test('#1030 - `save` with `wait` results in correct attributes if succes
alled during sync", 2, function() { s is called during sync', function(assert) {
assert.expect(2);
var model = new Backbone.Model({x: 1, y: 2}); var model = new Backbone.Model({x: 1, y: 2});
model.sync = function(method, model, options) { model.sync = function(method, model, options) {
options.success(); options.success();
}; };
model.on("change:x", function() { ok(true); }); model.on('change:x', function() { assert.ok(true); });
model.save({x: 3}, {wait: true}); model.save({x: 3}, {wait: true});
equal(model.get('x'), 3); assert.equal(model.get('x'), 3);
}); });
test("save with wait validates attributes", function() { QUnit.test('save with wait validates attributes', function(assert) {
var model = new Backbone.Model(); var model = new Backbone.Model();
model.url = '/test'; model.url = '/test';
model.validate = function() { ok(true); }; model.validate = function() { assert.ok(true); };
model.save({x: 1}, {wait: true}); model.save({x: 1}, {wait: true});
}); });
test("save turns on parse flag", function () { QUnit.test('save turns on parse flag', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
sync: function(method, model, options) { ok(options.parse); } sync: function(method, model, options) { assert.ok(options.parse); }
}); });
new Model().save(); new Model().save();
}); });
test("nested `set` during `'change:attr'`", 2, function() { QUnit.test("nested `set` during `'change:attr'`", function(assert) {
assert.expect(2);
var events = []; var events = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('all', function(event) { events.push(event); }); model.on('all', function(event) { events.push(event); });
model.on('change', function() { model.on('change', function() {
model.set({z: true}, {silent:true}); model.set({z: true}, {silent: true});
}); });
model.on('change:x', function() { model.on('change:x', function() {
model.set({y: true}); model.set({y: true});
}); });
model.set({x: true}); model.set({x: true});
deepEqual(events, ['change:y', 'change:x', 'change']); assert.deepEqual(events, ['change:y', 'change:x', 'change']);
events = []; events = [];
model.set({z: true}); model.set({z: true});
deepEqual(events, []); assert.deepEqual(events, []);
}); });
test("nested `change` only fires once", 1, function() { QUnit.test('nested `change` only fires once', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change', function() { model.on('change', function() {
ok(true); assert.ok(true);
model.set({x: true}); model.set({x: true});
}); });
model.set({x: true}); model.set({x: true});
}); });
test("nested `set` during `'change'`", 6, function() { QUnit.test("nested `set` during `'change'`", function(assert) {
assert.expect(6);
var count = 0; var count = 0;
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change', function() { model.on('change', function() {
switch(count++) { switch (count++) {
case 0: case 0:
deepEqual(this.changedAttributes(), {x: true}); assert.deepEqual(this.changedAttributes(), {x: true});
equal(model.previous('x'), undefined); assert.equal(model.previous('x'), undefined);
model.set({y: true}); model.set({y: true});
break; break;
case 1: case 1:
deepEqual(this.changedAttributes(), {x: true, y: true}); assert.deepEqual(this.changedAttributes(), {x: true, y: true});
equal(model.previous('x'), undefined); assert.equal(model.previous('x'), undefined);
model.set({z: true}); model.set({z: true});
break; break;
case 2: case 2:
deepEqual(this.changedAttributes(), {x: true, y: true, z: true}); assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true}
equal(model.previous('y'), undefined); );
assert.equal(model.previous('y'), undefined);
break; break;
default: default:
ok(false); assert.ok(false);
} }
}); });
model.set({x: true}); model.set({x: true});
}); });
test("nested `change` with silent", 3, function() { QUnit.test('nested `change` with silent', function(assert) {
assert.expect(3);
var count = 0; var count = 0;
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:y', function() { ok(false); }); model.on('change:y', function() { assert.ok(false); });
model.on('change', function() { model.on('change', function() {
switch(count++) { switch (count++) {
case 0: case 0:
deepEqual(this.changedAttributes(), {x: true}); assert.deepEqual(this.changedAttributes(), {x: true});
model.set({y: true}, {silent: true}); model.set({y: true}, {silent: true});
model.set({z: true}); model.set({z: true});
break; break;
case 1: case 1:
deepEqual(this.changedAttributes(), {x: true, y: true, z: true}); assert.deepEqual(this.changedAttributes(), {x: true, y: true, z: true} );
break; break;
case 2: case 2:
deepEqual(this.changedAttributes(), {z: false}); assert.deepEqual(this.changedAttributes(), {z: false});
break; break;
default: default:
ok(false); assert.ok(false);
} }
}); });
model.set({x: true}); model.set({x: true});
model.set({z: false}); model.set({z: false});
}); });
test("nested `change:attr` with silent", 0, function() { QUnit.test('nested `change:attr` with silent', function(assert) {
assert.expect(0);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:y', function(){ ok(false); }); model.on('change:y', function(){ assert.ok(false); });
model.on('change', function() { model.on('change', function() {
model.set({y: true}, {silent: true}); model.set({y: true}, {silent: true});
model.set({z: true}); model.set({z: true});
}); });
model.set({x: true}); model.set({x: true});
}); });
test("multiple nested changes with silent", 1, function() { QUnit.test('multiple nested changes with silent', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:x', function() { model.on('change:x', function() {
model.set({y: 1}, {silent: true}); model.set({y: 1}, {silent: true});
model.set({y: 2}); model.set({y: 2});
}); });
model.on('change:y', function(model, val) { model.on('change:y', function(model, val) {
equal(val, 2); assert.equal(val, 2);
}); });
model.set({x: true}); model.set({x: true});
}); });
test("multiple nested changes with silent", 1, function() { QUnit.test('multiple nested changes with silent', function(assert) {
assert.expect(1);
var changes = []; var changes = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:b', function(model, val) { changes.push(val); }); model.on('change:b', function(model, val) { changes.push(val); });
model.on('change', function() { model.on('change', function() {
model.set({b: 1}); model.set({b: 1});
}); });
model.set({b: 0}); model.set({b: 0});
deepEqual(changes, [0, 1]); assert.deepEqual(changes, [0, 1]);
}); });
test("basic silent change semantics", 1, function() { QUnit.test('basic silent change semantics', function(assert) {
assert.expect(1);
var model = new Backbone.Model; var model = new Backbone.Model;
model.set({x: 1}); model.set({x: 1});
model.on('change', function(){ ok(true); }); model.on('change', function(){ assert.ok(true); });
model.set({x: 2}, {silent: true}); model.set({x: 2}, {silent: true});
model.set({x: 1}); model.set({x: 1});
}); });
test("nested set multiple times", 1, function() { QUnit.test('nested set multiple times', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:b', function() { model.on('change:b', function() {
ok(true); assert.ok(true);
}); });
model.on('change:a', function() { model.on('change:a', function() {
model.set({b: true}); model.set({b: true});
model.set({b: true}); model.set({b: true});
}); });
model.set({a: true}); model.set({a: true});
}); });
test("#1122 - clear does not alter options.", 1, function() { QUnit.test('#1122 - clear does not alter options.', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
var options = {}; var options = {};
model.clear(options); model.clear(options);
ok(!options.unset); assert.ok(!options.unset);
}); });
test("#1122 - unset does not alter options.", 1, function() { QUnit.test('#1122 - unset does not alter options.', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
var options = {}; var options = {};
model.unset('x', options); model.unset('x', options);
ok(!options.unset); assert.ok(!options.unset);
}); });
test("#1355 - `options` is passed to success callbacks", 3, function() { QUnit.test('#1355 - `options` is passed to success callbacks', function(assert
) {
assert.expect(3);
var model = new Backbone.Model(); var model = new Backbone.Model();
var opts = { var opts = {
success: function( model, resp, options ) { success: function( model, resp, options ) {
ok(options); assert.ok(options);
} }
}; };
model.sync = function(method, model, options) { model.sync = function(method, model, options) {
options.success(); options.success();
}; };
model.save({id: 1}, opts); model.save({id: 1}, opts);
model.fetch(opts); model.fetch(opts);
model.destroy(opts); model.destroy(opts);
}); });
test("#1412 - Trigger 'sync' event.", 3, function() { QUnit.test("#1412 - Trigger 'sync' event.", function(assert) {
assert.expect(3);
var model = new Backbone.Model({id: 1}); var model = new Backbone.Model({id: 1});
model.sync = function (method, model, options) { options.success(); }; model.sync = function(method, model, options) { options.success(); };
model.on('sync', function(){ ok(true); }); model.on('sync', function(){ assert.ok(true); });
model.fetch(); model.fetch();
model.save(); model.save();
model.destroy(); model.destroy();
}); });
test("#1365 - Destroy: New models execute success callback.", 2, function() { QUnit.test('#1365 - Destroy: New models execute success callback.', function(a
ssert) {
var done = assert.async();
assert.expect(2);
new Backbone.Model() new Backbone.Model()
.on('sync', function() { ok(false); }) .on('sync', function() { assert.ok(false); })
.on('destroy', function(){ ok(true); }) .on('destroy', function(){ assert.ok(true); })
.destroy({ success: function(){ ok(true); }}); .destroy({success: function(){
assert.ok(true);
done();
}});
}); });
test("#1433 - Save: An invalid model cannot be persisted.", 1, function() { QUnit.test('#1433 - Save: An invalid model cannot be persisted.', function(ass
ert) {
assert.expect(1);
var model = new Backbone.Model; var model = new Backbone.Model;
model.validate = function(){ return 'invalid'; }; model.validate = function(){ return 'invalid'; };
model.sync = function(){ ok(false); }; model.sync = function(){ assert.ok(false); };
strictEqual(model.save(), false); assert.strictEqual(model.save(), false);
}); });
test("#1377 - Save without attrs triggers 'error'.", 1, function() { QUnit.test("#1377 - Save without attrs triggers 'error'.", function(assert) {
assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
url: '/test/', url: '/test/',
sync: function(method, model, options){ options.success(); }, sync: function(method, model, options){ options.success(); },
validate: function(){ return 'invalid'; } validate: function(){ return 'invalid'; }
}); });
var model = new Model({id: 1}); var model = new Model({id: 1});
model.on('invalid', function(){ ok(true); }); model.on('invalid', function(){ assert.ok(true); });
model.save(); model.save();
}); });
test("#1545 - `undefined` can be passed to a model constructor without coersio n", function() { QUnit.test('#1545 - `undefined` can be passed to a model constructor without c oersion', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
defaults: { one: 1 }, defaults: {one: 1},
initialize : function(attrs, opts) { initialize: function(attrs, opts) {
equal(attrs, undefined); assert.equal(attrs, undefined);
} }
}); });
var emptyattrs = new Model(); var emptyattrs = new Model();
var undefinedattrs = new Model(undefined); var undefinedattrs = new Model(undefined);
}); });
asyncTest("#1478 - Model `save` does not trigger change on unchanged attribute QUnit.test('#1478 - Model `save` does not trigger change on unchanged attribut
s", 0, function() { es', function(assert) {
var done = assert.async();
assert.expect(0);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, model, options) {
setTimeout(function(){ setTimeout(function(){
options.success(); options.success();
start(); done();
}, 0); }, 0);
} }
}); });
new Model({x: true}) new Model({x: true})
.on('change:x', function(){ ok(false); }) .on('change:x', function(){ assert.ok(false); })
.save(null, {wait: true}); .save(null, {wait: true});
}); });
test("#1664 - Changing from one value, silently to another, back to original t QUnit.test('#1664 - Changing from one value, silently to another, back to orig
riggers a change.", 1, function() { inal triggers a change.', function(assert) {
var model = new Backbone.Model({x:1}); assert.expect(1);
model.on('change:x', function() { ok(true); }); var model = new Backbone.Model({x: 1});
model.set({x:2},{silent:true}); model.on('change:x', function() { assert.ok(true); });
model.set({x:3},{silent:true}); model.set({x: 2}, {silent: true});
model.set({x:1}); model.set({x: 3}, {silent: true});
model.set({x: 1});
}); });
test("#1664 - multiple silent changes nested inside a change event", 2, functi QUnit.test('#1664 - multiple silent changes nested inside a change event', fun
on() { ction(assert) {
assert.expect(2);
var changes = []; var changes = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change', function() { model.on('change', function() {
model.set({a:'c'}, {silent:true}); model.set({a: 'c'}, {silent: true});
model.set({b:2}, {silent:true}); model.set({b: 2}, {silent: true});
model.unset('c', {silent:true}); model.unset('c', {silent: true});
}); });
model.on('change:a change:b change:c', function(model, val) { changes.push(v al); }); model.on('change:a change:b change:c', function(model, val) { changes.push(v al); });
model.set({a:'a', b:1, c:'item'}); model.set({a: 'a', b: 1, c: 'item'});
deepEqual(changes, ['a',1,'item']); assert.deepEqual(changes, ['a', 1, 'item']);
deepEqual(model.attributes, {a: 'c', b: 2}); assert.deepEqual(model.attributes, {a: 'c', b: 2});
}); });
test("#1791 - `attributes` is available for `parse`", function() { QUnit.test('#1791 - `attributes` is available for `parse`', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
parse: function() { this.has('a'); } // shouldn't throw an error parse: function() { this.has('a'); } // shouldn't throw an error
}); });
var model = new Model(null, {parse: true}); var model = new Model(null, {parse: true});
expect(0); assert.expect(0);
}); });
test("silent changes in last `change` event back to original triggers change", QUnit.test('silent changes in last `change` event back to original triggers ch
2, function() { ange', function(assert) {
assert.expect(2);
var changes = []; var changes = [];
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:a change:b change:c', function(model, val) { changes.push(v al); }); model.on('change:a change:b change:c', function(model, val) { changes.push(v al); });
model.on('change', function() { model.on('change', function() {
model.set({a:'c'}, {silent:true}); model.set({a: 'c'}, {silent: true});
}); });
model.set({a:'a'}); model.set({a: 'a'});
deepEqual(changes, ['a']); assert.deepEqual(changes, ['a']);
model.set({a:'a'}); model.set({a: 'a'});
deepEqual(changes, ['a', 'a']); assert.deepEqual(changes, ['a', 'a']);
}); });
test("#1943 change calculations should use _.isEqual", function() { QUnit.test('#1943 change calculations should use _.isEqual', function(assert) {
var model = new Backbone.Model({a: {key: 'value'}}); var model = new Backbone.Model({a: {key: 'value'}});
model.set('a', {key:'value'}, {silent:true}); model.set('a', {key: 'value'}, {silent: true});
equal(model.changedAttributes(), false); assert.equal(model.changedAttributes(), false);
}); });
test("#1964 - final `change` event is always fired, regardless of interim chan QUnit.test('#1964 - final `change` event is always fired, regardless of interi
ges", 1, function () { m changes', function(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change:property', function() { model.on('change:property', function() {
model.set('property', 'bar'); model.set('property', 'bar');
}); });
model.on('change', function() { model.on('change', function() {
ok(true); assert.ok(true);
}); });
model.set('property', 'foo'); model.set('property', 'foo');
}); });
test("isValid", function() { QUnit.test('isValid', function(assert) {
var model = new Backbone.Model({valid: true}); var model = new Backbone.Model({valid: true});
model.validate = function(attrs) { model.validate = function(attrs) {
if (!attrs.valid) return "invalid"; if (!attrs.valid) return 'invalid';
}; };
equal(model.isValid(), true); assert.equal(model.isValid(), true);
equal(model.set({valid: false}, {validate:true}), false); assert.equal(model.set({valid: false}, {validate: true}), false);
equal(model.isValid(), true); assert.equal(model.isValid(), true);
model.set({valid:false}); model.set({valid: false});
equal(model.isValid(), false); assert.equal(model.isValid(), false);
ok(!model.set('valid', false, {validate: true})); assert.ok(!model.set('valid', false, {validate: true}));
}); });
test("#1179 - isValid returns true in the absence of validate.", 1, function() QUnit.test('#1179 - isValid returns true in the absence of validate.', functio
{ n(assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.validate = null; model.validate = null;
ok(model.isValid()); assert.ok(model.isValid());
}); });
test("#1961 - Creating a model with {validate:true} will call validate and use the error callback", function () { QUnit.test('#1961 - Creating a model with {validate:true} will call validate a nd use the error callback', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
validate: function (attrs) { validate: function(attrs) {
if (attrs.id === 1) return "This shouldn't happen"; if (attrs.id === 1) return "This shouldn't happen";
} }
}); });
var model = new Model({id: 1}, {validate: true}); var model = new Model({id: 1}, {validate: true});
equal(model.validationError, "This shouldn't happen"); assert.equal(model.validationError, "This shouldn't happen");
}); });
test("toJSON receives attrs during save(..., {wait: true})", 1, function() { QUnit.test('toJSON receives attrs during save(..., {wait: true})', function(as
sert) {
assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
url: '/test', url: '/test',
toJSON: function() { toJSON: function() {
strictEqual(this.attributes.x, 1); assert.strictEqual(this.attributes.x, 1);
return _.clone(this.attributes); return _.clone(this.attributes);
} }
}); });
var model = new Model; var model = new Model;
model.save({x: 1}, {wait: true}); model.save({x: 1}, {wait: true});
}); });
test("#2034 - nested set with silent only triggers one change", 1, function() QUnit.test('#2034 - nested set with silent only triggers one change', function
{ (assert) {
assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
model.on('change', function() { model.on('change', function() {
model.set({b: true}, {silent: true}); model.set({b: true}, {silent: true});
ok(true); assert.ok(true);
}); });
model.set({a: true}); model.set({a: true});
}); });
QUnit.test('#3778 - id will only be updated if it is set', function(assert) {
assert.expect(2);
var model = new Backbone.Model({id: 1});
model.id = 2;
model.set({foo: 'bar'});
assert.equal(model.id, 2);
model.set({id: 3});
assert.equal(model.id, 3);
});
})(); })();
 End of changes. 255 change blocks. 
454 lines changed or deleted 754 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)