"Fossies" - the Fresh Open Source Software Archive  

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

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

collection.js  (lodash-4.0.0):collection.js  (lodash-4.17.21)
(function() { (function(QUnit) {
var a, b, c, d, e, col, otherCol; var a, b, c, d, e, col, otherCol;
QUnit.module('Backbone.Collection', { QUnit.module('Backbone.Collection', {
beforeEach: function(assert) { beforeEach: function(assert) {
a = new Backbone.Model({id: 3, label: 'a'}); a = new Backbone.Model({id: 3, label: 'a'});
b = new Backbone.Model({id: 2, label: 'b'}); b = new Backbone.Model({id: 2, label: 'b'});
c = new Backbone.Model({id: 1, label: 'c'}); c = new Backbone.Model({id: 1, label: 'c'});
d = new Backbone.Model({id: 0, label: 'd'}); d = new Backbone.Model({id: 0, label: 'd'});
skipping to change at line 24 skipping to change at line 24
otherCol = new Backbone.Collection(); otherCol = new Backbone.Collection();
} }
}); });
QUnit.test('new and sort', function(assert) { QUnit.test('new and sort', function(assert) {
assert.expect(6); assert.expect(6);
var counter = 0; var counter = 0;
col.on('sort', function(){ counter++; }); col.on('sort', function(){ counter++; });
assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']); assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']);
col.comparator = function(a, b) { col.comparator = function(m1, m2) {
return a.id > b.id ? -1 : 1; return m1.id > m2.id ? -1 : 1;
}; };
col.sort(); col.sort();
assert.equal(counter, 1); assert.equal(counter, 1);
assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']); assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']);
col.comparator = function(model) { return model.id; }; col.comparator = function(model) { return model.id; };
col.sort(); col.sort();
assert.equal(counter, 2); assert.equal(counter, 2);
assert.deepEqual(col.pluck('label'), ['d', 'c', 'b', 'a']); assert.deepEqual(col.pluck('label'), ['d', 'c', 'b', 'a']);
assert.equal(col.length, 4); assert.equal(col.length, 4);
}); });
skipping to change at line 92 skipping to change at line 92
assert.equal(col.get(2), b); assert.equal(col.get(2), b);
assert.equal(col.get({id: 1}), c); assert.equal(col.get({id: 1}), c);
assert.equal(col.get(c.clone()), c); assert.equal(col.get(c.clone()), c);
assert.equal(col.get(col.first().cid), col.first()); assert.equal(col.get(col.first().cid), col.first());
}); });
QUnit.test('get with non-default ids', function(assert) { QUnit.test('get with non-default ids', function(assert) {
assert.expect(5); assert.expect(5);
var MongoModel = Backbone.Model.extend({idAttribute: '_id'}); var MongoModel = Backbone.Model.extend({idAttribute: '_id'});
var model = new MongoModel({_id: 100}); var model = new MongoModel({_id: 100});
var col = new Backbone.Collection([model], {model: MongoModel}); var collection = new Backbone.Collection([model], {model: MongoModel});
assert.equal(col.get(100), model); assert.equal(collection.get(100), model);
assert.equal(col.get(model.cid), model); assert.equal(collection.get(model.cid), model);
assert.equal(col.get(model), model); assert.equal(collection.get(model), model);
assert.equal(col.get(101), void 0); assert.equal(collection.get(101), void 0);
var col2 = new Backbone.Collection(); var collection2 = new Backbone.Collection();
col2.model = MongoModel; collection2.model = MongoModel;
col2.add(model.attributes); collection2.add(model.attributes);
assert.equal(col2.get(model.clone()), col2.first()); assert.equal(collection2.get(model.clone()), collection2.first());
}); });
QUnit.test('get with "undefined" id', function(assert) { QUnit.test('has', function(assert) {
var collection = new Backbone.Collection([{id: 1}, {id: 'undefined'}]); assert.expect(15);
assert.equal(collection.get(1).id, 1); assert.ok(col.has(a));
assert.ok(col.has(b));
assert.ok(col.has(c));
assert.ok(col.has(d));
assert.ok(col.has(a.id));
assert.ok(col.has(b.id));
assert.ok(col.has(c.id));
assert.ok(col.has(d.id));
assert.ok(col.has(a.cid));
assert.ok(col.has(b.cid));
assert.ok(col.has(c.cid));
assert.ok(col.has(d.cid));
var outsider = new Backbone.Model({id: 4});
assert.notOk(col.has(outsider));
assert.notOk(col.has(outsider.id));
assert.notOk(col.has(outsider.cid));
}); });
QUnit.test('update index when id changes', function(assert) { QUnit.test('update index when id changes', function(assert) {
assert.expect(4); assert.expect(4);
var col = new Backbone.Collection(); var collection = new Backbone.Collection();
col.add([ collection.add([
{id: 0, name: 'one'}, {id: 0, name: 'one'},
{id: 1, name: 'two'} {id: 1, name: 'two'}
]); ]);
var one = col.get(0); var one = collection.get(0);
assert.equal(one.get('name'), 'one'); assert.equal(one.get('name'), 'one');
col.on('change:name', function(model) { assert.ok(this.get(model)); }); collection.on('change:name', function(model) { assert.ok(this.get(model)); } );
one.set({name: 'dalmatians', id: 101}); one.set({name: 'dalmatians', id: 101});
assert.equal(col.get(0), null); assert.equal(collection.get(0), null);
assert.equal(col.get(101).get('name'), 'dalmatians'); assert.equal(collection.get(101).get('name'), 'dalmatians');
}); });
QUnit.test('at', function(assert) { QUnit.test('at', function(assert) {
assert.expect(2); assert.expect(2);
assert.equal(col.at(2), c); assert.equal(col.at(2), c);
assert.equal(col.at(-2), c); assert.equal(col.at(-2), c);
}); });
QUnit.test('pluck', function(assert) { QUnit.test('pluck', function(assert) {
assert.expect(1); assert.expect(1);
skipping to change at line 181 skipping to change at line 196
coll.add([undefined, f, g]); coll.add([undefined, f, g]);
assert.equal(coll.length, 5); assert.equal(coll.length, 5);
assert.equal(addCount, 3); assert.equal(addCount, 3);
coll.add(new Array(4)); coll.add(new Array(4));
assert.equal(coll.length, 9); assert.equal(coll.length, 9);
assert.equal(addCount, 7); assert.equal(addCount, 7);
}); });
QUnit.test('add multiple models', function(assert) { QUnit.test('add multiple models', function(assert) {
assert.expect(6); assert.expect(6);
var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]); var collection = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at collection.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8
: 2}); }], {at: 2});
for (var i = 0; i <= 5; i++) { for (var i = 0; i <= 5; i++) {
assert.equal(col.at(i).get('at'), i); assert.equal(collection.at(i).get('at'), i);
} }
}); });
QUnit.test('add; at should have preference over comparator', function(assert) { QUnit.test('add; at should have preference over comparator', function(assert) {
assert.expect(1); assert.expect(1);
var Col = Backbone.Collection.extend({ var Col = Backbone.Collection.extend({
comparator: function(a, b) { comparator: function(m1, m2) {
return a.id > b.id ? -1 : 1; return m1.id > m2.id ? -1 : 1;
} }
}); });
var col = new Col([{id: 2}, {id: 3}]); var collection = new Col([{id: 2}, {id: 3}]);
col.add(new Backbone.Model({id: 1}), {at: 1}); collection.add(new Backbone.Model({id: 1}), {at: 1});
assert.equal(col.pluck('id').join(' '), '3 1 2'); assert.equal(collection.pluck('id').join(' '), '3 1 2');
}); });
QUnit.test('add; at should add to the end if the index is out of bounds', func tion(assert) { QUnit.test('add; at should add to the end if the index is out of bounds', func tion(assert) {
assert.expect(1); assert.expect(1);
var col = new Backbone.Collection([{id: 2}, {id: 3}]); var collection = new Backbone.Collection([{id: 2}, {id: 3}]);
col.add(new Backbone.Model({id: 1}), {at: 5}); collection.add(new Backbone.Model({id: 1}), {at: 5});
assert.equal(col.pluck('id').join(' '), '2 3 1'); assert.equal(collection.pluck('id').join(' '), '2 3 1');
}); });
QUnit.test("can't add model to collection twice", function(assert) { QUnit.test("can't add model to collection twice", function(assert) {
var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}
3}]); , {id: 3}]);
assert.equal(col.pluck('id').join(' '), '1 2 3'); assert.equal(collection.pluck('id').join(' '), '1 2 3');
}); });
QUnit.test("can't add different model with same id to collection twice", funct ion(assert) { QUnit.test("can't add different model with same id to collection twice", funct ion(assert) {
assert.expect(1); assert.expect(1);
var col = new Backbone.Collection; var collection = new Backbone.Collection;
col.unshift({id: 101}); collection.unshift({id: 101});
col.add({id: 101}); collection.add({id: 101});
assert.equal(col.length, 1); assert.equal(collection.length, 1);
}); });
QUnit.test('merge in duplicate models with {merge: true}', function(assert) { QUnit.test('merge in duplicate models with {merge: true}', function(assert) {
assert.expect(3); assert.expect(3);
var col = new Backbone.Collection; var collection = new Backbone.Collection;
col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry' collection.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name:
}]); 'Larry'}]);
col.add({id: 1, name: 'Moses'}); collection.add({id: 1, name: 'Moses'});
assert.equal(col.first().get('name'), 'Moe'); assert.equal(collection.first().get('name'), 'Moe');
col.add({id: 1, name: 'Moses'}, {merge: true}); collection.add({id: 1, name: 'Moses'}, {merge: true});
assert.equal(col.first().get('name'), 'Moses'); assert.equal(collection.first().get('name'), 'Moses');
col.add({id: 1, name: 'Tim'}, {merge: true, silent: true}); collection.add({id: 1, name: 'Tim'}, {merge: true, silent: true});
assert.equal(col.first().get('name'), 'Tim'); assert.equal(collection.first().get('name'), 'Tim');
}); });
QUnit.test('add model to multiple collections', function(assert) { QUnit.test('add model to multiple collections', function(assert) {
assert.expect(10); assert.expect(10);
var counter = 0; var counter = 0;
var e = new Backbone.Model({id: 10, label: 'e'}); var m = new Backbone.Model({id: 10, label: 'm'});
e.on('add', function(model, collection) { m.on('add', function(model, collection) {
counter++; counter++;
assert.equal(e, model); assert.equal(m, model);
if (counter > 1) { if (counter > 1) {
assert.equal(collection, colF); assert.equal(collection, col2);
} else { } else {
assert.equal(collection, colE); assert.equal(collection, col1);
} }
}); });
var colE = new Backbone.Collection([]); var col1 = new Backbone.Collection([]);
colE.on('add', function(model, collection) { col1.on('add', function(model, collection) {
assert.equal(e, model); assert.equal(m, model);
assert.equal(colE, collection); assert.equal(col1, collection);
}); });
var colF = new Backbone.Collection([]); var col2 = new Backbone.Collection([]);
colF.on('add', function(model, collection) { col2.on('add', function(model, collection) {
assert.equal(e, model); assert.equal(m, model);
assert.equal(colF, collection); assert.equal(col2, collection);
}); });
colE.add(e); col1.add(m);
assert.equal(e.collection, colE); assert.equal(m.collection, col1);
colF.add(e); col2.add(m);
assert.equal(e.collection, colE); assert.equal(m.collection, col1);
}); });
QUnit.test('add model with parse', function(assert) { QUnit.test('add model with parse', function(assert) {
assert.expect(1); assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
parse: function(obj) { parse: function(obj) {
obj.value += 1; obj.value += 1;
return obj; return obj;
} }
}); });
var Col = Backbone.Collection.extend({model: Model}); var Col = Backbone.Collection.extend({model: Model});
var col = new Col; var collection = new Col;
col.add({value: 1}, {parse: true}); collection.add({value: 1}, {parse: true});
assert.equal(col.at(0).get('value'), 2); assert.equal(collection.at(0).get('value'), 2);
}); });
QUnit.test('add with parse and merge', function(assert) { QUnit.test('add with parse and merge', function(assert) {
var collection = new Backbone.Collection(); var collection = new Backbone.Collection();
collection.parse = function(attrs) { collection.parse = function(attrs) {
return _.map(attrs, function(model) { return _.map(attrs, function(model) {
if (model.model) return model.model; if (model.model) return model.model;
return model; return model;
}); });
}; };
collection.add({id: 1}); collection.add({id: 1});
collection.add({model: {id: 1, name: 'Alf'}}, {parse: true, merge: true}); collection.add({model: {id: 1, name: 'Alf'}}, {parse: true, merge: true});
assert.equal(collection.first().get('name'), 'Alf'); assert.equal(collection.first().get('name'), 'Alf');
}); });
QUnit.test('add model to collection with sort()-style comparator', function(as sert) { QUnit.test('add model to collection with sort()-style comparator', function(as sert) {
assert.expect(3); assert.expect(3);
var col = new Backbone.Collection; var collection = new Backbone.Collection;
col.comparator = function(a, b) { collection.comparator = function(m1, m2) {
return a.get('name') < b.get('name') ? -1 : 1; return m1.get('name') < m2.get('name') ? -1 : 1;
}; };
var tom = new Backbone.Model({name: 'Tom'}); var tom = new Backbone.Model({name: 'Tom'});
var rob = new Backbone.Model({name: 'Rob'}); var rob = new Backbone.Model({name: 'Rob'});
var tim = new Backbone.Model({name: 'Tim'}); var tim = new Backbone.Model({name: 'Tim'});
col.add(tom); collection.add(tom);
col.add(rob); collection.add(rob);
col.add(tim); collection.add(tim);
assert.equal(col.indexOf(rob), 0); assert.equal(collection.indexOf(rob), 0);
assert.equal(col.indexOf(tim), 1); assert.equal(collection.indexOf(tim), 1);
assert.equal(col.indexOf(tom), 2); assert.equal(collection.indexOf(tom), 2);
}); });
QUnit.test('comparator that depends on `this`', function(assert) { QUnit.test('comparator that depends on `this`', function(assert) {
assert.expect(2); assert.expect(2);
var col = new Backbone.Collection; var collection = new Backbone.Collection;
col.negative = function(num) { collection.negative = function(num) {
return -num; return -num;
}; };
col.comparator = function(a) { collection.comparator = function(model) {
return this.negative(a.id); return this.negative(model.id);
}; };
col.add([{id: 1}, {id: 2}, {id: 3}]); collection.add([{id: 1}, {id: 2}, {id: 3}]);
assert.deepEqual(col.pluck('id'), [3, 2, 1]); assert.deepEqual(collection.pluck('id'), [3, 2, 1]);
col.comparator = function(a, b) { collection.comparator = function(m1, m2) {
return this.negative(b.id) - this.negative(a.id); return this.negative(m2.id) - this.negative(m1.id);
}; };
col.sort(); collection.sort();
assert.deepEqual(col.pluck('id'), [1, 2, 3]); assert.deepEqual(collection.pluck('id'), [1, 2, 3]);
}); });
QUnit.test('remove', function(assert) { QUnit.test('remove', function(assert) {
assert.expect(12); assert.expect(12);
var removed = null; var removed = null;
var result = null; var result = null;
col.on('remove', function(model, col, options) { col.on('remove', function(model, collection, options) {
removed = model.get('label'); removed = model.get('label');
assert.equal(options.index, 3); assert.equal(options.index, 3);
assert.equal(col.get(model), undefined, '#3693: model cannot be fetched fr om collection'); assert.equal(collection.get(model), undefined, '#3693: model cannot be fet ched from collection');
}); });
result = col.remove(d); result = col.remove(d);
assert.equal(removed, 'd'); assert.equal(removed, 'd');
assert.strictEqual(result, d); assert.strictEqual(result, d);
//if we try to remove d again, it's not going to actually get removed //if we try to remove d again, it's not going to actually get removed
result = col.remove(d); result = col.remove(d);
assert.strictEqual(result, undefined); assert.strictEqual(result, undefined);
assert.equal(col.length, 3); assert.equal(col.length, 3);
assert.equal(col.first(), a); assert.equal(col.first(), a);
col.off(); col.off();
skipping to change at line 362 skipping to change at line 377
assert.deepEqual(result, [], 'returns empty array when nothing removed'); assert.deepEqual(result, [], 'returns empty array when nothing removed');
}); });
QUnit.test('add and remove return values', function(assert) { QUnit.test('add and remove return values', function(assert) {
assert.expect(13); assert.expect(13);
var Even = Backbone.Model.extend({ var Even = Backbone.Model.extend({
validate: function(attrs) { validate: function(attrs) {
if (attrs.id % 2 !== 0) return 'odd'; if (attrs.id % 2 !== 0) return 'odd';
} }
}); });
var col = new Backbone.Collection; var collection = new Backbone.Collection;
col.model = Even; collection.model = Even;
var list = col.add([{id: 2}, {id: 4}], {validate: true}); var list = collection.add([{id: 2}, {id: 4}], {validate: true});
assert.equal(list.length, 2); assert.equal(list.length, 2);
assert.ok(list[0] instanceof Backbone.Model); assert.ok(list[0] instanceof Backbone.Model);
assert.equal(list[1], col.last()); assert.equal(list[1], collection.last());
assert.equal(list[1].get('id'), 4); assert.equal(list[1].get('id'), 4);
list = col.add([{id: 3}, {id: 6}], {validate: true}); list = collection.add([{id: 3}, {id: 6}], {validate: true});
assert.equal(col.length, 3); assert.equal(collection.length, 3);
assert.equal(list[0], false); assert.equal(list[0], false);
assert.equal(list[1].get('id'), 6); assert.equal(list[1].get('id'), 6);
var result = col.add({id: 6}); var result = collection.add({id: 6});
assert.equal(result.cid, list[1].cid); assert.equal(result.cid, list[1].cid);
result = col.remove({id: 6}); result = collection.remove({id: 6});
assert.equal(col.length, 2); assert.equal(collection.length, 2);
assert.equal(result.id, 6); assert.equal(result.id, 6);
list = col.remove([{id: 2}, {id: 8}]); list = collection.remove([{id: 2}, {id: 8}]);
assert.equal(col.length, 1); assert.equal(collection.length, 1);
assert.equal(list[0].get('id'), 2); assert.equal(list[0].get('id'), 2);
assert.equal(list[1], null); assert.equal(list[1], null);
}); });
QUnit.test('shift and pop', function(assert) { QUnit.test('shift and pop', function(assert) {
assert.expect(2); assert.expect(2);
var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); var collection = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
assert.equal(col.shift().get('a'), 'a'); assert.equal(collection.shift().get('a'), 'a');
assert.equal(col.pop().get('c'), 'c'); assert.equal(collection.pop().get('c'), 'c');
}); });
QUnit.test('slice', function(assert) { QUnit.test('slice', function(assert) {
assert.expect(2); assert.expect(2);
var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); var collection = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
var array = col.slice(1, 3); var array = collection.slice(1, 3);
assert.equal(array.length, 2); assert.equal(array.length, 2);
assert.equal(array[0].get('b'), 'b'); assert.equal(array[0].get('b'), 'b');
}); });
QUnit.test('events are unbound on remove', function(assert) { QUnit.test('events are unbound on remove', function(assert) {
assert.expect(3); assert.expect(3);
var counter = 0; var counter = 0;
var dj = new Backbone.Model(); var dj = new Backbone.Model();
var emcees = new Backbone.Collection([dj]); var emcees = new Backbone.Collection([dj]);
emcees.on('change', function(){ counter++; }); emcees.on('change', function(){ counter++; });
skipping to change at line 425 skipping to change at line 440
assert.equal(counter, 1); assert.equal(counter, 1);
}); });
QUnit.test('remove in multiple collections', function(assert) { QUnit.test('remove in multiple collections', function(assert) {
assert.expect(7); assert.expect(7);
var modelData = { var modelData = {
id: 5, id: 5,
title: 'Othello' title: 'Othello'
}; };
var passed = false; var passed = false;
var e = new Backbone.Model(modelData); var m1 = new Backbone.Model(modelData);
var f = new Backbone.Model(modelData); var m2 = new Backbone.Model(modelData);
f.on('remove', function() { m2.on('remove', function() {
passed = true; passed = true;
}); });
var colE = new Backbone.Collection([e]); var col1 = new Backbone.Collection([m1]);
var colF = new Backbone.Collection([f]); var col2 = new Backbone.Collection([m2]);
assert.ok(e != f); assert.notEqual(m1, m2);
assert.ok(colE.length === 1); assert.ok(col1.length === 1);
assert.ok(colF.length === 1); assert.ok(col2.length === 1);
colE.remove(e); col1.remove(m1);
assert.equal(passed, false); assert.equal(passed, false);
assert.ok(colE.length === 0); assert.ok(col1.length === 0);
colF.remove(e); col2.remove(m1);
assert.ok(colF.length === 0); assert.ok(col2.length === 0);
assert.equal(passed, true); assert.equal(passed, true);
}); });
QUnit.test('remove same model in multiple collection', function(assert) { QUnit.test('remove same model in multiple collection', function(assert) {
assert.expect(16); assert.expect(16);
var counter = 0; var counter = 0;
var e = new Backbone.Model({id: 5, title: 'Othello'}); var m = new Backbone.Model({id: 5, title: 'Othello'});
e.on('remove', function(model, collection) { m.on('remove', function(model, collection) {
counter++; counter++;
assert.equal(e, model); assert.equal(m, model);
if (counter > 1) { if (counter > 1) {
assert.equal(collection, colE); assert.equal(collection, col1);
} else { } else {
assert.equal(collection, colF); assert.equal(collection, col2);
} }
}); });
var colE = new Backbone.Collection([e]); var col1 = new Backbone.Collection([m]);
colE.on('remove', function(model, collection) { col1.on('remove', function(model, collection) {
assert.equal(e, model); assert.equal(m, model);
assert.equal(colE, collection); assert.equal(col1, collection);
}); });
var colF = new Backbone.Collection([e]); var col2 = new Backbone.Collection([m]);
colF.on('remove', function(model, collection) { col2.on('remove', function(model, collection) {
assert.equal(e, model); assert.equal(m, model);
assert.equal(colF, collection); assert.equal(col2, collection);
}); });
assert.equal(colE, e.collection); assert.equal(col1, m.collection);
colF.remove(e); col2.remove(m);
assert.ok(colF.length === 0); assert.ok(col2.length === 0);
assert.ok(colE.length === 1); assert.ok(col1.length === 1);
assert.equal(counter, 1); assert.equal(counter, 1);
assert.equal(colE, e.collection); assert.equal(col1, m.collection);
colE.remove(e); col1.remove(m);
assert.equal(null, e.collection); assert.equal(null, m.collection);
assert.ok(colE.length === 0); assert.ok(col1.length === 0);
assert.equal(counter, 2); assert.equal(counter, 2);
}); });
QUnit.test('model destroy removes from all collections', function(assert) { QUnit.test('model destroy removes from all collections', function(assert) {
assert.expect(3); assert.expect(3);
var e = new Backbone.Model({id: 5, title: 'Othello'}); var m = new Backbone.Model({id: 5, title: 'Othello'});
e.sync = function(method, model, options) { options.success(); }; m.sync = function(method, model, options) { options.success(); };
var colE = new Backbone.Collection([e]); var col1 = new Backbone.Collection([m]);
var colF = new Backbone.Collection([e]); var col2 = new Backbone.Collection([m]);
e.destroy(); m.destroy();
assert.ok(colE.length === 0); assert.ok(col1.length === 0);
assert.ok(colF.length === 0); assert.ok(col2.length === 0);
assert.equal(undefined, e.collection); assert.equal(undefined, m.collection);
}); });
QUnit.test('Collection: non-persisted model destroy removes from all collectio ns', function(assert) { QUnit.test('Collection: non-persisted model destroy removes from all collectio ns', function(assert) {
assert.expect(3); assert.expect(3);
var e = new Backbone.Model({title: 'Othello'}); var m = new Backbone.Model({title: 'Othello'});
e.sync = function(method, model, options) { throw 'should not be called'; }; m.sync = function(method, model, options) { throw 'should not be called'; };
var colE = new Backbone.Collection([e]); var col1 = new Backbone.Collection([m]);
var colF = new Backbone.Collection([e]); var col2 = new Backbone.Collection([m]);
e.destroy(); m.destroy();
assert.ok(colE.length === 0); assert.ok(col1.length === 0);
assert.ok(colF.length === 0); assert.ok(col2.length === 0);
assert.equal(undefined, e.collection); assert.equal(undefined, m.collection);
}); });
QUnit.test('fetch', function(assert) { QUnit.test('fetch', function(assert) {
assert.expect(4); assert.expect(4);
var collection = new Backbone.Collection; var collection = new Backbone.Collection;
collection.url = '/test'; collection.url = '/test';
collection.fetch(); collection.fetch();
assert.equal(this.syncArgs.method, 'read'); assert.equal(this.syncArgs.method, 'read');
assert.equal(this.syncArgs.model, collection); assert.equal(this.syncArgs.model, collection);
assert.equal(this.syncArgs.options.parse, true); assert.equal(this.syncArgs.options.parse, true);
skipping to change at line 535 skipping to change at line 550
QUnit.test('#3283 - fetch with an error response calls error with context', fu nction(assert) { QUnit.test('#3283 - fetch with an error response calls error with context', fu nction(assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection(); var collection = new Backbone.Collection();
var obj = {}; var obj = {};
var options = { var options = {
context: obj, context: obj,
error: function() { error: function() {
assert.equal(this, obj); assert.equal(this, obj);
} }
}; };
collection.sync = function(method, model, options) { collection.sync = function(method, model, opts) {
options.error.call(options.context); opts.error.call(opts.context);
}; };
collection.fetch(options); collection.fetch(options);
}); });
QUnit.test('ensure fetch only parses once', function(assert) { QUnit.test('ensure fetch only parses once', function(assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection; var collection = new Backbone.Collection;
var counter = 0; var counter = 0;
collection.parse = function(models) { collection.parse = function(models) {
counter++; counter++;
skipping to change at line 576 skipping to change at line 591
QUnit.test('create with validate:true enforces validation', function(assert) { QUnit.test('create with validate:true enforces validation', function(assert) {
assert.expect(3); assert.expect(3);
var ValidatingModel = Backbone.Model.extend({ var ValidatingModel = Backbone.Model.extend({
validate: function(attrs) { validate: function(attrs) {
return 'fail'; return 'fail';
} }
}); });
var ValidatingCollection = Backbone.Collection.extend({ var ValidatingCollection = Backbone.Collection.extend({
model: ValidatingModel model: ValidatingModel
}); });
var col = new ValidatingCollection(); var collection = new ValidatingCollection();
col.on('invalid', function(collection, error, options) { collection.on('invalid', function(coll, error, options) {
assert.equal(error, 'fail'); assert.equal(error, 'fail');
assert.equal(options.validationError, 'fail'); assert.equal(options.validationError, 'fail');
}); });
assert.equal(col.create({'foo': 'bar'}, {validate: true}), false); assert.equal(collection.create({foo: 'bar'}, {validate: true}), false);
}); });
QUnit.test('create will pass extra options to success callback', function(asse rt) { QUnit.test('create will pass extra options to success callback', function(asse rt) {
assert.expect(1); assert.expect(1);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, model, options) {
_.extend(options, {specialSync: true}); _.extend(options, {specialSync: true});
return Backbone.Model.prototype.sync.call(this, method, model, options); return Backbone.Model.prototype.sync.call(this, method, model, options);
} }
}); });
skipping to change at line 632 skipping to change at line 647
QUnit.test('a failing create returns model with errors', function(assert) { QUnit.test('a failing create returns model with errors', function(assert) {
var ValidatingModel = Backbone.Model.extend({ var ValidatingModel = Backbone.Model.extend({
validate: function(attrs) { validate: function(attrs) {
return 'fail'; return 'fail';
} }
}); });
var ValidatingCollection = Backbone.Collection.extend({ var ValidatingCollection = Backbone.Collection.extend({
model: ValidatingModel model: ValidatingModel
}); });
var col = new ValidatingCollection(); var collection = new ValidatingCollection();
var m = col.create({foo: 'bar'}); var m = collection.create({foo: 'bar'});
assert.equal(m.validationError, 'fail'); assert.equal(m.validationError, 'fail');
assert.equal(col.length, 1); assert.equal(collection.length, 1);
}); });
QUnit.test('initialize', function(assert) { QUnit.test('initialize', function(assert) {
assert.expect(1); assert.expect(1);
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
initialize: function() { initialize: function() {
this.one = 1; this.one = 1;
} }
}); });
var coll = new Collection; var coll = new Collection;
assert.equal(coll.one, 1); assert.equal(coll.one, 1);
}); });
QUnit.test('preinitialize', function(assert) {
assert.expect(1);
var Collection = Backbone.Collection.extend({
preinitialize: function() {
this.one = 1;
}
});
var coll = new Collection;
assert.equal(coll.one, 1);
});
QUnit.test('preinitialize occurs before the collection is set up', function(as
sert) {
assert.expect(2);
var Collection = Backbone.Collection.extend({
preinitialize: function() {
assert.notEqual(this.model, FooModel);
}
});
var FooModel = Backbone.Model.extend({id: 'foo'});
var coll = new Collection({}, {
model: FooModel
});
assert.equal(coll.model, FooModel);
});
QUnit.test('toJSON', function(assert) { QUnit.test('toJSON', function(assert) {
assert.expect(1); assert.expect(1);
assert.equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b" },{"id":1,"label":"c"},{"id":0,"label":"d"}]'); assert.equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b" },{"id":1,"label":"c"},{"id":0,"label":"d"}]');
}); });
QUnit.test('where and findWhere', function(assert) { QUnit.test('where and findWhere', function(assert) {
assert.expect(8); assert.expect(8);
var model = new Backbone.Model({a: 1}); var model = new Backbone.Model({a: 1});
var coll = new Backbone.Collection([ var coll = new Backbone.Collection([
model, model,
skipping to change at line 679 skipping to change at line 719
assert.equal(coll.where({a: 1, b: 2}).length, 1); assert.equal(coll.where({a: 1, b: 2}).length, 1);
assert.equal(coll.findWhere({a: 1}), model); assert.equal(coll.findWhere({a: 1}), model);
assert.equal(coll.findWhere({a: 4}), void 0); assert.equal(coll.findWhere({a: 4}), void 0);
}); });
QUnit.test('Underscore methods', function(assert) { QUnit.test('Underscore methods', function(assert) {
assert.expect(21); assert.expect(21);
assert.equal(col.map(function(model){ return model.get('label'); }).join(' ' ), 'a b c d'); assert.equal(col.map(function(model){ return model.get('label'); }).join(' ' ), 'a b c d');
assert.equal(col.some(function(model){ return model.id === 100; }), false); assert.equal(col.some(function(model){ return model.id === 100; }), false);
assert.equal(col.some(function(model){ return model.id === 0; }), true); assert.equal(col.some(function(model){ return model.id === 0; }), true);
assert.equal(col.reduce(function(a, b) {return a.id > b.id ? a : b;}).id, 3) assert.equal(col.reduce(function(m1, m2) {return m1.id > m2.id ? m1 : m2;}).
; id, 3);
assert.equal(col.reduceRight(function(a, b) {return a.id > b.id ? a : b;}).i assert.equal(col.reduceRight(function(m1, m2) {return m1.id > m2.id ? m1 : m
d, 3); 2;}).id, 3);
assert.equal(col.indexOf(b), 1); assert.equal(col.indexOf(b), 1);
assert.equal(col.size(), 4); assert.equal(col.size(), 4);
assert.equal(col.rest().length, 3); assert.equal(col.rest().length, 3);
assert.ok(!_.includes(col.rest(), a)); assert.ok(!_.includes(col.rest(), a));
assert.ok(_.includes(col.rest(), d)); assert.ok(_.includes(col.rest(), d));
assert.ok(!col.isEmpty()); assert.ok(!col.isEmpty());
assert.ok(!_.includes(col.without(d), d)); assert.ok(!_.includes(col.without(d), d));
var wrapped = col.chain(); var wrapped = col.chain();
assert.equal(wrapped.map('id').max().value(), 3); assert.equal(wrapped.map('id').max().value(), 3);
assert.equal(wrapped.map('id').min().value(), 0); assert.equal(wrapped.map('id').min().value(), 0);
assert.deepEqual(wrapped assert.deepEqual(wrapped
.filter(function(o){ return o.id % 2 === 0; }) .filter(function(o){ return o.id % 2 === 0; })
.map(function(o){ return o.id * 2; }) .map(function(o){ return o.id * 2; })
.value(), .value(),
[4, 0]); [4, 0]);
assert.deepEqual(col.difference([c, d]), [a, b]); assert.deepEqual(col.difference([c, d]), [a, b]);
assert.ok(col.includes(col.sample())); assert.ok(col.includes(col.sample()));
var first = col.first(); var first = col.first();
assert.deepEqual(col.groupBy(function(model){ return model.id; })[first.id], [first]); assert.deepEqual(col.groupBy(function(model){ return model.id; })[first.id], [first]);
assert.deepEqual(col.countBy(function(model){ return model.id; }), {0: 1, 1: 1, 2: 1, 3: 1}); assert.deepEqual(col.countBy(function(model){ return model.id; }), {0: 1, 1: 1, 2: 1, 3: 1});
assert.deepEqual(col.sortBy(function(model){ return model.id; })[0], col.at( 3)); assert.deepEqual(col.sortBy(function(model){ return model.id; })[0], col.at( 3));
assert.ok(col.indexBy('id')[first.id] === first); assert.ok(col.indexBy('id')[first.id] === first);
}); });
QUnit.test('Underscore methods with object-style and property-style iteratee', function(assert) { QUnit.test('Underscore methods with object-style and property-style iteratee', function(assert) {
assert.expect(26); assert.expect(26);
var model = new Backbone.Model({a: 4, b: 1, e: 3}); var model = new Backbone.Model({a: 4, b: 1, e: 3});
skipping to change at line 745 skipping to change at line 786
assert.deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1}); assert.deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1});
assert.deepEqual(coll.countBy('d'), {'undefined': 4}); assert.deepEqual(coll.countBy('d'), {'undefined': 4});
assert.equal(coll.findIndex({b: 1}), 0); assert.equal(coll.findIndex({b: 1}), 0);
assert.equal(coll.findIndex({b: 9}), -1); assert.equal(coll.findIndex({b: 9}), -1);
assert.equal(coll.findLastIndex({b: 1}), 3); assert.equal(coll.findLastIndex({b: 1}), 3);
assert.equal(coll.findLastIndex({b: 9}), -1); assert.equal(coll.findLastIndex({b: 9}), -1);
}); });
QUnit.test('reset', function(assert) { QUnit.test('reset', function(assert) {
assert.expect(16); assert.expect(16);
var resetCount = 0; var resetCount = 0;
var models = col.models; var models = col.models;
col.on('reset', function() { resetCount += 1; }); col.on('reset', function() { resetCount += 1; });
col.reset([]); col.reset([]);
assert.equal(resetCount, 1); assert.equal(resetCount, 1);
assert.equal(col.length, 0); assert.equal(col.length, 0);
assert.equal(col.last(), null); assert.equal(col.last(), null);
col.reset(models); col.reset(models);
assert.equal(resetCount, 2); assert.equal(resetCount, 2);
assert.equal(col.length, 4); assert.equal(col.length, 4);
skipping to change at line 776 skipping to change at line 818
col.reset([undefined, f]); col.reset([undefined, f]);
assert.equal(col.length, 2); assert.equal(col.length, 2);
assert.equal(resetCount, 5); assert.equal(resetCount, 5);
col.reset(new Array(4)); col.reset(new Array(4));
assert.equal(col.length, 4); assert.equal(col.length, 4);
assert.equal(resetCount, 6); assert.equal(resetCount, 6);
}); });
QUnit.test('reset with different values', function(assert) { QUnit.test('reset with different values', function(assert) {
var col = new Backbone.Collection({id: 1}); var collection = new Backbone.Collection({id: 1});
col.reset({id: 1, a: 1}); collection.reset({id: 1, a: 1});
assert.equal(col.get(1).get('a'), 1); assert.equal(collection.get(1).get('a'), 1);
}); });
QUnit.test('same references in reset', function(assert) { QUnit.test('same references in reset', function(assert) {
var model = new Backbone.Model({id: 1}); var model = new Backbone.Model({id: 1});
var collection = new Backbone.Collection({id: 1}); var collection = new Backbone.Collection({id: 1});
collection.reset(model); collection.reset(model);
assert.equal(collection.get(1), model); assert.equal(collection.get(1), model);
}); });
QUnit.test('reset passes caller options', function(assert) { QUnit.test('reset passes caller options', function(assert) {
assert.expect(3); assert.expect(3);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
initialize: function(attrs, options) { initialize: function(attrs, options) {
this.modelParameter = options.modelParameter; this.modelParameter = options.modelParameter;
} }
}); });
var col = new (Backbone.Collection.extend({model: Model}))(); var collection = new (Backbone.Collection.extend({model: Model}))();
col.reset([{astring: 'green', anumber: 1}, {astring: 'blue', anumber: 2}], { collection.reset([{astring: 'green', anumber: 1}, {astring: 'blue', anumber:
modelParameter: 'model parameter'}); 2}], {modelParameter: 'model parameter'});
assert.equal(col.length, 2); assert.equal(collection.length, 2);
col.each(function(model) { collection.each(function(model) {
assert.equal(model.modelParameter, 'model parameter'); assert.equal(model.modelParameter, 'model parameter');
}); });
}); });
QUnit.test('reset does not alter options by reference', function(assert) { QUnit.test('reset does not alter options by reference', function(assert) {
assert.expect(2); assert.expect(2);
var col = new Backbone.Collection([{id: 1}]); var collection = new Backbone.Collection([{id: 1}]);
var origOpts = {}; var origOpts = {};
col.on('reset', function(col, opts){ collection.on('reset', function(coll, opts){
assert.equal(origOpts.previousModels, undefined); assert.equal(origOpts.previousModels, undefined);
assert.equal(opts.previousModels[0].id, 1); assert.equal(opts.previousModels[0].id, 1);
}); });
col.reset([], origOpts); collection.reset([], origOpts);
}); });
QUnit.test('trigger custom events on models', function(assert) { QUnit.test('trigger custom events on models', function(assert) {
assert.expect(1); assert.expect(1);
var fired = null; var fired = null;
a.on('custom', function() { fired = true; }); a.on('custom', function() { fired = true; });
a.trigger('custom'); a.trigger('custom');
assert.equal(fired, true); assert.equal(fired, true);
}); });
skipping to change at line 848 skipping to change at line 890
assert.equal(this.collection, collection); assert.equal(this.collection, collection);
return this; return this;
} }
}); });
collection.model = Model; collection.model = Model;
collection.create({prop: 'value'}); collection.create({prop: 'value'});
}); });
QUnit.test('#574, remove its own reference to the .models array.', function(as sert) { QUnit.test('#574, remove its own reference to the .models array.', function(as sert) {
assert.expect(2); assert.expect(2);
var col = new Backbone.Collection([ var collection = new Backbone.Collection([
{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6} {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
]); ]);
assert.equal(col.length, 6); assert.equal(collection.length, 6);
col.remove(col.models); collection.remove(collection.models);
assert.equal(col.length, 0); assert.equal(collection.length, 0);
}); });
QUnit.test('#861, adding models to a collection which do not pass validation, with validate:true', function(assert) { QUnit.test('#861, adding models to a collection which do not pass validation, with validate:true', function(assert) {
assert.expect(2); assert.expect(2);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
validate: function(attrs) { validate: function(attrs) {
if (attrs.id == 3) return "id can't be 3"; if (attrs.id === 3) return "id can't be 3";
} }
}); });
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
model: Model model: Model
}); });
var collection = new Collection; var collection = new Collection;
collection.on('invalid', function() { assert.ok(true); }); collection.on('invalid', function() { assert.ok(true); });
skipping to change at line 893 skipping to change at line 935
collection.add([model, {id: 2}], {validate: true}); collection.add([model, {id: 2}], {validate: true});
model.trigger('test'); model.trigger('test');
assert.ok(collection.get(model.cid)); assert.ok(collection.get(model.cid));
assert.ok(collection.get(1)); assert.ok(collection.get(1));
assert.ok(!collection.get(2)); assert.ok(!collection.get(2));
assert.equal(collection.length, 1); assert.equal(collection.length, 1);
}); });
QUnit.test('multiple copies of the same model', function(assert) { QUnit.test('multiple copies of the same model', function(assert) {
assert.expect(3); assert.expect(3);
var col = new Backbone.Collection(); var collection = new Backbone.Collection();
var model = new Backbone.Model(); var model = new Backbone.Model();
col.add([model, model]); collection.add([model, model]);
assert.equal(col.length, 1); assert.equal(collection.length, 1);
col.add([{id: 1}, {id: 1}]); collection.add([{id: 1}, {id: 1}]);
assert.equal(col.length, 2); assert.equal(collection.length, 2);
assert.equal(col.last().id, 1); assert.equal(collection.last().id, 1);
}); });
QUnit.test('#964 - collection.get return inconsistent', function(assert) { QUnit.test('#964 - collection.get return inconsistent', function(assert) {
assert.expect(2); assert.expect(2);
var c = new Backbone.Collection(); var collection = new Backbone.Collection();
assert.ok(c.get(null) === undefined); assert.ok(collection.get(null) === undefined);
assert.ok(c.get() === undefined); assert.ok(collection.get() === undefined);
}); });
QUnit.test('#1112 - passing options.model sets collection.model', function(ass ert) { QUnit.test('#1112 - passing options.model sets collection.model', function(ass ert) {
assert.expect(2); assert.expect(2);
var Model = Backbone.Model.extend({}); var Model = Backbone.Model.extend({});
var c = new Backbone.Collection([{id: 1}], {model: Model}); var collection = new Backbone.Collection([{id: 1}], {model: Model});
assert.ok(c.model === Model); assert.ok(collection.model === Model);
assert.ok(c.at(0) instanceof Model); assert.ok(collection.at(0) instanceof Model);
}); });
QUnit.test('null and undefined are invalid ids.', function(assert) { QUnit.test('null and undefined are invalid ids.', function(assert) {
assert.expect(2); assert.expect(2);
var model = new Backbone.Model({id: 1}); var model = new Backbone.Model({id: 1});
var collection = new Backbone.Collection([model]); var collection = new Backbone.Collection([model]);
model.set({id: null}); model.set({id: null});
assert.ok(!collection.get('null')); assert.ok(!collection.get('null'));
model.set({id: 1}); model.set({id: 1});
model.set({id: undefined}); model.set({id: undefined});
assert.ok(!collection.get('undefined')); assert.ok(!collection.get('undefined'));
}); });
QUnit.test('falsy comparator', function(assert) { QUnit.test('falsy comparator', function(assert) {
assert.expect(4); assert.expect(4);
var Col = Backbone.Collection.extend({ var Col = Backbone.Collection.extend({
comparator: function(model){ return model.id; } comparator: function(model){ return model.id; }
}); });
var col = new Col(); var collection = new Col();
var colFalse = new Col(null, {comparator: false}); var colFalse = new Col(null, {comparator: false});
var colNull = new Col(null, {comparator: null}); var colNull = new Col(null, {comparator: null});
var colUndefined = new Col(null, {comparator: undefined}); var colUndefined = new Col(null, {comparator: undefined});
assert.ok(col.comparator); assert.ok(collection.comparator);
assert.ok(!colFalse.comparator); assert.ok(!colFalse.comparator);
assert.ok(!colNull.comparator); assert.ok(!colNull.comparator);
assert.ok(colUndefined.comparator); assert.ok(colUndefined.comparator);
}); });
QUnit.test('#1355 - `options` is passed to success callbacks', function(assert ) { QUnit.test('#1355 - `options` is passed to success callbacks', function(assert ) {
assert.expect(2); assert.expect(2);
var m = new Backbone.Model({x: 1}); var m = new Backbone.Model({x: 1});
var col = new Backbone.Collection(); var collection = new Backbone.Collection();
var opts = { var opts = {
opts: true, opts: true,
success: function(collection, resp, options) { success: function(coll, resp, options) {
assert.ok(options.opts); assert.ok(options.opts);
} }
}; };
col.sync = m.sync = function( method, collection, options ){ collection.sync = m.sync = function( method, coll, options ){
options.success({}); options.success({});
}; };
col.fetch(opts); collection.fetch(opts);
col.create(m, opts); collection.create(m, opts);
}); });
QUnit.test("#1412 - Trigger 'request' and 'sync' events.", function(assert) { QUnit.test("#1412 - Trigger 'request' and 'sync' events.", function(assert) {
assert.expect(4); assert.expect(4);
var collection = new Backbone.Collection; var collection = new Backbone.Collection;
collection.url = '/test'; collection.url = '/test';
Backbone.ajax = function(settings){ settings.success(); }; Backbone.ajax = function(settings){ settings.success(); };
collection.on('request', function(obj, xhr, options) { collection.on('request', function(obj, xhr, options) {
assert.ok(obj === collection, "collection has correct 'request' event afte r fetching"); assert.ok(obj === collection, "collection has correct 'request' event afte r fetching");
skipping to change at line 1008 skipping to change at line 1050
}; };
collection.fetch(options); collection.fetch(options);
collection.create({id: 1}, options); collection.create({id: 1}, options);
}); });
QUnit.test('#1447 - create with wait adds model.', function(assert) { QUnit.test('#1447 - create with wait adds model.', function(assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection; var collection = new Backbone.Collection;
var model = new Backbone.Model; var model = new Backbone.Model;
model.sync = function(method, model, options){ options.success(); }; model.sync = function(method, m, options){ options.success(); };
collection.on('add', function(){ assert.ok(true); }); collection.on('add', function(){ assert.ok(true); });
collection.create(model, {wait: true}); collection.create(model, {wait: true});
}); });
QUnit.test('#1448 - add sorts collection after merge.', function(assert) { QUnit.test('#1448 - add sorts collection after merge.', function(assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection([ var collection = new Backbone.Collection([
{id: 1, x: 1}, {id: 1, x: 1},
{id: 2, x: 2} {id: 2, x: 2}
]); ]);
skipping to change at line 1081 skipping to change at line 1123
assert.strictEqual(resp, model); assert.strictEqual(resp, model);
} }
}) })
}); });
new Collection().fetch(); new Collection().fetch();
this.ajaxSettings.success([model]); this.ajaxSettings.success([model]);
}); });
QUnit.test("`sort` shouldn't always fire on `add`", function(assert) { QUnit.test("`sort` shouldn't always fire on `add`", function(assert) {
assert.expect(1); assert.expect(1);
var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], { var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
comparator: 'id' comparator: 'id'
}); });
c.sort = function(){ assert.ok(true); }; collection.sort = function(){ assert.ok(true); };
c.add([]); collection.add([]);
c.add({id: 1}); collection.add({id: 1});
c.add([{id: 2}, {id: 3}]); collection.add([{id: 2}, {id: 3}]);
c.add({id: 4}); collection.add({id: 4});
}); });
QUnit.test('#1407 parse option on constructor parses collection and models', f unction(assert) { QUnit.test('#1407 parse option on constructor parses collection and models', f unction(assert) {
assert.expect(2); assert.expect(2);
var model = { var model = {
namespace: [{id: 1}, {id: 2}] namespace: [{id: 1}, {id: 2}]
}; };
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
model: Backbone.Model.extend({ model: Backbone.Model.extend({
parse: function(model) { parse: function(m) {
model.name = 'test'; m.name = 'test';
return model; return m;
} }
}), }),
parse: function(model) { parse: function(m) {
return model.namespace; return m.namespace;
} }
}); });
var c = new Collection(model, {parse: true}); var collection = new Collection(model, {parse: true});
assert.equal(c.length, 2); assert.equal(collection.length, 2);
assert.equal(c.at(0).get('name'), 'test'); assert.equal(collection.at(0).get('name'), 'test');
}); });
QUnit.test('#1407 parse option on reset parses collection and models', functio n(assert) { QUnit.test('#1407 parse option on reset parses collection and models', functio n(assert) {
assert.expect(2); assert.expect(2);
var model = { var model = {
namespace: [{id: 1}, {id: 2}] namespace: [{id: 1}, {id: 2}]
}; };
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
model: Backbone.Model.extend({ model: Backbone.Model.extend({
parse: function(model) { parse: function(m) {
model.name = 'test'; m.name = 'test';
return model; return m;
} }
}), }),
parse: function(model) { parse: function(m) {
return model.namespace; return m.namespace;
} }
}); });
var c = new Collection(); var collection = new Collection();
c.reset(model, {parse: true}); collection.reset(model, {parse: true});
assert.equal(c.length, 2); assert.equal(collection.length, 2);
assert.equal(c.at(0).get('name'), 'test'); assert.equal(collection.at(0).get('name'), 'test');
}); });
QUnit.test('Reset includes previous models in triggered event.', function(asse rt) { QUnit.test('Reset includes previous models in triggered event.', function(asse rt) {
assert.expect(1); assert.expect(1);
var model = new Backbone.Model(); var model = new Backbone.Model();
var collection = new Backbone.Collection([model]) var collection = new Backbone.Collection([model]);
.on('reset', function(collection, options) { collection.on('reset', function(coll, options) {
assert.deepEqual(options.previousModels, [model]); assert.deepEqual(options.previousModels, [model]);
}); });
collection.reset([]); collection.reset([]);
}); });
QUnit.test('set', function(assert) { QUnit.test('set', function(assert) {
var m1 = new Backbone.Model(); var m1 = new Backbone.Model();
var m2 = new Backbone.Model({id: 2}); var m2 = new Backbone.Model({id: 2});
var m3 = new Backbone.Model(); var m3 = new Backbone.Model();
var c = new Backbone.Collection([m1, m2]); var collection = new Backbone.Collection([m1, m2]);
// Test add/change/remove events // Test add/change/remove events
c.on('add', function(model) { collection.on('add', function(model) {
assert.strictEqual(model, m3); assert.strictEqual(model, m3);
}); });
c.on('change', function(model) { collection.on('change', function(model) {
assert.strictEqual(model, m2); assert.strictEqual(model, m2);
}); });
c.on('remove', function(model) { collection.on('remove', function(model) {
assert.strictEqual(model, m1); assert.strictEqual(model, m1);
}); });
// remove: false doesn't remove any models // remove: false doesn't remove any models
c.set([], {remove: false}); collection.set([], {remove: false});
assert.strictEqual(c.length, 2); assert.strictEqual(collection.length, 2);
// add: false doesn't add any models // add: false doesn't add any models
c.set([m1, m2, m3], {add: false}); collection.set([m1, m2, m3], {add: false});
assert.strictEqual(c.length, 2); assert.strictEqual(collection.length, 2);
// merge: false doesn't change any models // merge: false doesn't change any models
c.set([m1, {id: 2, a: 1}], {merge: false}); collection.set([m1, {id: 2, a: 1}], {merge: false});
assert.strictEqual(m2.get('a'), void 0); assert.strictEqual(m2.get('a'), void 0);
// add: false, remove: false only merges existing models // add: false, remove: false only merges existing models
c.set([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false}); collection.set([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false}
assert.strictEqual(c.length, 2); );
assert.strictEqual(collection.length, 2);
assert.strictEqual(m2.get('a'), 0); assert.strictEqual(m2.get('a'), 0);
// default options add/remove/merge as appropriate // default options add/remove/merge as appropriate
c.set([{id: 2, a: 1}, m3]); collection.set([{id: 2, a: 1}, m3]);
assert.strictEqual(c.length, 2); assert.strictEqual(collection.length, 2);
assert.strictEqual(m2.get('a'), 1); assert.strictEqual(m2.get('a'), 1);
// Test removing models not passing an argument // Test removing models not passing an argument
c.off('remove').on('remove', function(model) { collection.off('remove').on('remove', function(model) {
assert.ok(model === m2 || model === m3); assert.ok(model === m2 || model === m3);
}); });
c.set([]); collection.set([]);
assert.strictEqual(c.length, 0); assert.strictEqual(collection.length, 0);
// Test null models on set doesn't clear collection // Test null models on set doesn't clear collection
c.off(); collection.off();
c.set([{id: 1}]); collection.set([{id: 1}]);
c.set(); collection.set();
assert.strictEqual(c.length, 1); assert.strictEqual(collection.length, 1);
}); });
QUnit.test('set with only cids', function(assert) { QUnit.test('set with only cids', function(assert) {
assert.expect(3); assert.expect(3);
var m1 = new Backbone.Model; var m1 = new Backbone.Model;
var m2 = new Backbone.Model; var m2 = new Backbone.Model;
var c = new Backbone.Collection; var collection = new Backbone.Collection;
c.set([m1, m2]); collection.set([m1, m2]);
assert.equal(c.length, 2); assert.equal(collection.length, 2);
c.set([m1]); collection.set([m1]);
assert.equal(c.length, 1); assert.equal(collection.length, 1);
c.set([m1, m1, m1, m2, m2], {remove: false}); collection.set([m1, m1, m1, m2, m2], {remove: false});
assert.equal(c.length, 2); assert.equal(collection.length, 2);
}); });
QUnit.test('set with only idAttribute', function(assert) { QUnit.test('set with only idAttribute', function(assert) {
assert.expect(3); assert.expect(3);
var m1 = {_id: 1}; var m1 = {_id: 1};
var m2 = {_id: 2}; var m2 = {_id: 2};
var Col = Backbone.Collection.extend({ var Col = Backbone.Collection.extend({
model: Backbone.Model.extend({ model: Backbone.Model.extend({
idAttribute: '_id' idAttribute: '_id'
}) })
}); });
var c = new Col; var collection = new Col;
c.set([m1, m2]); collection.set([m1, m2]);
assert.equal(c.length, 2); assert.equal(collection.length, 2);
c.set([m1]); collection.set([m1]);
assert.equal(c.length, 1); assert.equal(collection.length, 1);
c.set([m1, m1, m1, m2, m2], {remove: false}); collection.set([m1, m1, m1, m2, m2], {remove: false});
assert.equal(c.length, 2); assert.equal(collection.length, 2);
}); });
QUnit.test('set + merge with default values defined', function(assert) { QUnit.test('set + merge with default values defined', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
defaults: { defaults: {
key: 'value' key: 'value'
} }
}); });
var m = new Model({id: 1}); var m = new Model({id: 1});
var col = new Backbone.Collection([m], {model: Model}); var collection = new Backbone.Collection([m], {model: Model});
assert.equal(col.first().get('key'), 'value'); assert.equal(collection.first().get('key'), 'value');
col.set({id: 1, key: 'other'}); collection.set({id: 1, key: 'other'});
assert.equal(col.first().get('key'), 'other'); assert.equal(collection.first().get('key'), 'other');
col.set({id: 1, other: 'value'}); collection.set({id: 1, other: 'value'});
assert.equal(col.first().get('key'), 'other'); assert.equal(collection.first().get('key'), 'other');
assert.equal(col.length, 1); assert.equal(collection.length, 1);
}); });
QUnit.test('merge without mutation', function(assert) { QUnit.test('merge without mutation', function(assert) {
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
initialize: function(attrs, options) { initialize: function(attrs, options) {
if (attrs.child) { if (attrs.child) {
this.set('child', new Model(attrs.child, options), options); this.set('child', new Model(attrs.child, options), options);
} }
} }
}); });
skipping to change at line 1323 skipping to change at line 1365
assert.expect(0); assert.expect(0);
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
comparator: 'id', comparator: 'id',
sort: function() { assert.ok(false); } sort: function() { assert.ok(false); }
}); });
new Collection().push({id: 1}); new Collection().push({id: 1});
}); });
QUnit.test('#2428 - push duplicate models, return the correct one', function(a ssert) { QUnit.test('#2428 - push duplicate models, return the correct one', function(a ssert) {
assert.expect(1); assert.expect(1);
var col = new Backbone.Collection; var collection = new Backbone.Collection;
var model1 = col.push({id: 101}); var model1 = collection.push({id: 101});
var model2 = col.push({id: 101}); var model2 = collection.push({id: 101});
assert.ok(model2.cid == model1.cid); assert.ok(model2.cid === model1.cid);
}); });
QUnit.test('`set` with non-normal id', function(assert) { QUnit.test('`set` with non-normal id', function(assert) {
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
model: Backbone.Model.extend({idAttribute: '_id'}) model: Backbone.Model.extend({idAttribute: '_id'})
}); });
var collection = new Collection({_id: 1}); var collection = new Collection({_id: 1});
collection.set([{_id: 1, a: 1}], {add: false}); collection.set([{_id: 1, a: 1}], {add: false});
assert.equal(collection.first().get('a'), 1); assert.equal(collection.first().get('a'), 1);
}); });
skipping to change at line 1391 skipping to change at line 1433
var SpecialSyncCollection = Backbone.Collection.extend({ var SpecialSyncCollection = Backbone.Collection.extend({
url: '/test', url: '/test',
sync: function(method, collection, options) { sync: function(method, collection, options) {
_.extend(options, {specialSync: true}); _.extend(options, {specialSync: true});
return Backbone.Collection.prototype.sync.call(this, method, collection, options); return Backbone.Collection.prototype.sync.call(this, method, collection, options);
} }
}); });
var collection = new SpecialSyncCollection(); var collection = new SpecialSyncCollection();
var onSuccess = function(collection, resp, options) { var onSuccess = function(coll, resp, options) {
assert.ok(options.specialSync, 'Options were passed correctly to callback' ); assert.ok(options.specialSync, 'Options were passed correctly to callback' );
}; };
collection.fetch({success: onSuccess}); collection.fetch({success: onSuccess});
this.ajaxSettings.success(); this.ajaxSettings.success();
}); });
QUnit.test('`add` only `sort`s when necessary', function(assert) { QUnit.test('`add` only `sort`s when necessary', function(assert) {
assert.expect(2); assert.expect(2);
var collection = new (Backbone.Collection.extend({ var collection = new (Backbone.Collection.extend({
skipping to change at line 1416 skipping to change at line 1458
collection.add({id: 1, a: 1}, {merge: true}); // do sort, comparator change collection.add({id: 1, a: 1}, {merge: true}); // do sort, comparator change
collection.add({id: 1, b: 1}, {merge: true}); // don't sort, no comparator c hange collection.add({id: 1, b: 1}, {merge: true}); // don't sort, no comparator c hange
collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no comparator c hange collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no comparator c hange
collection.add(collection.models); // don't sort, nothing new collection.add(collection.models); // don't sort, nothing new
collection.add(collection.models, {merge: true}); // don't sort collection.add(collection.models, {merge: true}); // don't sort
}); });
QUnit.test('`add` only `sort`s when necessary with comparator function', funct ion(assert) { QUnit.test('`add` only `sort`s when necessary with comparator function', funct ion(assert) {
assert.expect(3); assert.expect(3);
var collection = new (Backbone.Collection.extend({ var collection = new (Backbone.Collection.extend({
comparator: function(a, b) { comparator: function(m1, m2) {
return a.get('a') > b.get('a') ? 1 : (a.get('a') < b.get('a') ? -1 : 0); return m1.get('a') > m2.get('a') ? 1 : (m1.get('a') < m2.get('a') ? -1 :
0);
} }
}))([{id: 1}, {id: 2}, {id: 3}]); }))([{id: 1}, {id: 2}, {id: 3}]);
collection.on('sort', function() { assert.ok(true); }); collection.on('sort', function() { assert.ok(true); });
collection.add({id: 4}); // do sort, new model collection.add({id: 4}); // do sort, new model
collection.add({id: 1, a: 1}, {merge: true}); // do sort, model change collection.add({id: 1, a: 1}, {merge: true}); // do sort, model change
collection.add({id: 1, b: 1}, {merge: true}); // do sort, model change collection.add({id: 1, b: 1}, {merge: true}); // do sort, model change
collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no model change collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no model change
collection.add(collection.models); // don't sort, nothing new collection.add(collection.models); // don't sort, nothing new
collection.add(collection.models, {merge: true}); // don't sort collection.add(collection.models, {merge: true}); // don't sort
}); });
skipping to change at line 1454 skipping to change at line 1496
assert.expect(9); assert.expect(9);
var opts = {a: 1, b: 2}; var opts = {a: 1, b: 2};
_.forEach([undefined, null, false], function(falsey) { _.forEach([undefined, null, false], function(falsey) {
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
initialize: function(models, options) { initialize: function(models, options) {
assert.strictEqual(models, falsey); assert.strictEqual(models, falsey);
assert.strictEqual(options, opts); assert.strictEqual(options, opts);
} }
}); });
var col = new Collection(falsey, opts); var collection = new Collection(falsey, opts);
assert.strictEqual(col.length, 0); assert.strictEqual(collection.length, 0);
}); });
}); });
QUnit.test('`add` overrides `set` flags', function(assert) { QUnit.test('`add` overrides `set` flags', function(assert) {
var collection = new Backbone.Collection(); var collection = new Backbone.Collection();
collection.once('add', function(model, collection, options) { collection.once('add', function(model, coll, options) {
collection.add({id: 2}, options); coll.add({id: 2}, options);
}); });
collection.set({id: 1}); collection.set({id: 1});
assert.equal(collection.length, 2); assert.equal(collection.length, 2);
}); });
QUnit.test('#2606 - Collection#create, success arguments', function(assert) { QUnit.test('#2606 - Collection#create, success arguments', function(assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection; var collection = new Backbone.Collection;
collection.url = 'test'; collection.url = 'test';
collection.create({}, { collection.create({}, {
skipping to change at line 1563 skipping to change at line 1605
assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'Three'); assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'Three');
job.set(job.parse(newData, {parse: true})); job.set(job.parse(newData, {parse: true}));
assert.equal(job.get('name'), 'NewJobName'); assert.equal(job.get('name'), 'NewJobName');
assert.equal(job.items.at(0).get('name'), 'NewSub1'); assert.equal(job.items.at(0).get('name'), 'NewSub1');
assert.equal(job.items.length, 2); assert.equal(job.items.length, 2);
assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'NewOne'); assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'NewOne');
assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'NewThree'); assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'NewThree');
}); });
QUnit.test('_addReference binds all collection events & adds to the lookup has hes', function(assert) { QUnit.test('_addReference binds all collection events & adds to the lookup has hes', function(assert) {
assert.expect(9); assert.expect(8);
var calls = {add: 0, remove: 0}; var calls = {add: 0, remove: 0};
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
_addReference: function(model) { _addReference: function(model) {
Backbone.Collection.prototype._addReference.apply(this, arguments); Backbone.Collection.prototype._addReference.apply(this, arguments);
calls.add++; calls.add++;
assert.equal(model, this._byId[model.id]); assert.equal(model, this._byId[model.id]);
assert.equal(model, this._byId[model.cid]); assert.equal(model, this._byId[model.cid]);
assert.equal(model._events.all.length, 1); assert.equal(model._events.all.length, 1);
}, },
_removeReference: function(model) { _removeReference: function(model) {
Backbone.Collection.prototype._removeReference.apply(this, arguments); Backbone.Collection.prototype._removeReference.apply(this, arguments);
calls.remove++; calls.remove++;
assert.equal(this._byId[model.id], void 0); assert.equal(this._byId[model.id], void 0);
assert.equal(this._byId[model.cid], void 0); assert.equal(this._byId[model.cid], void 0);
assert.equal(model.collection, void 0); assert.equal(model.collection, void 0);
assert.equal(model._events, void 0);
} }
}); });
var collection = new Collection(); var collection = new Collection();
var model = collection.add({id: 1}); var model = collection.add({id: 1});
collection.remove(model); collection.remove(model);
assert.equal(calls.add, 1); assert.equal(calls.add, 1);
assert.equal(calls.remove, 1); assert.equal(calls.remove, 1);
}); });
QUnit.test('Do not allow duplicate models to be `add`ed or `set`', function(as sert) { QUnit.test('Do not allow duplicate models to be `add`ed or `set`', function(as sert) {
var c = new Backbone.Collection(); var collection = new Backbone.Collection();
collection.add([{id: 1}, {id: 1}]);
assert.equal(collection.length, 1);
assert.equal(collection.models.length, 1);
c.add([{id: 1}, {id: 1}]); collection.set([{id: 1}, {id: 1}]);
assert.equal(c.length, 1); assert.equal(collection.length, 1);
assert.equal(c.models.length, 1); assert.equal(collection.models.length, 1);
c.set([{id: 1}, {id: 1}]);
assert.equal(c.length, 1);
assert.equal(c.models.length, 1);
}); });
QUnit.test('#3020: #set with {add: false} should not throw.', function(assert) { QUnit.test('#3020: #set with {add: false} should not throw.', function(assert) {
assert.expect(2); assert.expect(2);
var collection = new Backbone.Collection; var collection = new Backbone.Collection;
collection.set([{id: 1}], {add: false}); collection.set([{id: 1}], {add: false});
assert.strictEqual(collection.length, 0); assert.strictEqual(collection.length, 0);
assert.strictEqual(collection.models.length, 0); assert.strictEqual(collection.models.length, 0);
}); });
skipping to change at line 1688 skipping to change at line 1729
} }
}); });
collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]); collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]);
assert.equal(collection.length, 2); assert.equal(collection.length, 2);
assert.ok(collection.at(0) instanceof A); assert.ok(collection.at(0) instanceof A);
assert.equal(collection.at(0), collection.get('a-1')); assert.equal(collection.at(0), collection.get('a-1'));
assert.ok(collection.at(1) instanceof B); assert.ok(collection.at(1) instanceof B);
assert.equal(collection.at(1), collection.get('b-1')); assert.equal(collection.at(1), collection.get('b-1'));
}); });
QUnit.test('#3039: adding at index fires with correct at', function(assert) { QUnit.test('Collection with polymorphic models receives default id from modelI
assert.expect(3); d', function(assert) {
var col = new Backbone.Collection([{at: 0}, {at: 4}]); assert.expect(6);
col.on('add', function(model, col, options) { // When the polymorphic models use 'id' for the idAttribute, all is fine.
assert.equal(model.get('at'), options.index); var C1 = Backbone.Collection.extend({
model: function(attrs) {
return new Backbone.Model(attrs);
}
});
var c1 = new C1({id: 1});
assert.equal(c1.get(1).id, 1);
assert.equal(c1.modelId({id: 1}), 1);
// If the polymorphic models define their own idAttribute,
// the modelId method should be overridden, for the reason below.
var M = Backbone.Model.extend({
idAttribute: '_id'
});
var C2 = Backbone.Collection.extend({
model: function(attrs) {
return new M(attrs);
}
}); });
col.add([{at: 1}, {at: 2}, {at: 3}], {at: 1}); var c2 = new C2({_id: 1});
assert.equal(c2.get(1), void 0);
assert.equal(c2.modelId(c2.at(0).attributes), void 0);
var m = new M({_id: 2});
c2.add(m);
assert.equal(c2.get(2), void 0);
assert.equal(c2.modelId(m.attributes), void 0);
});
QUnit.test('#3039 #3951: adding at index fires with correct at', function(asse
rt) {
assert.expect(4);
var collection = new Backbone.Collection([{val: 0}, {val: 4}]);
collection.on('add', function(model, coll, options) {
assert.equal(model.get('val'), options.index);
});
collection.add([{val: 1}, {val: 2}, {val: 3}], {at: 1});
collection.add({val: 5}, {at: 10});
}); });
QUnit.test('#3039: index is not sent when at is not specified', function(asser t) { QUnit.test('#3039: index is not sent when at is not specified', function(asser t) {
assert.expect(2); assert.expect(2);
var col = new Backbone.Collection([{at: 0}]); var collection = new Backbone.Collection([{at: 0}]);
col.on('add', function(model, col, options) { collection.on('add', function(model, coll, options) {
assert.equal(undefined, options.index); assert.equal(undefined, options.index);
}); });
col.add([{at: 1}, {at: 2}]); collection.add([{at: 1}, {at: 2}]);
}); });
QUnit.test('#3199 - Order changing should trigger a sort', function(assert) { QUnit.test('#3199 - Order changing should trigger a sort', function(assert) {
assert.expect(1); assert.expect(1);
var one = new Backbone.Model({id: 1}); var one = new Backbone.Model({id: 1});
var two = new Backbone.Model({id: 2}); var two = new Backbone.Model({id: 2});
var three = new Backbone.Model({id: 3}); var three = new Backbone.Model({id: 3});
var collection = new Backbone.Collection([one, two, three]); var collection = new Backbone.Collection([one, two, three]);
collection.on('sort', function() { collection.on('sort', function() {
assert.ok(true); assert.ok(true);
skipping to change at line 1772 skipping to change at line 1845
collection.add([{id: 1}, {id: 2}, {id: 3}]); collection.add([{id: 1}, {id: 2}, {id: 3}]);
}); });
QUnit.test('removing models triggers `update` event once', function(assert) { QUnit.test('removing models triggers `update` event once', function(assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}]); var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}]);
collection.on('update', function() { assert.ok(true); }); collection.on('update', function() { assert.ok(true); });
collection.remove([{id: 1}, {id: 2}]); collection.remove([{id: 1}, {id: 2}]);
}); });
QUnit.test('remove does not trigger `set` when nothing removed', function(asse rt) { QUnit.test('remove does not trigger `update` when nothing removed', function(a ssert) {
assert.expect(0); assert.expect(0);
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
collection.on('update', function() { assert.ok(false); }); collection.on('update', function() { assert.ok(false); });
collection.remove([{id: 3}]); collection.remove([{id: 3}]);
}); });
QUnit.test('set triggers `set` event once', function(assert) { QUnit.test('set triggers `set` event once', function(assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
collection.on('update', function() { assert.ok(true); }); collection.on('update', function() { assert.ok(true); });
collection.set([{id: 1}, {id: 3}]); collection.set([{id: 1}, {id: 3}]);
}); });
QUnit.test('set does not trigger `update` event when nothing added nor removed ', function(assert) { QUnit.test('set does not trigger `update` event when nothing added nor removed ', function(assert) {
assert.expect(0);
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
collection.on('update', function() { assert.ok(false); }); collection.on('update', function(coll, options) {
assert.equal(options.changes.added.length, 0);
assert.equal(options.changes.removed.length, 0);
assert.equal(options.changes.merged.length, 2);
});
collection.set([{id: 1}, {id: 2}]); collection.set([{id: 1}, {id: 2}]);
}); });
QUnit.test('#3610 - invoke collects arguments', function(assert) { QUnit.test('#3610 - invoke collects arguments', function(assert) {
assert.expect(3); assert.expect(3);
var Model = Backbone.Model.extend({ var Model = Backbone.Model.extend({
method: function(a, b, c) { method: function(x, y, z) {
assert.equal(a, 1); assert.equal(x, 1);
assert.equal(b, 2); assert.equal(y, 2);
assert.equal(c, 3); assert.equal(z, 3);
} }
}); });
var Collection = Backbone.Collection.extend({ var Collection = Backbone.Collection.extend({
model: Model model: Model
}); });
var collection = new Collection([{id: 1}]); var collection = new Collection([{id: 1}]);
collection.invoke('method', 1, 2, 3); collection.invoke('method', 1, 2, 3);
}); });
QUnit.test('#3662 - triggering change without model will not error', function( assert) { QUnit.test('#3662 - triggering change without model will not error', function( assert) {
assert.expect(1); assert.expect(1);
var collection = new Backbone.Collection([{id: 1}]); var collection = new Backbone.Collection([{id: 1}]);
var model = collection.first(); var model = collection.first();
collection.on('change', function(model) { collection.on('change', function(m) {
assert.equal(model, undefined); assert.equal(m, undefined);
}); });
model.trigger('change'); model.trigger('change');
}); });
QUnit.test('#3871 - falsy parse result creates empty collection', function(ass ert) { QUnit.test('#3871 - falsy parse result creates empty collection', function(ass ert) {
var collection = new (Backbone.Collection.extend({ var collection = new (Backbone.Collection.extend({
parse: function(data, options) {} parse: function(data, options) {}
})); }));
collection.set('', {parse: true}); collection.set('', {parse: true});
assert.equal(collection.length, 0); assert.equal(collection.length, 0);
}); });
})(); QUnit.test("#3711 - remove's `update` event returns one removed model", functi
on(assert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var collection = new Backbone.Collection([model]);
collection.on('update', function(context, options) {
var changed = options.changes;
assert.deepEqual(changed.added, []);
assert.deepEqual(changed.merged, []);
assert.strictEqual(changed.removed[0], model);
});
collection.remove(model);
});
QUnit.test("#3711 - remove's `update` event returns multiple removed models",
function(assert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
var collection = new Backbone.Collection([model, model2]);
collection.on('update', function(context, options) {
var changed = options.changes;
assert.deepEqual(changed.added, []);
assert.deepEqual(changed.merged, []);
assert.ok(changed.removed.length === 2);
assert.ok(_.indexOf(changed.removed, model) > -1 && _.indexOf(changed.remo
ved, model2) > -1);
});
collection.remove([model, model2]);
});
QUnit.test("#3711 - set's `update` event returns one added model", function(as
sert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var collection = new Backbone.Collection();
collection.on('update', function(context, options) {
var addedModels = options.changes.added;
assert.ok(addedModels.length === 1);
assert.strictEqual(addedModels[0], model);
});
collection.set(model);
});
QUnit.test("#3711 - set's `update` event returns multiple added models", funct
ion(assert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
var collection = new Backbone.Collection();
collection.on('update', function(context, options) {
var addedModels = options.changes.added;
assert.ok(addedModels.length === 2);
assert.strictEqual(addedModels[0], model);
assert.strictEqual(addedModels[1], model2);
});
collection.set([model, model2]);
});
QUnit.test("#3711 - set's `update` event returns one removed model", function(
assert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
var model3 = new Backbone.Model({id: 3, title: 'My Last Post'});
var collection = new Backbone.Collection([model]);
collection.on('update', function(context, options) {
var changed = options.changes;
assert.equal(changed.added.length, 2);
assert.equal(changed.merged.length, 0);
assert.ok(changed.removed.length === 1);
assert.strictEqual(changed.removed[0], model);
});
collection.set([model2, model3]);
});
QUnit.test("#3711 - set's `update` event returns multiple removed models", fun
ction(assert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
var model3 = new Backbone.Model({id: 3, title: 'My Last Post'});
var collection = new Backbone.Collection([model, model2]);
collection.on('update', function(context, options) {
var removedModels = options.changes.removed;
assert.ok(removedModels.length === 2);
assert.strictEqual(removedModels[0], model);
assert.strictEqual(removedModels[1], model2);
});
collection.set([model3]);
});
QUnit.test("#3711 - set's `update` event returns one merged model", function(a
ssert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
var model2Update = new Backbone.Model({id: 2, title: 'Second Post V2'});
var collection = new Backbone.Collection([model, model2]);
collection.on('update', function(context, options) {
var mergedModels = options.changes.merged;
assert.ok(mergedModels.length === 1);
assert.strictEqual(mergedModels[0].get('title'), model2Update.get('title')
);
});
collection.set([model2Update]);
});
QUnit.test("#3711 - set's `update` event returns multiple merged models", func
tion(assert) {
var model = new Backbone.Model({id: 1, title: 'First Post'});
var modelUpdate = new Backbone.Model({id: 1, title: 'First Post V2'});
var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
var model2Update = new Backbone.Model({id: 2, title: 'Second Post V2'});
var collection = new Backbone.Collection([model, model2]);
collection.on('update', function(context, options) {
var mergedModels = options.changes.merged;
assert.ok(mergedModels.length === 2);
assert.strictEqual(mergedModels[0].get('title'), model2Update.get('title')
);
assert.strictEqual(mergedModels[1].get('title'), modelUpdate.get('title'))
;
});
collection.set([model2Update, modelUpdate]);
});
QUnit.test("#3711 - set's `update` event should not be triggered adding a mode
l which already exists exactly alike", function(assert) {
var fired = false;
var model = new Backbone.Model({id: 1, title: 'First Post'});
var collection = new Backbone.Collection([model]);
collection.on('update', function(context, options) {
fired = true;
});
collection.set([model]);
assert.equal(fired, false);
});
})(QUnit);
 End of changes. 126 change blocks. 
315 lines changed or deleted 396 lines changed or added

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