collection.js (lodash-3.0.0) | : | collection.js (lodash-4.0.0) | ||
---|---|---|---|---|
(function() { | (function() { | |||
var a, b, c, d, e, col, otherCol; | var a, b, c, d, e, col, otherCol; | |||
module("Backbone.Collection", { | QUnit.module('Backbone.Collection', { | |||
setup: function() { | 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'}); | |||
e = null; | e = null; | |||
col = new Backbone.Collection([a,b,c,d]); | col = new Backbone.Collection([a, b, c, d]); | |||
otherCol = new Backbone.Collection(); | otherCol = new Backbone.Collection(); | |||
} | } | |||
}); | }); | |||
test("new and sort", 9, function() { | QUnit.test('new and sort', function(assert) { | |||
assert.expect(6); | ||||
var counter = 0; | var counter = 0; | |||
col.on('sort', function(){ counter++; }); | col.on('sort', function(){ counter++; }); | |||
equal(col.first(), a, "a should be first"); | assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']); | |||
equal(col.last(), d, "d should be last"); | ||||
col.comparator = function(a, b) { | col.comparator = function(a, b) { | |||
return a.id > b.id ? -1 : 1; | return a.id > b.id ? -1 : 1; | |||
}; | }; | |||
col.sort(); | col.sort(); | |||
equal(counter, 1); | assert.equal(counter, 1); | |||
equal(col.first(), a, "a should be first"); | assert.deepEqual(col.pluck('label'), ['a', 'b', 'c', 'd']); | |||
equal(col.last(), d, "d should be last"); | ||||
col.comparator = function(model) { return model.id; }; | col.comparator = function(model) { return model.id; }; | |||
col.sort(); | col.sort(); | |||
equal(counter, 2); | assert.equal(counter, 2); | |||
equal(col.first(), d, "d should be first"); | assert.deepEqual(col.pluck('label'), ['d', 'c', 'b', 'a']); | |||
equal(col.last(), a, "a should be last"); | assert.equal(col.length, 4); | |||
equal(col.length, 4); | ||||
}); | }); | |||
test("String comparator.", 1, function() { | QUnit.test('String comparator.', function(assert) { | |||
assert.expect(1); | ||||
var collection = new Backbone.Collection([ | var collection = new Backbone.Collection([ | |||
{id: 3}, | {id: 3}, | |||
{id: 1}, | {id: 1}, | |||
{id: 2} | {id: 2} | |||
], {comparator: 'id'}); | ], {comparator: 'id'}); | |||
deepEqual(collection.pluck('id'), [1, 2, 3]); | assert.deepEqual(collection.pluck('id'), [1, 2, 3]); | |||
}); | }); | |||
test("new and parse", 3, function() { | QUnit.test('new and parse', function(assert) { | |||
assert.expect(3); | ||||
var Collection = Backbone.Collection.extend({ | var Collection = Backbone.Collection.extend({ | |||
parse : function(data) { | parse: function(data) { | |||
return _.filter(data, function(datum) { | return _.filter(data, function(datum) { | |||
return datum.a % 2 === 0; | return datum.a % 2 === 0; | |||
}); | }); | |||
} | } | |||
}); | }); | |||
var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}]; | var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}]; | |||
var collection = new Collection(models, {parse: true}); | var collection = new Collection(models, {parse: true}); | |||
strictEqual(collection.length, 2); | assert.strictEqual(collection.length, 2); | |||
strictEqual(collection.first().get('a'), 2); | assert.strictEqual(collection.first().get('a'), 2); | |||
strictEqual(collection.last().get('a'), 4); | assert.strictEqual(collection.last().get('a'), 4); | |||
}); | }); | |||
test("get", 6, function() { | QUnit.test('clone preserves model and comparator', function(assert) { | |||
equal(col.get(0), d); | assert.expect(3); | |||
equal(col.get(d.clone()), d); | var Model = Backbone.Model.extend(); | |||
equal(col.get(2), b); | var comparator = function(model){ return model.id; }; | |||
equal(col.get({id: 1}), c); | ||||
equal(col.get(c.clone()), c); | var collection = new Backbone.Collection([{id: 1}], { | |||
equal(col.get(col.first().cid), col.first()); | model: Model, | |||
comparator: comparator | ||||
}).clone(); | ||||
collection.add({id: 2}); | ||||
assert.ok(collection.at(0) instanceof Model); | ||||
assert.ok(collection.at(1) instanceof Model); | ||||
assert.strictEqual(collection.comparator, comparator); | ||||
}); | ||||
QUnit.test('get', function(assert) { | ||||
assert.expect(6); | ||||
assert.equal(col.get(0), d); | ||||
assert.equal(col.get(d.clone()), d); | ||||
assert.equal(col.get(2), b); | ||||
assert.equal(col.get({id: 1}), c); | ||||
assert.equal(col.get(c.clone()), c); | ||||
assert.equal(col.get(col.first().cid), col.first()); | ||||
}); | }); | |||
test("get with non-default ids", 5, function() { | QUnit.test('get with non-default ids', function(assert) { | |||
var col = new Backbone.Collection(); | 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}); | |||
col.add(model); | var col = new Backbone.Collection([model], {model: MongoModel}); | |||
equal(col.get(100), model); | assert.equal(col.get(100), model); | |||
equal(col.get(model.cid), model); | assert.equal(col.get(model.cid), model); | |||
equal(col.get(model), model); | assert.equal(col.get(model), model); | |||
equal(col.get(101), void 0); | assert.equal(col.get(101), void 0); | |||
var col2 = new Backbone.Collection(); | var col2 = new Backbone.Collection(); | |||
col2.model = MongoModel; | col2.model = MongoModel; | |||
col2.add(model.attributes); | col2.add(model.attributes); | |||
equal(col2.get(model.clone()), col2.first()); | assert.equal(col2.get(model.clone()), col2.first()); | |||
}); | }); | |||
test('get with "undefined" id', function() { | QUnit.test('get with "undefined" id', function(assert) { | |||
var collection = new Backbone.Collection([{id: 1}, {id: 'undefined'}]); | var collection = new Backbone.Collection([{id: 1}, {id: 'undefined'}]); | |||
equal(collection.get(1).id, 1); | assert.equal(collection.get(1).id, 1); | |||
}), | }); | |||
test("update index when id changes", 4, function() { | QUnit.test('update index when id changes', function(assert) { | |||
assert.expect(4); | ||||
var col = new Backbone.Collection(); | var col = new Backbone.Collection(); | |||
col.add([ | col.add([ | |||
{id : 0, name : 'one'}, | {id: 0, name: 'one'}, | |||
{id : 1, name : 'two'} | {id: 1, name: 'two'} | |||
]); | ]); | |||
var one = col.get(0); | var one = col.get(0); | |||
equal(one.get('name'), 'one'); | assert.equal(one.get('name'), 'one'); | |||
col.on('change:name', function (model) { ok(this.get(model)); }); | col.on('change:name', function(model) { assert.ok(this.get(model)); }); | |||
one.set({name: 'dalmatians', id : 101}); | one.set({name: 'dalmatians', id: 101}); | |||
equal(col.get(0), null); | assert.equal(col.get(0), null); | |||
equal(col.get(101).get('name'), 'dalmatians'); | assert.equal(col.get(101).get('name'), 'dalmatians'); | |||
}); | }); | |||
test("at", 1, function() { | QUnit.test('at', function(assert) { | |||
equal(col.at(2), c); | assert.expect(2); | |||
assert.equal(col.at(2), c); | ||||
assert.equal(col.at(-2), c); | ||||
}); | }); | |||
test("pluck", 1, function() { | QUnit.test('pluck', function(assert) { | |||
equal(col.pluck('label').join(' '), 'a b c d'); | assert.expect(1); | |||
assert.equal(col.pluck('label').join(' '), 'a b c d'); | ||||
}); | }); | |||
test("add", 14, function() { | QUnit.test('add', function(assert) { | |||
assert.expect(14); | ||||
var added, opts, secondAdded; | var added, opts, secondAdded; | |||
added = opts = secondAdded = null; | added = opts = secondAdded = null; | |||
e = new Backbone.Model({id: 10, label : 'e'}); | e = new Backbone.Model({id: 10, label: 'e'}); | |||
otherCol.add(e); | otherCol.add(e); | |||
otherCol.on('add', function() { | otherCol.on('add', function() { | |||
secondAdded = true; | secondAdded = true; | |||
}); | }); | |||
col.on('add', function(model, collection, options){ | col.on('add', function(model, collection, options){ | |||
added = model.get('label'); | added = model.get('label'); | |||
opts = options; | opts = options; | |||
}); | }); | |||
col.add(e, {amazing: true}); | col.add(e, {amazing: true}); | |||
equal(added, 'e'); | assert.equal(added, 'e'); | |||
equal(col.length, 5); | assert.equal(col.length, 5); | |||
equal(col.last(), e); | assert.equal(col.last(), e); | |||
equal(otherCol.length, 1); | assert.equal(otherCol.length, 1); | |||
equal(secondAdded, null); | assert.equal(secondAdded, null); | |||
ok(opts.amazing); | assert.ok(opts.amazing); | |||
var f = new Backbone.Model({id: 20, label : 'f'}); | var f = new Backbone.Model({id: 20, label: 'f'}); | |||
var g = new Backbone.Model({id: 21, label : 'g'}); | var g = new Backbone.Model({id: 21, label: 'g'}); | |||
var h = new Backbone.Model({id: 22, label : 'h'}); | var h = new Backbone.Model({id: 22, label: 'h'}); | |||
var atCol = new Backbone.Collection([f, g, h]); | var atCol = new Backbone.Collection([f, g, h]); | |||
equal(atCol.length, 3); | assert.equal(atCol.length, 3); | |||
atCol.add(e, {at: 1}); | atCol.add(e, {at: 1}); | |||
equal(atCol.length, 4); | assert.equal(atCol.length, 4); | |||
equal(atCol.at(1), e); | assert.equal(atCol.at(1), e); | |||
equal(atCol.last(), h); | assert.equal(atCol.last(), h); | |||
var coll = new Backbone.Collection(new Array(2)); | var coll = new Backbone.Collection(new Array(2)); | |||
var addCount = 0; | var addCount = 0; | |||
coll.on('add', function(){ | coll.on('add', function(){ | |||
addCount += 1; | addCount += 1; | |||
}); | }); | |||
coll.add([undefined, f, g]); | coll.add([undefined, f, g]); | |||
equal(coll.length, 5); | assert.equal(coll.length, 5); | |||
equal(addCount, 3); | assert.equal(addCount, 3); | |||
coll.add(new Array(4)); | coll.add(new Array(4)); | |||
equal(coll.length, 9); | assert.equal(coll.length, 9); | |||
equal(addCount, 7); | assert.equal(addCount, 7); | |||
}); | }); | |||
test("add multiple models", 6, function() { | QUnit.test('add multiple models', function(assert) { | |||
assert.expect(6); | ||||
var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]); | var col = 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 : 2}); | col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at : 2}); | |||
for (var i = 0; i <= 5; i++) { | for (var i = 0; i <= 5; i++) { | |||
equal(col.at(i).get('at'), i); | assert.equal(col.at(i).get('at'), i); | |||
} | } | |||
}); | }); | |||
test("add; at should have preference over comparator", 1, function() { | QUnit.test('add; at should have preference over comparator', function(assert) | |||
{ | ||||
assert.expect(1); | ||||
var Col = Backbone.Collection.extend({ | var Col = Backbone.Collection.extend({ | |||
comparator: function(a,b) { | comparator: function(a, b) { | |||
return a.id > b.id ? -1 : 1; | return a.id > b.id ? -1 : 1; | |||
} | } | |||
}); | }); | |||
var col = new Col([{id: 2}, {id: 3}]); | var col = new Col([{id: 2}, {id: 3}]); | |||
col.add(new Backbone.Model({id: 1}), {at: 1}); | col.add(new Backbone.Model({id: 1}), {at: 1}); | |||
equal(col.pluck('id').join(' '), '3 1 2'); | assert.equal(col.pluck('id').join(' '), '3 1 2'); | |||
}); | }); | |||
test("can't add model to collection twice", function() { | QUnit.test('add; at should add to the end if the index is out of bounds', func | |||
tion(assert) { | ||||
assert.expect(1); | ||||
var col = new Backbone.Collection([{id: 2}, {id: 3}]); | ||||
col.add(new Backbone.Model({id: 1}), {at: 5}); | ||||
assert.equal(col.pluck('id').join(' '), '2 3 1'); | ||||
}); | ||||
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: 3}]); | var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]); | |||
equal(col.pluck('id').join(' '), '1 2 3'); | assert.equal(col.pluck('id').join(' '), '1 2 3'); | |||
}); | }); | |||
test("can't add different model with same id to collection twice", 1, function | QUnit.test("can't add different model with same id to collection twice", funct | |||
() { | ion(assert) { | |||
assert.expect(1); | ||||
var col = new Backbone.Collection; | var col = new Backbone.Collection; | |||
col.unshift({id: 101}); | col.unshift({id: 101}); | |||
col.add({id: 101}); | col.add({id: 101}); | |||
equal(col.length, 1); | assert.equal(col.length, 1); | |||
}); | }); | |||
test("merge in duplicate models with {merge: true}", 3, function() { | QUnit.test('merge in duplicate models with {merge: true}', function(assert) { | |||
assert.expect(3); | ||||
var col = new Backbone.Collection; | var col = new Backbone.Collection; | |||
col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry' }]); | col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry' }]); | |||
col.add({id: 1, name: 'Moses'}); | col.add({id: 1, name: 'Moses'}); | |||
equal(col.first().get('name'), 'Moe'); | assert.equal(col.first().get('name'), 'Moe'); | |||
col.add({id: 1, name: 'Moses'}, {merge: true}); | col.add({id: 1, name: 'Moses'}, {merge: true}); | |||
equal(col.first().get('name'), 'Moses'); | assert.equal(col.first().get('name'), 'Moses'); | |||
col.add({id: 1, name: 'Tim'}, {merge: true, silent: true}); | col.add({id: 1, name: 'Tim'}, {merge: true, silent: true}); | |||
equal(col.first().get('name'), 'Tim'); | assert.equal(col.first().get('name'), 'Tim'); | |||
}); | }); | |||
test("add model to multiple collections", 10, function() { | QUnit.test('add model to multiple collections', function(assert) { | |||
assert.expect(10); | ||||
var counter = 0; | var counter = 0; | |||
var e = new Backbone.Model({id: 10, label : 'e'}); | var e = new Backbone.Model({id: 10, label: 'e'}); | |||
e.on('add', function(model, collection) { | e.on('add', function(model, collection) { | |||
counter++; | counter++; | |||
equal(e, model); | assert.equal(e, model); | |||
if (counter > 1) { | if (counter > 1) { | |||
equal(collection, colF); | assert.equal(collection, colF); | |||
} else { | } else { | |||
equal(collection, colE); | assert.equal(collection, colE); | |||
} | } | |||
}); | }); | |||
var colE = new Backbone.Collection([]); | var colE = new Backbone.Collection([]); | |||
colE.on('add', function(model, collection) { | colE.on('add', function(model, collection) { | |||
equal(e, model); | assert.equal(e, model); | |||
equal(colE, collection); | assert.equal(colE, collection); | |||
}); | }); | |||
var colF = new Backbone.Collection([]); | var colF = new Backbone.Collection([]); | |||
colF.on('add', function(model, collection) { | colF.on('add', function(model, collection) { | |||
equal(e, model); | assert.equal(e, model); | |||
equal(colF, collection); | assert.equal(colF, collection); | |||
}); | }); | |||
colE.add(e); | colE.add(e); | |||
equal(e.collection, colE); | assert.equal(e.collection, colE); | |||
colF.add(e); | colF.add(e); | |||
equal(e.collection, colE); | assert.equal(e.collection, colE); | |||
}); | }); | |||
test("add model with parse", 1, function() { | QUnit.test('add model with parse', function(assert) { | |||
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 col = new Col; | |||
col.add({value: 1}, {parse: true}); | col.add({value: 1}, {parse: true}); | |||
equal(col.at(0).get('value'), 2); | assert.equal(col.at(0).get('value'), 2); | |||
}); | }); | |||
test("add with parse and merge", function() { | 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}); | |||
equal(collection.first().get('name'), 'Alf'); | assert.equal(collection.first().get('name'), 'Alf'); | |||
}); | }); | |||
test("add model to collection with sort()-style comparator", 3, function() { | QUnit.test('add model to collection with sort()-style comparator', function(as | |||
sert) { | ||||
assert.expect(3); | ||||
var col = new Backbone.Collection; | var col = new Backbone.Collection; | |||
col.comparator = function(a, b) { | col.comparator = function(a, b) { | |||
return a.get('name') < b.get('name') ? -1 : 1; | return a.get('name') < b.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); | col.add(tom); | |||
col.add(rob); | col.add(rob); | |||
col.add(tim); | col.add(tim); | |||
equal(col.indexOf(rob), 0); | assert.equal(col.indexOf(rob), 0); | |||
equal(col.indexOf(tim), 1); | assert.equal(col.indexOf(tim), 1); | |||
equal(col.indexOf(tom), 2); | assert.equal(col.indexOf(tom), 2); | |||
}); | }); | |||
test("comparator that depends on `this`", 2, function() { | QUnit.test('comparator that depends on `this`', function(assert) { | |||
assert.expect(2); | ||||
var col = new Backbone.Collection; | var col = new Backbone.Collection; | |||
col.negative = function(num) { | col.negative = function(num) { | |||
return -num; | return -num; | |||
}; | }; | |||
col.comparator = function(a) { | col.comparator = function(a) { | |||
return this.negative(a.id); | return this.negative(a.id); | |||
}; | }; | |||
col.add([{id: 1}, {id: 2}, {id: 3}]); | col.add([{id: 1}, {id: 2}, {id: 3}]); | |||
deepEqual(col.pluck('id'), [3, 2, 1]); | assert.deepEqual(col.pluck('id'), [3, 2, 1]); | |||
col.comparator = function(a, b) { | col.comparator = function(a, b) { | |||
return this.negative(b.id) - this.negative(a.id); | return this.negative(b.id) - this.negative(a.id); | |||
}; | }; | |||
col.sort(); | col.sort(); | |||
deepEqual(col.pluck('id'), [1, 2, 3]); | assert.deepEqual(col.pluck('id'), [1, 2, 3]); | |||
}); | }); | |||
test("remove", 5, function() { | QUnit.test('remove', function(assert) { | |||
assert.expect(12); | ||||
var removed = null; | var removed = null; | |||
var otherRemoved = null; | var result = null; | |||
col.on('remove', function(model, col, options) { | col.on('remove', function(model, col, options) { | |||
removed = model.get('label'); | removed = model.get('label'); | |||
equal(options.index, 3); | assert.equal(options.index, 3); | |||
assert.equal(col.get(model), undefined, '#3693: model cannot be fetched fr | ||||
om collection'); | ||||
}); | }); | |||
otherCol.on('remove', function(model, col, options) { | result = col.remove(d); | |||
otherRemoved = true; | assert.equal(removed, 'd'); | |||
}); | assert.strictEqual(result, d); | |||
col.remove(d); | //if we try to remove d again, it's not going to actually get removed | |||
equal(removed, 'd'); | result = col.remove(d); | |||
equal(col.length, 3); | assert.strictEqual(result, undefined); | |||
equal(col.first(), a); | assert.equal(col.length, 3); | |||
equal(otherRemoved, null); | assert.equal(col.first(), a); | |||
col.off(); | ||||
result = col.remove([c, d]); | ||||
assert.equal(result.length, 1, 'only returns removed models'); | ||||
assert.equal(result[0], c, 'only returns removed models'); | ||||
result = col.remove([c, b]); | ||||
assert.equal(result.length, 1, 'only returns removed models'); | ||||
assert.equal(result[0], b, 'only returns removed models'); | ||||
result = col.remove([]); | ||||
assert.deepEqual(result, [], 'returns empty array when nothing removed'); | ||||
}); | }); | |||
test("add and remove return values", 13, function() { | QUnit.test('add and remove return values', function(assert) { | |||
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 col = new Backbone.Collection; | |||
col.model = Even; | col.model = Even; | |||
var list = col.add([{id: 2}, {id: 4}], {validate: true}); | var list = col.add([{id: 2}, {id: 4}], {validate: true}); | |||
equal(list.length, 2); | assert.equal(list.length, 2); | |||
ok(list[0] instanceof Backbone.Model); | assert.ok(list[0] instanceof Backbone.Model); | |||
equal(list[1], col.last()); | assert.equal(list[1], col.last()); | |||
equal(list[1].get('id'), 4); | assert.equal(list[1].get('id'), 4); | |||
list = col.add([{id: 3}, {id: 6}], {validate: true}); | list = col.add([{id: 3}, {id: 6}], {validate: true}); | |||
equal(col.length, 3); | assert.equal(col.length, 3); | |||
equal(list[0], false); | assert.equal(list[0], false); | |||
equal(list[1].get('id'), 6); | assert.equal(list[1].get('id'), 6); | |||
var result = col.add({id: 6}); | var result = col.add({id: 6}); | |||
equal(result.cid, list[1].cid); | assert.equal(result.cid, list[1].cid); | |||
result = col.remove({id: 6}); | result = col.remove({id: 6}); | |||
equal(col.length, 2); | assert.equal(col.length, 2); | |||
equal(result.id, 6); | assert.equal(result.id, 6); | |||
list = col.remove([{id: 2}, {id: 8}]); | list = col.remove([{id: 2}, {id: 8}]); | |||
equal(col.length, 1); | assert.equal(col.length, 1); | |||
equal(list[0].get('id'), 2); | assert.equal(list[0].get('id'), 2); | |||
equal(list[1], null); | assert.equal(list[1], null); | |||
}); | }); | |||
test("shift and pop", 2, function() { | QUnit.test('shift and pop', function(assert) { | |||
assert.expect(2); | ||||
var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); | var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); | |||
equal(col.shift().get('a'), 'a'); | assert.equal(col.shift().get('a'), 'a'); | |||
equal(col.pop().get('c'), 'c'); | assert.equal(col.pop().get('c'), 'c'); | |||
}); | }); | |||
test("slice", 2, function() { | QUnit.test('slice', function(assert) { | |||
assert.expect(2); | ||||
var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); | var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]); | |||
var array = col.slice(1, 3); | var array = col.slice(1, 3); | |||
equal(array.length, 2); | assert.equal(array.length, 2); | |||
equal(array[0].get('b'), 'b'); | assert.equal(array[0].get('b'), 'b'); | |||
}); | }); | |||
test("events are unbound on remove", 3, function() { | QUnit.test('events are unbound on remove', function(assert) { | |||
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++; }); | |||
dj.set({name : 'Kool'}); | dj.set({name: 'Kool'}); | |||
equal(counter, 1); | assert.equal(counter, 1); | |||
emcees.reset([]); | emcees.reset([]); | |||
equal(dj.collection, undefined); | assert.equal(dj.collection, undefined); | |||
dj.set({name : 'Shadow'}); | dj.set({name: 'Shadow'}); | |||
equal(counter, 1); | assert.equal(counter, 1); | |||
}); | }); | |||
test("remove in multiple collections", 7, function() { | QUnit.test('remove in multiple collections', function(assert) { | |||
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 e = new Backbone.Model(modelData); | |||
var f = new Backbone.Model(modelData); | var f = new Backbone.Model(modelData); | |||
f.on('remove', function() { | f.on('remove', function() { | |||
passed = true; | passed = true; | |||
}); | }); | |||
var colE = new Backbone.Collection([e]); | var colE = new Backbone.Collection([e]); | |||
var colF = new Backbone.Collection([f]); | var colF = new Backbone.Collection([f]); | |||
ok(e != f); | assert.ok(e != f); | |||
ok(colE.length === 1); | assert.ok(colE.length === 1); | |||
ok(colF.length === 1); | assert.ok(colF.length === 1); | |||
colE.remove(e); | colE.remove(e); | |||
equal(passed, false); | assert.equal(passed, false); | |||
ok(colE.length === 0); | assert.ok(colE.length === 0); | |||
colF.remove(e); | colF.remove(e); | |||
ok(colF.length === 0); | assert.ok(colF.length === 0); | |||
equal(passed, true); | assert.equal(passed, true); | |||
}); | }); | |||
test("remove same model in multiple collection", 16, function() { | QUnit.test('remove same model in multiple collection', function(assert) { | |||
assert.expect(16); | ||||
var counter = 0; | var counter = 0; | |||
var e = new Backbone.Model({id: 5, title: 'Othello'}); | var e = new Backbone.Model({id: 5, title: 'Othello'}); | |||
e.on('remove', function(model, collection) { | e.on('remove', function(model, collection) { | |||
counter++; | counter++; | |||
equal(e, model); | assert.equal(e, model); | |||
if (counter > 1) { | if (counter > 1) { | |||
equal(collection, colE); | assert.equal(collection, colE); | |||
} else { | } else { | |||
equal(collection, colF); | assert.equal(collection, colF); | |||
} | } | |||
}); | }); | |||
var colE = new Backbone.Collection([e]); | var colE = new Backbone.Collection([e]); | |||
colE.on('remove', function(model, collection) { | colE.on('remove', function(model, collection) { | |||
equal(e, model); | assert.equal(e, model); | |||
equal(colE, collection); | assert.equal(colE, collection); | |||
}); | }); | |||
var colF = new Backbone.Collection([e]); | var colF = new Backbone.Collection([e]); | |||
colF.on('remove', function(model, collection) { | colF.on('remove', function(model, collection) { | |||
equal(e, model); | assert.equal(e, model); | |||
equal(colF, collection); | assert.equal(colF, collection); | |||
}); | }); | |||
equal(colE, e.collection); | assert.equal(colE, e.collection); | |||
colF.remove(e); | colF.remove(e); | |||
ok(colF.length === 0); | assert.ok(colF.length === 0); | |||
ok(colE.length === 1); | assert.ok(colE.length === 1); | |||
equal(counter, 1); | assert.equal(counter, 1); | |||
equal(colE, e.collection); | assert.equal(colE, e.collection); | |||
colE.remove(e); | colE.remove(e); | |||
equal(null, e.collection); | assert.equal(null, e.collection); | |||
ok(colE.length === 0); | assert.ok(colE.length === 0); | |||
equal(counter, 2); | assert.equal(counter, 2); | |||
}); | }); | |||
test("model destroy removes from all collections", 3, function() { | QUnit.test('model destroy removes from all collections', function(assert) { | |||
assert.expect(3); | ||||
var e = new Backbone.Model({id: 5, title: 'Othello'}); | var e = new Backbone.Model({id: 5, title: 'Othello'}); | |||
e.sync = function(method, model, options) { options.success(); }; | e.sync = function(method, model, options) { options.success(); }; | |||
var colE = new Backbone.Collection([e]); | var colE = new Backbone.Collection([e]); | |||
var colF = new Backbone.Collection([e]); | var colF = new Backbone.Collection([e]); | |||
e.destroy(); | e.destroy(); | |||
ok(colE.length === 0); | assert.ok(colE.length === 0); | |||
ok(colF.length === 0); | assert.ok(colF.length === 0); | |||
equal(undefined, e.collection); | assert.equal(undefined, e.collection); | |||
}); | }); | |||
test("Colllection: non-persisted model destroy removes from all collections", | QUnit.test('Collection: non-persisted model destroy removes from all collectio | |||
3, function() { | ns', function(assert) { | |||
assert.expect(3); | ||||
var e = new Backbone.Model({title: 'Othello'}); | var e = new Backbone.Model({title: 'Othello'}); | |||
e.sync = function(method, model, options) { throw "should not be called"; }; | e.sync = function(method, model, options) { throw 'should not be called'; }; | |||
var colE = new Backbone.Collection([e]); | var colE = new Backbone.Collection([e]); | |||
var colF = new Backbone.Collection([e]); | var colF = new Backbone.Collection([e]); | |||
e.destroy(); | e.destroy(); | |||
ok(colE.length === 0); | assert.ok(colE.length === 0); | |||
ok(colF.length === 0); | assert.ok(colF.length === 0); | |||
equal(undefined, e.collection); | assert.equal(undefined, e.collection); | |||
}); | }); | |||
test("fetch", 4, function() { | QUnit.test('fetch', function(assert) { | |||
assert.expect(4); | ||||
var collection = new Backbone.Collection; | var collection = new Backbone.Collection; | |||
collection.url = '/test'; | collection.url = '/test'; | |||
collection.fetch(); | collection.fetch(); | |||
equal(this.syncArgs.method, 'read'); | assert.equal(this.syncArgs.method, 'read'); | |||
equal(this.syncArgs.model, collection); | assert.equal(this.syncArgs.model, collection); | |||
equal(this.syncArgs.options.parse, true); | assert.equal(this.syncArgs.options.parse, true); | |||
collection.fetch({parse: false}); | collection.fetch({parse: false}); | |||
equal(this.syncArgs.options.parse, false); | assert.equal(this.syncArgs.options.parse, false); | |||
}); | }); | |||
test("fetch with an error response triggers an error event", 1, function () { | QUnit.test('fetch with an error response triggers an error event', function(as | |||
sert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection(); | var collection = new Backbone.Collection(); | |||
collection.on('error', function () { | collection.on('error', function() { | |||
ok(true); | assert.ok(true); | |||
}); | }); | |||
collection.sync = function (method, model, options) { options.error(); }; | collection.sync = function(method, model, options) { options.error(); }; | |||
collection.fetch(); | collection.fetch(); | |||
}); | }); | |||
test("ensure fetch only parses once", 1, function() { | QUnit.test('#3283 - fetch with an error response calls error with context', fu | |||
nction(assert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection(); | ||||
var obj = {}; | ||||
var options = { | ||||
context: obj, | ||||
error: function() { | ||||
assert.equal(this, obj); | ||||
} | ||||
}; | ||||
collection.sync = function(method, model, options) { | ||||
options.error.call(options.context); | ||||
}; | ||||
collection.fetch(options); | ||||
}); | ||||
QUnit.test('ensure fetch only parses once', function(assert) { | ||||
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++; | |||
return models; | return models; | |||
}; | }; | |||
collection.url = '/test'; | collection.url = '/test'; | |||
collection.fetch(); | collection.fetch(); | |||
this.syncArgs.options.success(); | this.syncArgs.options.success([]); | |||
equal(counter, 1); | assert.equal(counter, 1); | |||
}); | }); | |||
test("create", 4, function() { | QUnit.test('create', function(assert) { | |||
assert.expect(4); | ||||
var collection = new Backbone.Collection; | var collection = new Backbone.Collection; | |||
collection.url = '/test'; | collection.url = '/test'; | |||
var model = collection.create({label: 'f'}, {wait: true}); | var model = collection.create({label: 'f'}, {wait: true}); | |||
equal(this.syncArgs.method, 'create'); | assert.equal(this.syncArgs.method, 'create'); | |||
equal(this.syncArgs.model, model); | assert.equal(this.syncArgs.model, model); | |||
equal(model.get('label'), 'f'); | assert.equal(model.get('label'), 'f'); | |||
equal(model.collection, collection); | assert.equal(model.collection, collection); | |||
}); | }); | |||
test("create with validate:true enforces validation", 3, function() { | QUnit.test('create with validate:true enforces validation', function(assert) { | |||
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 col = new ValidatingCollection(); | |||
col.on('invalid', function (collection, error, options) { | col.on('invalid', function(collection, error, options) { | |||
equal(error, "fail"); | assert.equal(error, 'fail'); | |||
equal(options.validationError, 'fail'); | assert.equal(options.validationError, 'fail'); | |||
}); | }); | |||
equal(col.create({"foo":"bar"}, {validate:true}), false); | assert.equal(col.create({'foo': 'bar'}, {validate: true}), false); | |||
}); | }); | |||
test("a failing create returns model with errors", function() { | QUnit.test('create will pass extra options to success callback', function(asse | |||
rt) { | ||||
assert.expect(1); | ||||
var Model = Backbone.Model.extend({ | ||||
sync: function(method, model, options) { | ||||
_.extend(options, {specialSync: true}); | ||||
return Backbone.Model.prototype.sync.call(this, method, model, options); | ||||
} | ||||
}); | ||||
var Collection = Backbone.Collection.extend({ | ||||
model: Model, | ||||
url: '/test' | ||||
}); | ||||
var collection = new Collection; | ||||
var success = function(model, response, options) { | ||||
assert.ok(options.specialSync, 'Options were passed correctly to callback' | ||||
); | ||||
}; | ||||
collection.create({}, {success: success}); | ||||
this.ajaxSettings.success(); | ||||
}); | ||||
QUnit.test('create with wait:true should not call collection.parse', function( | ||||
assert) { | ||||
assert.expect(0); | ||||
var Collection = Backbone.Collection.extend({ | ||||
url: '/test', | ||||
parse: function() { | ||||
assert.ok(false); | ||||
} | ||||
}); | ||||
var collection = new Collection; | ||||
collection.create({}, {wait: true}); | ||||
this.ajaxSettings.success(); | ||||
}); | ||||
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 col = new ValidatingCollection(); | |||
var m = col.create({"foo":"bar"}); | var m = col.create({foo: 'bar'}); | |||
equal(m.validationError, 'fail'); | assert.equal(m.validationError, 'fail'); | |||
equal(col.length, 1); | assert.equal(col.length, 1); | |||
}); | }); | |||
test("initialize", 1, function() { | QUnit.test('initialize', function(assert) { | |||
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; | |||
equal(coll.one, 1); | assert.equal(coll.one, 1); | |||
}); | }); | |||
test("toJSON", 1, function() { | QUnit.test('toJSON', function(assert) { | |||
equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id" | assert.expect(1); | |||
: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"}]'); | ||||
}); | }); | |||
test("where and findWhere", 8, function() { | QUnit.test('where and findWhere', function(assert) { | |||
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, | |||
{a: 1}, | {a: 1}, | |||
{a: 1, b: 2}, | {a: 1, b: 2}, | |||
{a: 2, b: 2}, | {a: 2, b: 2}, | |||
{a: 3} | {a: 3} | |||
]); | ]); | |||
equal(coll.where({a: 1}).length, 3); | assert.equal(coll.where({a: 1}).length, 3); | |||
equal(coll.where({a: 2}).length, 1); | assert.equal(coll.where({a: 2}).length, 1); | |||
equal(coll.where({a: 3}).length, 1); | assert.equal(coll.where({a: 3}).length, 1); | |||
equal(coll.where({b: 1}).length, 0); | assert.equal(coll.where({b: 1}).length, 0); | |||
equal(coll.where({b: 2}).length, 2); | assert.equal(coll.where({b: 2}).length, 2); | |||
equal(coll.where({a: 1, b: 2}).length, 1); | assert.equal(coll.where({a: 1, b: 2}).length, 1); | |||
equal(coll.findWhere({a: 1}), model); | assert.equal(coll.findWhere({a: 1}), model); | |||
equal(coll.findWhere({a: 4}), void 0); | assert.equal(coll.findWhere({a: 4}), void 0); | |||
}); | }); | |||
test("Underscore methods", 16, function() { | QUnit.test('Underscore methods', function(assert) { | |||
equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b | assert.expect(21); | |||
c d'); | assert.equal(col.map(function(model){ return model.get('label'); }).join(' ' | |||
equal(col.any(function(model){ return model.id === 100; }), false); | ), 'a b c d'); | |||
equal(col.any(function(model){ return model.id === 0; }), true); | assert.equal(col.some(function(model){ return model.id === 100; }), false); | |||
equal(col.indexOf(b), 1); | assert.equal(col.some(function(model){ return model.id === 0; }), true); | |||
equal(col.size(), 4); | assert.equal(col.reduce(function(a, b) {return a.id > b.id ? a : b;}).id, 3) | |||
equal(col.rest().length, 3); | ; | |||
ok(!_.include(col.rest(), a)); | assert.equal(col.reduceRight(function(a, b) {return a.id > b.id ? a : b;}).i | |||
ok(_.include(col.rest(), d)); | d, 3); | |||
ok(!col.isEmpty()); | assert.equal(col.indexOf(b), 1); | |||
ok(!_.include(col.without(d), d)); | assert.equal(col.size(), 4); | |||
equal(col.max(function(model){ return model.id; }).id, 3); | assert.equal(col.rest().length, 3); | |||
equal(col.min(function(model){ return model.id; }).id, 0); | assert.ok(!_.includes(col.rest(), a)); | |||
deepEqual(col.chain() | assert.ok(_.includes(col.rest(), d)); | |||
.filter(function(o){ return o.id % 2 === 0; }) | assert.ok(!col.isEmpty()); | |||
.map(function(o){ return o.id * 2; }) | assert.ok(!_.includes(col.without(d), d)); | |||
.value(), | ||||
[4, 0]); | var wrapped = col.chain(); | |||
deepEqual(col.difference([c, d]), [a, b]); | assert.equal(wrapped.map('id').max().value(), 3); | |||
ok(col.include(col.sample())); | assert.equal(wrapped.map('id').min().value(), 0); | |||
assert.deepEqual(wrapped | ||||
.filter(function(o){ return o.id % 2 === 0; }) | ||||
.map(function(o){ return o.id * 2; }) | ||||
.value(), | ||||
[4, 0]); | ||||
assert.deepEqual(col.difference([c, d]), [a, b]); | ||||
assert.ok(col.includes(col.sample())); | ||||
var first = col.first(); | var first = col.first(); | |||
ok(col.indexBy('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.sortBy(function(model){ return model.id; })[0], col.at( | ||||
3)); | ||||
assert.ok(col.indexBy('id')[first.id] === first); | ||||
}); | }); | |||
test("reset", 16, function() { | QUnit.test('Underscore methods with object-style and property-style iteratee', | |||
function(assert) { | ||||
assert.expect(26); | ||||
var model = new Backbone.Model({a: 4, b: 1, e: 3}); | ||||
var coll = new Backbone.Collection([ | ||||
{a: 1, b: 1}, | ||||
{a: 2, b: 1, c: 1}, | ||||
{a: 3, b: 1}, | ||||
model | ||||
]); | ||||
assert.equal(coll.find({a: 0}), undefined); | ||||
assert.deepEqual(coll.find({a: 4}), model); | ||||
assert.equal(coll.find('d'), undefined); | ||||
assert.deepEqual(coll.find('e'), model); | ||||
assert.equal(coll.filter({a: 0}), false); | ||||
assert.deepEqual(coll.filter({a: 4}), [model]); | ||||
assert.equal(coll.some({a: 0}), false); | ||||
assert.equal(coll.some({a: 1}), true); | ||||
assert.equal(coll.reject({a: 0}).length, 4); | ||||
assert.deepEqual(coll.reject({a: 4}), _.without(coll.models, model)); | ||||
assert.equal(coll.every({a: 0}), false); | ||||
assert.equal(coll.every({b: 1}), true); | ||||
assert.deepEqual(coll.partition({a: 0})[0], []); | ||||
assert.deepEqual(coll.partition({a: 0})[1], coll.models); | ||||
assert.deepEqual(coll.partition({a: 4})[0], [model]); | ||||
assert.deepEqual(coll.partition({a: 4})[1], _.without(coll.models, model)); | ||||
assert.deepEqual(coll.map({a: 2}), [false, true, false, false]); | ||||
assert.deepEqual(coll.map('a'), [1, 2, 3, 4]); | ||||
assert.deepEqual(coll.sortBy('a')[3], model); | ||||
assert.deepEqual(coll.sortBy('e')[0], model); | ||||
assert.deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1}); | ||||
assert.deepEqual(coll.countBy('d'), {'undefined': 4}); | ||||
assert.equal(coll.findIndex({b: 1}), 0); | ||||
assert.equal(coll.findIndex({b: 9}), -1); | ||||
assert.equal(coll.findLastIndex({b: 1}), 3); | ||||
assert.equal(coll.findLastIndex({b: 9}), -1); | ||||
}); | ||||
QUnit.test('reset', function(assert) { | ||||
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([]); | |||
equal(resetCount, 1); | assert.equal(resetCount, 1); | |||
equal(col.length, 0); | assert.equal(col.length, 0); | |||
equal(col.last(), null); | assert.equal(col.last(), null); | |||
col.reset(models); | col.reset(models); | |||
equal(resetCount, 2); | assert.equal(resetCount, 2); | |||
equal(col.length, 4); | assert.equal(col.length, 4); | |||
equal(col.last(), d); | assert.equal(col.last(), d); | |||
col.reset(_.map(models, function(m){ return m.attributes; })); | col.reset(_.map(models, function(m){ return m.attributes; })); | |||
equal(resetCount, 3); | assert.equal(resetCount, 3); | |||
equal(col.length, 4); | assert.equal(col.length, 4); | |||
ok(col.last() !== d); | assert.ok(col.last() !== d); | |||
ok(_.isEqual(col.last().attributes, d.attributes)); | assert.ok(_.isEqual(col.last().attributes, d.attributes)); | |||
col.reset(); | col.reset(); | |||
equal(col.length, 0); | assert.equal(col.length, 0); | |||
equal(resetCount, 4); | assert.equal(resetCount, 4); | |||
var f = new Backbone.Model({id: 20, label : 'f'}); | var f = new Backbone.Model({id: 20, label: 'f'}); | |||
col.reset([undefined, f]); | col.reset([undefined, f]); | |||
equal(col.length, 2); | assert.equal(col.length, 2); | |||
equal(resetCount, 5); | assert.equal(resetCount, 5); | |||
col.reset(new Array(4)); | col.reset(new Array(4)); | |||
equal(col.length, 4); | assert.equal(col.length, 4); | |||
equal(resetCount, 6); | assert.equal(resetCount, 6); | |||
}); | }); | |||
test ("reset with different values", function(){ | QUnit.test('reset with different values', function(assert) { | |||
var col = new Backbone.Collection({id: 1}); | var col = new Backbone.Collection({id: 1}); | |||
col.reset({id: 1, a: 1}); | col.reset({id: 1, a: 1}); | |||
equal(col.get(1).get('a'), 1); | assert.equal(col.get(1).get('a'), 1); | |||
}); | }); | |||
test("same references in reset", function() { | 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); | |||
equal(collection.get(1), model); | assert.equal(collection.get(1), model); | |||
}); | }); | |||
test("reset passes caller options", 3, function() { | QUnit.test('reset passes caller options', function(assert) { | |||
assert.expect(3); | ||||
var Model = Backbone.Model.extend({ | var Model = Backbone.Model.extend({ | |||
initialize: function(attrs, options) { | initialize: function(attrs, options) { | |||
this.model_parameter = options.model_parameter; | this.modelParameter = options.modelParameter; | |||
} | } | |||
}); | }); | |||
var col = new (Backbone.Collection.extend({ model: Model }))(); | var col = new (Backbone.Collection.extend({model: Model}))(); | |||
col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 } | col.reset([{astring: 'green', anumber: 1}, {astring: 'blue', anumber: 2}], { | |||
], { model_parameter: 'model parameter' }); | modelParameter: 'model parameter'}); | |||
equal(col.length, 2); | assert.equal(col.length, 2); | |||
col.each(function(model) { | col.each(function(model) { | |||
equal(model.model_parameter, 'model parameter'); | assert.equal(model.modelParameter, 'model parameter'); | |||
}); | }); | |||
}); | }); | |||
test("trigger custom events on models", 1, function() { | QUnit.test('reset does not alter options by reference', function(assert) { | |||
assert.expect(2); | ||||
var col = new Backbone.Collection([{id: 1}]); | ||||
var origOpts = {}; | ||||
col.on('reset', function(col, opts){ | ||||
assert.equal(origOpts.previousModels, undefined); | ||||
assert.equal(opts.previousModels[0].id, 1); | ||||
}); | ||||
col.reset([], origOpts); | ||||
}); | ||||
QUnit.test('trigger custom events on models', function(assert) { | ||||
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'); | |||
equal(fired, true); | assert.equal(fired, true); | |||
}); | }); | |||
test("add does not alter arguments", 2, function(){ | QUnit.test('add does not alter arguments', function(assert) { | |||
assert.expect(2); | ||||
var attrs = {}; | var attrs = {}; | |||
var models = [attrs]; | var models = [attrs]; | |||
new Backbone.Collection().add(models); | new Backbone.Collection().add(models); | |||
equal(models.length, 1); | assert.equal(models.length, 1); | |||
ok(attrs === models[0]); | assert.ok(attrs === models[0]); | |||
}); | }); | |||
test("#714: access `model.collection` in a brand new model.", 2, function() { | QUnit.test('#714: access `model.collection` in a brand new model.', function(a | |||
ssert) { | ||||
assert.expect(2); | ||||
var collection = new Backbone.Collection; | var collection = new Backbone.Collection; | |||
collection.url = '/test'; | collection.url = '/test'; | |||
var Model = Backbone.Model.extend({ | var Model = Backbone.Model.extend({ | |||
set: function(attrs) { | set: function(attrs) { | |||
equal(attrs.prop, 'value'); | assert.equal(attrs.prop, 'value'); | |||
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'}); | |||
}); | }); | |||
test("#574, remove its own reference to the .models array.", 2, function() { | QUnit.test('#574, remove its own reference to the .models array.', function(as | |||
sert) { | ||||
assert.expect(2); | ||||
var col = new Backbone.Collection([ | var col = 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} | |||
]); | ]); | |||
equal(col.length, 6); | assert.equal(col.length, 6); | |||
col.remove(col.models); | col.remove(col.models); | |||
equal(col.length, 0); | assert.equal(col.length, 0); | |||
}); | }); | |||
test("#861, adding models to a collection which do not pass validation, with v | QUnit.test('#861, adding models to a collection which do not pass validation, | |||
alidate:true", function() { | with validate:true', function(assert) { | |||
var Model = Backbone.Model.extend({ | assert.expect(2); | |||
validate: function(attrs) { | var Model = Backbone.Model.extend({ | |||
if (attrs.id == 3) return "id can't be 3"; | validate: function(attrs) { | |||
} | 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("error", function() { ok(true); }); | collection.on('invalid', function() { assert.ok(true); }); | |||
collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {va | collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {vali | |||
lidate:true}); | date: true}); | |||
deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]); | assert.deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]); | |||
}); | }); | |||
test("Invalid models are discarded with validate:true.", 5, function() { | QUnit.test('Invalid models are discarded with validate:true.', function(assert | |||
) { | ||||
assert.expect(5); | ||||
var collection = new Backbone.Collection; | var collection = new Backbone.Collection; | |||
collection.on('test', function() { ok(true); }); | collection.on('test', function() { assert.ok(true); }); | |||
collection.model = Backbone.Model.extend({ | collection.model = Backbone.Model.extend({ | |||
validate: function(attrs){ if (!attrs.valid) return 'invalid'; } | validate: function(attrs){ if (!attrs.valid) return 'invalid'; } | |||
}); | }); | |||
var model = new collection.model({id: 1, valid: true}); | var model = new collection.model({id: 1, valid: true}); | |||
collection.add([model, {id: 2}], {validate:true}); | collection.add([model, {id: 2}], {validate: true}); | |||
model.trigger('test'); | model.trigger('test'); | |||
ok(collection.get(model.cid)); | assert.ok(collection.get(model.cid)); | |||
ok(collection.get(1)); | assert.ok(collection.get(1)); | |||
ok(!collection.get(2)); | assert.ok(!collection.get(2)); | |||
equal(collection.length, 1); | assert.equal(collection.length, 1); | |||
}); | }); | |||
test("multiple copies of the same model", 3, function() { | QUnit.test('multiple copies of the same model', function(assert) { | |||
assert.expect(3); | ||||
var col = new Backbone.Collection(); | var col = new Backbone.Collection(); | |||
var model = new Backbone.Model(); | var model = new Backbone.Model(); | |||
col.add([model, model]); | col.add([model, model]); | |||
equal(col.length, 1); | assert.equal(col.length, 1); | |||
col.add([{id: 1}, {id: 1}]); | col.add([{id: 1}, {id: 1}]); | |||
equal(col.length, 2); | assert.equal(col.length, 2); | |||
equal(col.last().id, 1); | assert.equal(col.last().id, 1); | |||
}); | }); | |||
test("#964 - collection.get return inconsistent", 2, function() { | QUnit.test('#964 - collection.get return inconsistent', function(assert) { | |||
assert.expect(2); | ||||
var c = new Backbone.Collection(); | var c = new Backbone.Collection(); | |||
ok(c.get(null) === undefined); | assert.ok(c.get(null) === undefined); | |||
ok(c.get() === undefined); | assert.ok(c.get() === undefined); | |||
}); | }); | |||
test("#1112 - passing options.model sets collection.model", 2, function() { | QUnit.test('#1112 - passing options.model sets collection.model', function(ass | |||
ert) { | ||||
assert.expect(2); | ||||
var Model = Backbone.Model.extend({}); | var Model = Backbone.Model.extend({}); | |||
var c = new Backbone.Collection([{id: 1}], {model: Model}); | var c = new Backbone.Collection([{id: 1}], {model: Model}); | |||
ok(c.model === Model); | assert.ok(c.model === Model); | |||
ok(c.at(0) instanceof Model); | assert.ok(c.at(0) instanceof Model); | |||
}); | }); | |||
test("null and undefined are invalid ids.", 2, function() { | QUnit.test('null and undefined are invalid ids.', function(assert) { | |||
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}); | |||
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}); | |||
ok(!collection.get('undefined')); | assert.ok(!collection.get('undefined')); | |||
}); | }); | |||
test("falsy comparator", 4, function(){ | QUnit.test('falsy comparator', function(assert) { | |||
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 col = 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}); | |||
ok(col.comparator); | assert.ok(col.comparator); | |||
ok(!colFalse.comparator); | assert.ok(!colFalse.comparator); | |||
ok(!colNull.comparator); | assert.ok(!colNull.comparator); | |||
ok(colUndefined.comparator); | assert.ok(colUndefined.comparator); | |||
}); | }); | |||
test("#1355 - `options` is passed to success callbacks", 2, function(){ | QUnit.test('#1355 - `options` is passed to success callbacks', function(assert | |||
var m = new Backbone.Model({x:1}); | ) { | |||
assert.expect(2); | ||||
var m = new Backbone.Model({x: 1}); | ||||
var col = new Backbone.Collection(); | var col = new Backbone.Collection(); | |||
var opts = { | var opts = { | |||
success: function(collection, resp, options){ | opts: true, | |||
ok(options); | success: function(collection, resp, options) { | |||
assert.ok(options.opts); | ||||
} | } | |||
}; | }; | |||
col.sync = m.sync = function( method, collection, options ){ | col.sync = m.sync = function( method, collection, options ){ | |||
options.success(collection, [], options); | options.success({}); | |||
}; | }; | |||
col.fetch(opts); | col.fetch(opts); | |||
col.create(m, opts); | col.create(m, opts); | |||
}); | }); | |||
test("#1412 - Trigger 'request' and 'sync' events.", 4, function() { | QUnit.test("#1412 - Trigger 'request' and 'sync' events.", function(assert) { | |||
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) { | |||
ok(obj === collection, "collection has correct 'request' event after fetch ing"); | assert.ok(obj === collection, "collection has correct 'request' event afte r fetching"); | |||
}); | }); | |||
collection.on('sync', function(obj, response, options) { | collection.on('sync', function(obj, response, options) { | |||
ok(obj === collection, "collection has correct 'sync' event after fetching "); | assert.ok(obj === collection, "collection has correct 'sync' event after f etching"); | |||
}); | }); | |||
collection.fetch(); | collection.fetch(); | |||
collection.off(); | collection.off(); | |||
collection.on('request', function(obj, xhr, options) { | collection.on('request', function(obj, xhr, options) { | |||
ok(obj === collection.get(1), "collection has correct 'request' event afte r one of its models save"); | assert.ok(obj === collection.get(1), "collection has correct 'request' eve nt after one of its models save"); | |||
}); | }); | |||
collection.on('sync', function(obj, response, options) { | collection.on('sync', function(obj, response, options) { | |||
ok(obj === collection.get(1), "collection has correct 'sync' event after o ne of its models save"); | assert.ok(obj === collection.get(1), "collection has correct 'sync' event after one of its models save"); | |||
}); | }); | |||
collection.create({id: 1}); | collection.create({id: 1}); | |||
collection.off(); | collection.off(); | |||
}); | }); | |||
test("#1447 - create with wait adds model.", 1, function() { | QUnit.test('#3283 - fetch, create calls success with context', function(assert | |||
) { | ||||
assert.expect(2); | ||||
var collection = new Backbone.Collection; | ||||
collection.url = '/test'; | ||||
Backbone.ajax = function(settings) { | ||||
settings.success.call(settings.context); | ||||
}; | ||||
var obj = {}; | ||||
var options = { | ||||
context: obj, | ||||
success: function() { | ||||
assert.equal(this, obj); | ||||
} | ||||
}; | ||||
collection.fetch(options); | ||||
collection.create({id: 1}, options); | ||||
}); | ||||
QUnit.test('#1447 - create with wait adds model.', function(assert) { | ||||
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, model, options){ options.success(); }; | |||
collection.on('add', function(){ ok(true); }); | collection.on('add', function(){ assert.ok(true); }); | |||
collection.create(model, {wait: true}); | collection.create(model, {wait: true}); | |||
}); | }); | |||
test("#1448 - add sorts collection after merge.", 1, function() { | QUnit.test('#1448 - add sorts collection after merge.', function(assert) { | |||
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} | |||
]); | ]); | |||
collection.comparator = function(model){ return model.get('x'); }; | collection.comparator = function(model){ return model.get('x'); }; | |||
collection.add({id: 1, x: 3}, {merge: true}); | collection.add({id: 1, x: 3}, {merge: true}); | |||
deepEqual(collection.pluck('id'), [2, 1]); | assert.deepEqual(collection.pluck('id'), [2, 1]); | |||
}); | }); | |||
test("#1655 - groupBy can be used with a string argument.", 3, function() { | QUnit.test('#1655 - groupBy can be used with a string argument.', function(ass | |||
ert) { | ||||
assert.expect(3); | ||||
var collection = new Backbone.Collection([{x: 1}, {x: 2}]); | var collection = new Backbone.Collection([{x: 1}, {x: 2}]); | |||
var grouped = collection.groupBy('x'); | var grouped = collection.groupBy('x'); | |||
strictEqual(_.keys(grouped).length, 2); | assert.strictEqual(_.keys(grouped).length, 2); | |||
strictEqual(grouped[1][0].get('x'), 1); | assert.strictEqual(grouped[1][0].get('x'), 1); | |||
strictEqual(grouped[2][0].get('x'), 2); | assert.strictEqual(grouped[2][0].get('x'), 2); | |||
}); | }); | |||
test("#1655 - sortBy can be used with a string argument.", 1, function() { | QUnit.test('#1655 - sortBy can be used with a string argument.', function(asse | |||
rt) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]); | var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]); | |||
var values = _.map(collection.sortBy('x'), function(model) { | var values = _.map(collection.sortBy('x'), function(model) { | |||
return model.get('x'); | return model.get('x'); | |||
}); | }); | |||
deepEqual(values, [1, 2, 3]); | assert.deepEqual(values, [1, 2, 3]); | |||
}); | }); | |||
test("#1604 - Removal during iteration.", 0, function() { | QUnit.test('#1604 - Removal during iteration.', function(assert) { | |||
assert.expect(0); | ||||
var collection = new Backbone.Collection([{}, {}]); | var collection = new Backbone.Collection([{}, {}]); | |||
collection.on('add', function() { | collection.on('add', function() { | |||
collection.at(0).destroy(); | collection.at(0).destroy(); | |||
}); | }); | |||
collection.add({}, {at: 0}); | collection.add({}, {at: 0}); | |||
}); | }); | |||
test("#1638 - `sort` during `add` triggers correctly.", function() { | QUnit.test('#1638 - `sort` during `add` triggers correctly.', function(assert) { | |||
var collection = new Backbone.Collection; | var collection = new Backbone.Collection; | |||
collection.comparator = function(model) { return model.get('x'); }; | collection.comparator = function(model) { return model.get('x'); }; | |||
var added = []; | var added = []; | |||
collection.on('add', function(model) { | collection.on('add', function(model) { | |||
model.set({x: 3}); | model.set({x: 3}); | |||
collection.sort(); | collection.sort(); | |||
added.push(model.id); | added.push(model.id); | |||
}); | }); | |||
collection.add([{id: 1, x: 1}, {id: 2, x: 2}]); | collection.add([{id: 1, x: 1}, {id: 2, x: 2}]); | |||
deepEqual(added, [1, 2]); | assert.deepEqual(added, [1, 2]); | |||
}); | }); | |||
test("fetch parses models by default", 1, function() { | QUnit.test('fetch parses models by default', function(assert) { | |||
assert.expect(1); | ||||
var model = {}; | var model = {}; | |||
var Collection = Backbone.Collection.extend({ | var Collection = Backbone.Collection.extend({ | |||
url: 'test', | url: 'test', | |||
model: Backbone.Model.extend({ | model: Backbone.Model.extend({ | |||
parse: function(resp) { | parse: function(resp) { | |||
strictEqual(resp, model); | assert.strictEqual(resp, model); | |||
} | } | |||
}) | }) | |||
}); | }); | |||
new Collection().fetch(); | new Collection().fetch(); | |||
this.ajaxSettings.success([model]); | this.ajaxSettings.success([model]); | |||
}); | }); | |||
test("`sort` shouldn't always fire on `add`", 1, function() { | QUnit.test("`sort` shouldn't always fire on `add`", function(assert) { | |||
assert.expect(1); | ||||
var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], { | var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], { | |||
comparator: 'id' | comparator: 'id' | |||
}); | }); | |||
c.sort = function(){ ok(true); }; | c.sort = function(){ assert.ok(true); }; | |||
c.add([]); | c.add([]); | |||
c.add({id: 1}); | c.add({id: 1}); | |||
c.add([{id: 2}, {id: 3}]); | c.add([{id: 2}, {id: 3}]); | |||
c.add({id: 4}); | c.add({id: 4}); | |||
}); | }); | |||
test("#1407 parse option on constructor parses collection and models", 2, func | QUnit.test('#1407 parse option on constructor parses collection and models', f | |||
tion() { | unction(assert) { | |||
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(model) { | |||
model.name = 'test'; | model.name = 'test'; | |||
return model; | return model; | |||
} | } | |||
}), | }), | |||
parse: function(model) { | parse: function(model) { | |||
return model.namespace; | return model.namespace; | |||
} | } | |||
}); | }); | |||
var c = new Collection(model, {parse:true}); | var c = new Collection(model, {parse: true}); | |||
equal(c.length, 2); | assert.equal(c.length, 2); | |||
equal(c.at(0).get('name'), 'test'); | assert.equal(c.at(0).get('name'), 'test'); | |||
}); | }); | |||
test("#1407 parse option on reset parses collection and models", 2, function() | QUnit.test('#1407 parse option on reset parses collection and models', functio | |||
{ | n(assert) { | |||
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(model) { | |||
model.name = 'test'; | model.name = 'test'; | |||
return model; | return model; | |||
} | } | |||
}), | }), | |||
parse: function(model) { | parse: function(model) { | |||
return model.namespace; | return model.namespace; | |||
} | } | |||
}); | }); | |||
var c = new Collection(); | var c = new Collection(); | |||
c.reset(model, {parse:true}); | c.reset(model, {parse: true}); | |||
equal(c.length, 2); | assert.equal(c.length, 2); | |||
equal(c.at(0).get('name'), 'test'); | assert.equal(c.at(0).get('name'), 'test'); | |||
}); | }); | |||
test("Reset includes previous models in triggered event.", 1, function() { | QUnit.test('Reset includes previous models in triggered event.', function(asse | |||
rt) { | ||||
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) { | .on('reset', function(collection, options) { | |||
deepEqual(options.previousModels, [model]); | assert.deepEqual(options.previousModels, [model]); | |||
}); | }); | |||
collection.reset([]); | collection.reset([]); | |||
}); | }); | |||
test("set", function() { | 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 c = new Backbone.Collection([m1, m2]); | |||
// Test add/change/remove events | // Test add/change/remove events | |||
c.on('add', function(model) { | c.on('add', function(model) { | |||
strictEqual(model, m3); | assert.strictEqual(model, m3); | |||
}); | }); | |||
c.on('change', function(model) { | c.on('change', function(model) { | |||
strictEqual(model, m2); | assert.strictEqual(model, m2); | |||
}); | }); | |||
c.on('remove', function(model) { | c.on('remove', function(model) { | |||
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}); | c.set([], {remove: false}); | |||
strictEqual(c.length, 2); | assert.strictEqual(c.length, 2); | |||
// add: false doesn't add any models | // add: false doesn't add any models | |||
c.set([m1, m2, m3], {add: false}); | c.set([m1, m2, m3], {add: false}); | |||
strictEqual(c.length, 2); | assert.strictEqual(c.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}); | c.set([m1, {id: 2, a: 1}], {merge: false}); | |||
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}); | c.set([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false}); | |||
strictEqual(c.length, 2); | assert.strictEqual(c.length, 2); | |||
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]); | c.set([{id: 2, a: 1}, m3]); | |||
strictEqual(c.length, 2); | assert.strictEqual(c.length, 2); | |||
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) { | c.off('remove').on('remove', function(model) { | |||
ok(model === m2 || model === m3); | assert.ok(model === m2 || model === m3); | |||
}); | }); | |||
c.set([]); | c.set([]); | |||
strictEqual(c.length, 0); | assert.strictEqual(c.length, 0); | |||
// Test null models on set doesn't clear collection | ||||
c.off(); | ||||
c.set([{id: 1}]); | ||||
c.set(); | ||||
assert.strictEqual(c.length, 1); | ||||
}); | }); | |||
test("set with only cids", 3, function() { | QUnit.test('set with only cids', function(assert) { | |||
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 c = new Backbone.Collection; | |||
c.set([m1, m2]); | c.set([m1, m2]); | |||
equal(c.length, 2); | assert.equal(c.length, 2); | |||
c.set([m1]); | c.set([m1]); | |||
equal(c.length, 1); | assert.equal(c.length, 1); | |||
c.set([m1, m1, m1, m2, m2], {remove: false}); | c.set([m1, m1, m1, m2, m2], {remove: false}); | |||
equal(c.length, 2); | assert.equal(c.length, 2); | |||
}); | }); | |||
test("set with only idAttribute", 3, function() { | QUnit.test('set with only idAttribute', function(assert) { | |||
var m1 = { _id: 1 }; | assert.expect(3); | |||
var m2 = { _id: 2 }; | var m1 = {_id: 1}; | |||
var col = Backbone.Collection.extend({ | var m2 = {_id: 2}; | |||
var Col = Backbone.Collection.extend({ | ||||
model: Backbone.Model.extend({ | model: Backbone.Model.extend({ | |||
idAttribute: '_id' | idAttribute: '_id' | |||
}) | }) | |||
}); | }); | |||
var c = new col; | var c = new Col; | |||
c.set([m1, m2]); | c.set([m1, m2]); | |||
equal(c.length, 2); | assert.equal(c.length, 2); | |||
c.set([m1]); | c.set([m1]); | |||
equal(c.length, 1); | assert.equal(c.length, 1); | |||
c.set([m1, m1, m1, m2, m2], {remove: false}); | c.set([m1, m1, m1, m2, m2], {remove: false}); | |||
equal(c.length, 2); | assert.equal(c.length, 2); | |||
}); | }); | |||
test("set + merge with default values defined", function() { | 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 col = new Backbone.Collection([m], {model: Model}); | |||
equal(col.first().get('key'), 'value'); | assert.equal(col.first().get('key'), 'value'); | |||
col.set({id: 1, key: 'other'}); | col.set({id: 1, key: 'other'}); | |||
equal(col.first().get('key'), 'other'); | assert.equal(col.first().get('key'), 'other'); | |||
col.set({id: 1, other: 'value'}); | col.set({id: 1, other: 'value'}); | |||
equal(col.first().get('key'), 'other'); | assert.equal(col.first().get('key'), 'other'); | |||
equal(col.length, 1); | assert.equal(col.length, 1); | |||
}); | }); | |||
test('merge without mutation', function () { | 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); | |||
} | } | |||
} | } | |||
}); | }); | |||
var Collection = Backbone.Collection.extend({model: Model}); | var Collection = Backbone.Collection.extend({model: Model}); | |||
var data = [{id: 1, child: {id: 2}}]; | var data = [{id: 1, child: {id: 2}}]; | |||
var collection = new Collection(data); | var collection = new Collection(data); | |||
equal(collection.first().id, 1); | assert.equal(collection.first().id, 1); | |||
collection.set(data); | collection.set(data); | |||
equal(collection.first().id, 1); | assert.equal(collection.first().id, 1); | |||
collection.set([{id: 2, child: {id: 2}}].concat(data)); | collection.set([{id: 2, child: {id: 2}}].concat(data)); | |||
deepEqual(collection.pluck('id'), [2, 1]); | assert.deepEqual(collection.pluck('id'), [2, 1]); | |||
}); | }); | |||
test("`set` and model level `parse`", function() { | QUnit.test('`set` and model level `parse`', function(assert) { | |||
var Model = Backbone.Model.extend({}); | var Model = Backbone.Model.extend({}); | |||
var Collection = Backbone.Collection.extend({ | var Collection = Backbone.Collection.extend({ | |||
model: Model, | model: Model, | |||
parse: function (res) { return _.pluck(res.models, 'model'); } | parse: function(res) { return _.map(res.models, 'model'); } | |||
}); | }); | |||
var model = new Model({id: 1}); | var model = new Model({id: 1}); | |||
var collection = new Collection(model); | var collection = new Collection(model); | |||
collection.set({models: [ | collection.set({models: [ | |||
{model: {id: 1}}, | {model: {id: 1}}, | |||
{model: {id: 2}} | {model: {id: 2}} | |||
]}, {parse: true}); | ]}, {parse: true}); | |||
equal(collection.first(), model); | assert.equal(collection.first(), model); | |||
}); | }); | |||
test("`set` data is only parsed once", function() { | QUnit.test('`set` data is only parsed once', function(assert) { | |||
var collection = new Backbone.Collection(); | var collection = new Backbone.Collection(); | |||
collection.model = Backbone.Model.extend({ | collection.model = Backbone.Model.extend({ | |||
parse: function (data) { | parse: function(data) { | |||
equal(data.parsed, void 0); | assert.equal(data.parsed, void 0); | |||
data.parsed = true; | data.parsed = true; | |||
return data; | return data; | |||
} | } | |||
}); | }); | |||
collection.set({}, {parse: true}); | collection.set({}, {parse: true}); | |||
}); | }); | |||
test('`set` matches input order in the absence of a comparator', function () { | QUnit.test('`set` matches input order in the absence of a comparator', functio n(assert) { | |||
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.set([{id: 3}, {id: 2}, {id: 1}]); | collection.set([{id: 3}, {id: 2}, {id: 1}]); | |||
deepEqual(collection.models, [three, two, one]); | assert.deepEqual(collection.models, [three, two, one]); | |||
collection.set([{id: 1}, {id: 2}]); | collection.set([{id: 1}, {id: 2}]); | |||
deepEqual(collection.models, [one, two]); | assert.deepEqual(collection.models, [one, two]); | |||
collection.set([two, three, one]); | collection.set([two, three, one]); | |||
deepEqual(collection.models, [two, three, one]); | assert.deepEqual(collection.models, [two, three, one]); | |||
collection.set([{id: 1}, {id: 2}], {remove: false}); | collection.set([{id: 1}, {id: 2}], {remove: false}); | |||
deepEqual(collection.models, [two, three, one]); | assert.deepEqual(collection.models, [two, three, one]); | |||
collection.set([{id: 1}, {id: 2}, {id: 3}], {merge: false}); | collection.set([{id: 1}, {id: 2}, {id: 3}], {merge: false}); | |||
deepEqual(collection.models, [one, two, three]); | assert.deepEqual(collection.models, [one, two, three]); | |||
collection.set([three, two, one, {id: 4}], {add: false}); | collection.set([three, two, one, {id: 4}], {add: false}); | |||
deepEqual(collection.models, [one, two, three]); | assert.deepEqual(collection.models, [one, two, three]); | |||
}); | }); | |||
test("#1894 - Push should not trigger a sort", 0, function() { | QUnit.test('#1894 - Push should not trigger a sort', function(assert) { | |||
assert.expect(0); | ||||
var Collection = Backbone.Collection.extend({ | var Collection = Backbone.Collection.extend({ | |||
comparator: 'id', | comparator: 'id', | |||
sort: function() { | sort: function() { assert.ok(false); } | |||
ok(false); | ||||
} | ||||
}); | }); | |||
new Collection().push({id: 1}); | new Collection().push({id: 1}); | |||
}); | }); | |||
test("#2428 - push duplicate models, return the correct one", 1, function() { | QUnit.test('#2428 - push duplicate models, return the correct one', function(a | |||
ssert) { | ||||
assert.expect(1); | ||||
var col = new Backbone.Collection; | var col = new Backbone.Collection; | |||
var model1 = col.push({id: 101}); | var model1 = col.push({id: 101}); | |||
var model2 = col.push({id: 101}) | var model2 = col.push({id: 101}); | |||
ok(model2.cid == model1.cid); | assert.ok(model2.cid == model1.cid); | |||
}); | }); | |||
test("`set` with non-normal id", function() { | 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}); | |||
equal(collection.first().get('a'), 1); | assert.equal(collection.first().get('a'), 1); | |||
}); | }); | |||
test("#1894 - `sort` can optionally be turned off", 0, function() { | QUnit.test('#1894 - `sort` can optionally be turned off', function(assert) { | |||
assert.expect(0); | ||||
var Collection = Backbone.Collection.extend({ | var Collection = Backbone.Collection.extend({ | |||
comparator: 'id', | comparator: 'id', | |||
sort: function() { ok(true); } | sort: function() { assert.ok(false); } | |||
}); | }); | |||
new Collection().add({id: 1}, {sort: false}); | new Collection().add({id: 1}, {sort: false}); | |||
}); | }); | |||
test("#1915 - `parse` data in the right order in `set`", function() { | QUnit.test('#1915 - `parse` data in the right order in `set`', function(assert ) { | |||
var collection = new (Backbone.Collection.extend({ | var collection = new (Backbone.Collection.extend({ | |||
parse: function (data) { | parse: function(data) { | |||
strictEqual(data.status, 'ok'); | assert.strictEqual(data.status, 'ok'); | |||
return data.data; | return data.data; | |||
} | } | |||
})); | })); | |||
var res = {status: 'ok', data:[{id: 1}]}; | var res = {status: 'ok', data: [{id: 1}]}; | |||
collection.set(res, {parse: true}); | collection.set(res, {parse: true}); | |||
}); | }); | |||
asyncTest("#1939 - `parse` is passed `options`", 1, function () { | QUnit.test('#1939 - `parse` is passed `options`', function(assert) { | |||
var done = assert.async(); | ||||
assert.expect(1); | ||||
var collection = new (Backbone.Collection.extend({ | var collection = new (Backbone.Collection.extend({ | |||
url: '/', | url: '/', | |||
parse: function (data, options) { | parse: function(data, options) { | |||
strictEqual(options.xhr.someHeader, 'headerValue'); | assert.strictEqual(options.xhr.someHeader, 'headerValue'); | |||
return data; | return data; | |||
} | } | |||
})); | })); | |||
var ajax = Backbone.ajax; | var ajax = Backbone.ajax; | |||
Backbone.ajax = function (params) { | Backbone.ajax = function(params) { | |||
_.defer(params.success); | _.defer(params.success, []); | |||
return {someHeader: 'headerValue'}; | return {someHeader: 'headerValue'}; | |||
}; | }; | |||
collection.fetch({ | collection.fetch({ | |||
success: function () { start(); } | success: function() { done(); } | |||
}); | }); | |||
Backbone.ajax = ajax; | Backbone.ajax = ajax; | |||
}); | }); | |||
test("`add` only `sort`s when necessary", 2, function () { | QUnit.test('fetch will pass extra options to success callback', function(asser | |||
t) { | ||||
assert.expect(1); | ||||
var SpecialSyncCollection = Backbone.Collection.extend({ | ||||
url: '/test', | ||||
sync: function(method, collection, options) { | ||||
_.extend(options, {specialSync: true}); | ||||
return Backbone.Collection.prototype.sync.call(this, method, collection, | ||||
options); | ||||
} | ||||
}); | ||||
var collection = new SpecialSyncCollection(); | ||||
var onSuccess = function(collection, resp, options) { | ||||
assert.ok(options.specialSync, 'Options were passed correctly to callback' | ||||
); | ||||
}; | ||||
collection.fetch({success: onSuccess}); | ||||
this.ajaxSettings.success(); | ||||
}); | ||||
QUnit.test('`add` only `sort`s when necessary', function(assert) { | ||||
assert.expect(2); | ||||
var collection = new (Backbone.Collection.extend({ | var collection = new (Backbone.Collection.extend({ | |||
comparator: 'a' | comparator: 'a' | |||
}))([{id: 1}, {id: 2}, {id: 3}]); | }))([{id: 1}, {id: 2}, {id: 3}]); | |||
collection.on('sort', function () { 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, 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 | |||
}); | }); | |||
test("`add` only `sort`s when necessary with comparator function", 3, function | QUnit.test('`add` only `sort`s when necessary with comparator function', funct | |||
() { | ion(assert) { | |||
assert.expect(3); | ||||
var collection = new (Backbone.Collection.extend({ | var collection = new (Backbone.Collection.extend({ | |||
comparator: function(a, b) { | comparator: function(a, b) { | |||
return a.get('a') > b.get('a') ? 1 : (a.get('a') < b.get('a') ? -1 : 0); | return a.get('a') > b.get('a') ? 1 : (a.get('a') < b.get('a') ? -1 : 0); | |||
} | } | |||
}))([{id: 1}, {id: 2}, {id: 3}]); | }))([{id: 1}, {id: 2}, {id: 3}]); | |||
collection.on('sort', function () { 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 | |||
}); | }); | |||
test("Attach options to collection.", 2, function() { | QUnit.test('Attach options to collection.', function(assert) { | |||
var model = new Backbone.Model; | assert.expect(2); | |||
var Model = Backbone.Model; | ||||
var comparator = function(){}; | var comparator = function(){}; | |||
var collection = new Backbone.Collection([], { | var collection = new Backbone.Collection([], { | |||
model: model, | model: Model, | |||
comparator: comparator | comparator: comparator | |||
}); | }); | |||
ok(collection.model === model); | assert.ok(collection.model === Model); | |||
ok(collection.comparator === comparator); | assert.ok(collection.comparator === comparator); | |||
}); | }); | |||
test("`add` overrides `set` flags", function () { | QUnit.test('Pass falsey for `models` for empty Col with `options`', function(a | |||
ssert) { | ||||
assert.expect(9); | ||||
var opts = {a: 1, b: 2}; | ||||
_.forEach([undefined, null, false], function(falsey) { | ||||
var Collection = Backbone.Collection.extend({ | ||||
initialize: function(models, options) { | ||||
assert.strictEqual(models, falsey); | ||||
assert.strictEqual(options, opts); | ||||
} | ||||
}); | ||||
var col = new Collection(falsey, opts); | ||||
assert.strictEqual(col.length, 0); | ||||
}); | ||||
}); | ||||
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, collection, options) { | |||
collection.add({id: 2}, options); | collection.add({id: 2}, options); | |||
}); | }); | |||
collection.set({id: 1}); | collection.set({id: 1}); | |||
equal(collection.length, 2); | assert.equal(collection.length, 2); | |||
}); | }); | |||
test("#2606 - Collection#create, success arguments", 1, function() { | QUnit.test('#2606 - Collection#create, success arguments', function(assert) { | |||
assert.expect(1); | ||||
var collection = new Backbone.Collection; | var collection = new Backbone.Collection; | |||
collection.url = 'test'; | collection.url = 'test'; | |||
collection.create({}, { | collection.create({}, { | |||
success: function(model, resp, options) { | success: function(model, resp, options) { | |||
strictEqual(resp, 'response'); | assert.strictEqual(resp, 'response'); | |||
} | } | |||
}); | }); | |||
this.ajaxSettings.success('response'); | this.ajaxSettings.success('response'); | |||
}); | }); | |||
test("#2612 - nested `parse` works with `Collection#set`", function() { | QUnit.test('#2612 - nested `parse` works with `Collection#set`', function(asse rt) { | |||
var Job = Backbone.Model.extend({ | var Job = Backbone.Model.extend({ | |||
constructor: function() { | constructor: function() { | |||
this.items = new Items(); | this.items = new Items(); | |||
Backbone.Model.apply(this, arguments); | Backbone.Model.apply(this, arguments); | |||
}, | }, | |||
parse: function(attrs) { | parse: function(attrs) { | |||
this.items.set(attrs.items, {parse: true}); | this.items.set(attrs.items, {parse: true}); | |||
return _.omit(attrs, 'items'); | return _.omit(attrs, 'items'); | |||
} | } | |||
skipping to change at line 1264 | skipping to change at line 1535 | |||
}] | }] | |||
}; | }; | |||
var newData = { | var newData = { | |||
name: 'NewJobName', | name: 'NewJobName', | |||
id: 1, | id: 1, | |||
items: [{ | items: [{ | |||
id: 1, | id: 1, | |||
name: 'NewSub1', | name: 'NewSub1', | |||
subItems: [ | subItems: [ | |||
{id: 1,subName: 'NewOne'}, | {id: 1, subName: 'NewOne'}, | |||
{id: 2,subName: 'NewTwo'} | {id: 2, subName: 'NewTwo'} | |||
] | ] | |||
}, { | }, { | |||
id: 2, | id: 2, | |||
name: 'NewSub2', | name: 'NewSub2', | |||
subItems: [ | subItems: [ | |||
{id: 3,subName: 'NewThree'}, | {id: 3, subName: 'NewThree'}, | |||
{id: 4,subName: 'NewFour'} | {id: 4, subName: 'NewFour'} | |||
] | ] | |||
}] | }] | |||
}; | }; | |||
var job = new Job(data, {parse: true}); | var job = new Job(data, {parse: true}); | |||
equal(job.get('name'), 'JobName'); | assert.equal(job.get('name'), 'JobName'); | |||
equal(job.items.at(0).get('name'), 'Sub1'); | assert.equal(job.items.at(0).get('name'), 'Sub1'); | |||
equal(job.items.length, 2); | assert.equal(job.items.length, 2); | |||
equal(job.items.get(1).subItems.get(1).get('subName'), 'One'); | assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'One'); | |||
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})); | |||
equal(job.get('name'), 'NewJobName'); | assert.equal(job.get('name'), 'NewJobName'); | |||
equal(job.items.at(0).get('name'), 'NewSub1'); | assert.equal(job.items.at(0).get('name'), 'NewSub1'); | |||
equal(job.items.length, 2); | assert.equal(job.items.length, 2); | |||
equal(job.items.get(1).subItems.get(1).get('subName'), 'NewOne'); | assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'NewOne'); | |||
equal(job.items.get(2).subItems.get(3).get('subName'), 'NewThree'); | assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'NewThree'); | |||
}); | }); | |||
test('_addReference binds all collection events & adds to the lookup hashes', | QUnit.test('_addReference binds all collection events & adds to the lookup has | |||
9, function() { | hes', function(assert) { | |||
assert.expect(9); | ||||
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++; | |||
equal(model, this._byId[model.id]); | assert.equal(model, this._byId[model.id]); | |||
equal(model, this._byId[model.cid]); | assert.equal(model, this._byId[model.cid]); | |||
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++; | |||
equal(this._byId[model.id], void 0); | assert.equal(this._byId[model.id], void 0); | |||
equal(this._byId[model.cid], void 0); | assert.equal(this._byId[model.cid], void 0); | |||
equal(model.collection, void 0); | assert.equal(model.collection, void 0); | |||
equal(model._events.all, 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); | |||
equal(calls.add, 1); | assert.equal(calls.add, 1); | |||
equal(calls.remove, 1); | assert.equal(calls.remove, 1); | |||
}); | }); | |||
test('Do not allow duplicate models to be `add`ed or `set`', function() { | QUnit.test('Do not allow duplicate models to be `add`ed or `set`', function(as sert) { | |||
var c = new Backbone.Collection(); | var c = new Backbone.Collection(); | |||
c.add([{id: 1}, {id: 1}]); | c.add([{id: 1}, {id: 1}]); | |||
equal(c.length, 1); | assert.equal(c.length, 1); | |||
equal(c.models.length, 1); | assert.equal(c.models.length, 1); | |||
c.set([{id: 1}, {id: 1}]); | c.set([{id: 1}, {id: 1}]); | |||
equal(c.length, 1); | assert.equal(c.length, 1); | |||
equal(c.models.length, 1); | assert.equal(c.models.length, 1); | |||
}); | ||||
QUnit.test('#3020: #set with {add: false} should not throw.', function(assert) | ||||
{ | ||||
assert.expect(2); | ||||
var collection = new Backbone.Collection; | ||||
collection.set([{id: 1}], {add: false}); | ||||
assert.strictEqual(collection.length, 0); | ||||
assert.strictEqual(collection.models.length, 0); | ||||
}); | ||||
QUnit.test('create with wait, model instance, #3028', function(assert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection(); | ||||
var model = new Backbone.Model({id: 1}); | ||||
model.sync = function(){ | ||||
assert.equal(this.collection, collection); | ||||
}; | ||||
collection.create(model, {wait: true}); | ||||
}); | ||||
QUnit.test('modelId', function(assert) { | ||||
var Stooge = Backbone.Model.extend(); | ||||
var StoogeCollection = Backbone.Collection.extend({model: Stooge}); | ||||
// Default to using `Collection::model::idAttribute`. | ||||
assert.equal(StoogeCollection.prototype.modelId({id: 1}), 1); | ||||
Stooge.prototype.idAttribute = '_id'; | ||||
assert.equal(StoogeCollection.prototype.modelId({_id: 1}), 1); | ||||
}); | ||||
QUnit.test('Polymorphic models work with "simple" constructors', function(asse | ||||
rt) { | ||||
var A = Backbone.Model.extend(); | ||||
var B = Backbone.Model.extend(); | ||||
var C = Backbone.Collection.extend({ | ||||
model: function(attrs) { | ||||
return attrs.type === 'a' ? new A(attrs) : new B(attrs); | ||||
} | ||||
}); | ||||
var collection = new C([{id: 1, type: 'a'}, {id: 2, type: 'b'}]); | ||||
assert.equal(collection.length, 2); | ||||
assert.ok(collection.at(0) instanceof A); | ||||
assert.equal(collection.at(0).id, 1); | ||||
assert.ok(collection.at(1) instanceof B); | ||||
assert.equal(collection.at(1).id, 2); | ||||
}); | ||||
QUnit.test('Polymorphic models work with "advanced" constructors', function(as | ||||
sert) { | ||||
var A = Backbone.Model.extend({idAttribute: '_id'}); | ||||
var B = Backbone.Model.extend({idAttribute: '_id'}); | ||||
var C = Backbone.Collection.extend({ | ||||
model: Backbone.Model.extend({ | ||||
constructor: function(attrs) { | ||||
return attrs.type === 'a' ? new A(attrs) : new B(attrs); | ||||
}, | ||||
idAttribute: '_id' | ||||
}) | ||||
}); | ||||
var collection = new C([{_id: 1, type: 'a'}, {_id: 2, type: 'b'}]); | ||||
assert.equal(collection.length, 2); | ||||
assert.ok(collection.at(0) instanceof A); | ||||
assert.equal(collection.at(0), collection.get(1)); | ||||
assert.ok(collection.at(1) instanceof B); | ||||
assert.equal(collection.at(1), collection.get(2)); | ||||
C = Backbone.Collection.extend({ | ||||
model: function(attrs) { | ||||
return attrs.type === 'a' ? new A(attrs) : new B(attrs); | ||||
}, | ||||
modelId: function(attrs) { | ||||
return attrs.type + '-' + attrs.id; | ||||
} | ||||
}); | ||||
collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]); | ||||
assert.equal(collection.length, 2); | ||||
assert.ok(collection.at(0) instanceof A); | ||||
assert.equal(collection.at(0), collection.get('a-1')); | ||||
assert.ok(collection.at(1) instanceof B); | ||||
assert.equal(collection.at(1), collection.get('b-1')); | ||||
}); | ||||
QUnit.test('#3039: adding at index fires with correct at', function(assert) { | ||||
assert.expect(3); | ||||
var col = new Backbone.Collection([{at: 0}, {at: 4}]); | ||||
col.on('add', function(model, col, options) { | ||||
assert.equal(model.get('at'), options.index); | ||||
}); | ||||
col.add([{at: 1}, {at: 2}, {at: 3}], {at: 1}); | ||||
}); | ||||
QUnit.test('#3039: index is not sent when at is not specified', function(asser | ||||
t) { | ||||
assert.expect(2); | ||||
var col = new Backbone.Collection([{at: 0}]); | ||||
col.on('add', function(model, col, options) { | ||||
assert.equal(undefined, options.index); | ||||
}); | ||||
col.add([{at: 1}, {at: 2}]); | ||||
}); | ||||
QUnit.test('#3199 - Order changing should trigger a sort', function(assert) { | ||||
assert.expect(1); | ||||
var one = new Backbone.Model({id: 1}); | ||||
var two = new Backbone.Model({id: 2}); | ||||
var three = new Backbone.Model({id: 3}); | ||||
var collection = new Backbone.Collection([one, two, three]); | ||||
collection.on('sort', function() { | ||||
assert.ok(true); | ||||
}); | ||||
collection.set([{id: 3}, {id: 2}, {id: 1}]); | ||||
}); | ||||
QUnit.test('#3199 - Adding a model should trigger a sort', function(assert) { | ||||
assert.expect(1); | ||||
var one = new Backbone.Model({id: 1}); | ||||
var two = new Backbone.Model({id: 2}); | ||||
var three = new Backbone.Model({id: 3}); | ||||
var collection = new Backbone.Collection([one, two, three]); | ||||
collection.on('sort', function() { | ||||
assert.ok(true); | ||||
}); | ||||
collection.set([{id: 1}, {id: 2}, {id: 3}, {id: 0}]); | ||||
}); | ||||
QUnit.test('#3199 - Order not changing should not trigger a sort', function(as | ||||
sert) { | ||||
assert.expect(0); | ||||
var one = new Backbone.Model({id: 1}); | ||||
var two = new Backbone.Model({id: 2}); | ||||
var three = new Backbone.Model({id: 3}); | ||||
var collection = new Backbone.Collection([one, two, three]); | ||||
collection.on('sort', function() { | ||||
assert.ok(false); | ||||
}); | ||||
collection.set([{id: 1}, {id: 2}, {id: 3}]); | ||||
}); | ||||
QUnit.test('add supports negative indexes', function(assert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection([{id: 1}]); | ||||
collection.add([{id: 2}, {id: 3}], {at: -1}); | ||||
collection.add([{id: 2.5}], {at: -2}); | ||||
collection.add([{id: 0.5}], {at: -6}); | ||||
assert.equal(collection.pluck('id').join(','), '0.5,1,2,2.5,3'); | ||||
}); | ||||
QUnit.test('#set accepts options.at as a string', function(assert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); | ||||
collection.add([{id: 3}], {at: '1'}); | ||||
assert.deepEqual(collection.pluck('id'), [1, 3, 2]); | ||||
}); | ||||
QUnit.test('adding multiple models triggers `update` event once', function(ass | ||||
ert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection; | ||||
collection.on('update', function() { assert.ok(true); }); | ||||
collection.add([{id: 1}, {id: 2}, {id: 3}]); | ||||
}); | ||||
QUnit.test('removing models triggers `update` event once', function(assert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}]); | ||||
collection.on('update', function() { assert.ok(true); }); | ||||
collection.remove([{id: 1}, {id: 2}]); | ||||
}); | ||||
QUnit.test('remove does not trigger `set` when nothing removed', function(asse | ||||
rt) { | ||||
assert.expect(0); | ||||
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); | ||||
collection.on('update', function() { assert.ok(false); }); | ||||
collection.remove([{id: 3}]); | ||||
}); | ||||
QUnit.test('set triggers `set` event once', function(assert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection([{id: 1}, {id: 2}]); | ||||
collection.on('update', function() { assert.ok(true); }); | ||||
collection.set([{id: 1}, {id: 3}]); | ||||
}); | ||||
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}]); | ||||
collection.on('update', function() { assert.ok(false); }); | ||||
collection.set([{id: 1}, {id: 2}]); | ||||
}); | ||||
QUnit.test('#3610 - invoke collects arguments', function(assert) { | ||||
assert.expect(3); | ||||
var Model = Backbone.Model.extend({ | ||||
method: function(a, b, c) { | ||||
assert.equal(a, 1); | ||||
assert.equal(b, 2); | ||||
assert.equal(c, 3); | ||||
} | ||||
}); | ||||
var Collection = Backbone.Collection.extend({ | ||||
model: Model | ||||
}); | ||||
var collection = new Collection([{id: 1}]); | ||||
collection.invoke('method', 1, 2, 3); | ||||
}); | ||||
QUnit.test('#3662 - triggering change without model will not error', function( | ||||
assert) { | ||||
assert.expect(1); | ||||
var collection = new Backbone.Collection([{id: 1}]); | ||||
var model = collection.first(); | ||||
collection.on('change', function(model) { | ||||
assert.equal(model, undefined); | ||||
}); | ||||
model.trigger('change'); | ||||
}); | ||||
QUnit.test('#3871 - falsy parse result creates empty collection', function(ass | ||||
ert) { | ||||
var collection = new (Backbone.Collection.extend({ | ||||
parse: function(data, options) {} | ||||
})); | ||||
collection.set('', {parse: true}); | ||||
assert.equal(collection.length, 0); | ||||
}); | }); | |||
})(); | })(); | |||
End of changes. 281 change blocks. | ||||
487 lines changed or deleted | 1016 lines changed or added |