PageRenderTime 817ms CodeModel.GetById 161ms app.highlight 481ms RepoModel.GetById 163ms app.codeStats 1ms

/test/collection.js

https://github.com/harshniketseta/backbone
JavaScript | 968 lines | 863 code | 98 blank | 7 comment | 10 complexity | 02eb446c57306d8fb69ca79a4712fa79 MD5 | raw file
  1$(document).ready(function() {
  2
  3  var a, b, c, d, e, col, otherCol;
  4
  5  module("Backbone.Collection", _.extend(new Environment, {
  6
  7    setup: function() {
  8      Environment.prototype.setup.apply(this, arguments);
  9
 10      a         = new Backbone.Model({id: 3, label: 'a'});
 11      b         = new Backbone.Model({id: 2, label: 'b'});
 12      c         = new Backbone.Model({id: 1, label: 'c'});
 13      d         = new Backbone.Model({id: 0, label: 'd'});
 14      e         = null;
 15      col       = new Backbone.Collection([a,b,c,d]);
 16      otherCol  = new Backbone.Collection();
 17    }
 18
 19  }));
 20
 21  test("new and sort", 9, function() {
 22    var counter = 0;
 23    col.on('sort', function(){ counter++; });
 24    equal(col.first(), a, "a should be first");
 25    equal(col.last(), d, "d should be last");
 26    col.comparator = function(a, b) {
 27      return a.id > b.id ? -1 : 1;
 28    };
 29    col.sort();
 30    equal(counter, 1);
 31    equal(col.first(), a, "a should be first");
 32    equal(col.last(), d, "d should be last");
 33    col.comparator = function(model) { return model.id; };
 34    col.sort();
 35    equal(counter, 2);
 36    equal(col.first(), d, "d should be first");
 37    equal(col.last(), a, "a should be last");
 38    equal(col.length, 4);
 39  });
 40
 41  test("String comparator.", 1, function() {
 42    var collection = new Backbone.Collection([
 43      {id: 3},
 44      {id: 1},
 45      {id: 2}
 46    ], {comparator: 'id'});
 47    deepEqual(collection.pluck('id'), [1, 2, 3]);
 48  });
 49
 50  test("new and parse", 3, function() {
 51    var Collection = Backbone.Collection.extend({
 52      parse : function(data) {
 53        return _.filter(data, function(datum) {
 54          return datum.a % 2 === 0;
 55        });
 56      }
 57    });
 58    var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
 59    var collection = new Collection(models, {parse: true});
 60    strictEqual(collection.length, 2);
 61    strictEqual(collection.first().get('a'), 2);
 62    strictEqual(collection.last().get('a'), 4);
 63  });
 64
 65  test("get", 5, function() {
 66    equal(col.get(0), d);
 67    equal(col.get(2), b);
 68    equal(col.get({id: 1}), c);
 69    equal(col.get(c.clone()), c);
 70    equal(col.get(col.first().cid), col.first());
 71  });
 72
 73  test("get with non-default ids", 4, function() {
 74    var col = new Backbone.Collection();
 75    var MongoModel = Backbone.Model.extend({
 76      idAttribute: '_id'
 77    });
 78    var model = new MongoModel({_id: 100});
 79    col.push(model);
 80    equal(col.get(100), model);
 81    model.set({_id: 101});
 82    equal(col.get(101), model);
 83
 84    var Col2 = Backbone.Collection.extend({ model: MongoModel });
 85    var col2 = new Col2();
 86    col2.push(model);
 87    equal(col2.get({_id: 101}), model);
 88    equal(col2.get(model.clone()), model);
 89  });
 90
 91  test("update index when id changes", 3, function() {
 92    var col = new Backbone.Collection();
 93    col.add([
 94      {id : 0, name : 'one'},
 95      {id : 1, name : 'two'}
 96    ]);
 97    var one = col.get(0);
 98    equal(one.get('name'), 'one');
 99    one.set({id : 101});
100    equal(col.get(0), null);
101    equal(col.get(101).get('name'), 'one');
102  });
103
104  test("at", 1, function() {
105    equal(col.at(2), c);
106  });
107
108  test("pluck", 1, function() {
109    equal(col.pluck('label').join(' '), 'a b c d');
110  });
111
112  test("add", 10, function() {
113    var added, opts, secondAdded;
114    added = opts = secondAdded = null;
115    e = new Backbone.Model({id: 10, label : 'e'});
116    otherCol.add(e);
117    otherCol.on('add', function() {
118      secondAdded = true;
119    });
120    col.on('add', function(model, collection, options){
121      added = model.get('label');
122      opts = options;
123    });
124    col.add(e, {amazing: true});
125    equal(added, 'e');
126    equal(col.length, 5);
127    equal(col.last(), e);
128    equal(otherCol.length, 1);
129    equal(secondAdded, null);
130    ok(opts.amazing);
131
132    var f = new Backbone.Model({id: 20, label : 'f'});
133    var g = new Backbone.Model({id: 21, label : 'g'});
134    var h = new Backbone.Model({id: 22, label : 'h'});
135    var atCol = new Backbone.Collection([f, g, h]);
136    equal(atCol.length, 3);
137    atCol.add(e, {at: 1});
138    equal(atCol.length, 4);
139    equal(atCol.at(1), e);
140    equal(atCol.last(), h);
141  });
142
143  test("add multiple models", 6, function() {
144    var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
145    col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
146    for (var i = 0; i <= 5; i++) {
147      equal(col.at(i).get('at'), i);
148    }
149  });
150
151  test("add; at should have preference over comparator", 1, function() {
152    var Col = Backbone.Collection.extend({
153      comparator: function(a,b) {
154        return a.id > b.id ? -1 : 1;
155      }
156    });
157
158    var col = new Col([{id: 2}, {id: 3}]);
159    col.add(new Backbone.Model({id: 1}), {at:   1});
160
161    equal(col.pluck('id').join(' '), '3 1 2');
162  });
163
164  test("can't add model to collection twice", function() {
165    var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
166    equal(col.pluck('id').join(' '), '1 2 3');
167  });
168
169  test("can't add different model with same id to collection twice", 1, function() {
170    var col = new Backbone.Collection;
171    col.unshift({id: 101});
172    col.add({id: 101});
173    equal(col.length, 1);
174  });
175
176  test("merge in duplicate models with {merge: true}", 3, function() {
177    var col = new Backbone.Collection;
178    col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]);
179    col.add({id: 1, name: 'Moses'});
180    equal(col.first().get('name'), 'Moe');
181    col.add({id: 1, name: 'Moses'}, {merge: true});
182    equal(col.first().get('name'), 'Moses');
183    col.add({id: 1, name: 'Tim'}, {merge: true, silent: true});
184    equal(col.first().get('name'), 'Tim');
185  });
186
187  test("add model to multiple collections", 10, function() {
188    var counter = 0;
189    var e = new Backbone.Model({id: 10, label : 'e'});
190    e.on('add', function(model, collection) {
191      counter++;
192      equal(e, model);
193      if (counter > 1) {
194        equal(collection, colF);
195      } else {
196        equal(collection, colE);
197      }
198    });
199    var colE = new Backbone.Collection([]);
200    colE.on('add', function(model, collection) {
201      equal(e, model);
202      equal(colE, collection);
203    });
204    var colF = new Backbone.Collection([]);
205    colF.on('add', function(model, collection) {
206      equal(e, model);
207      equal(colF, collection);
208    });
209    colE.add(e);
210    equal(e.collection, colE);
211    colF.add(e);
212    equal(e.collection, colE);
213  });
214
215  test("add model with parse", 1, function() {
216    var Model = Backbone.Model.extend({
217      parse: function(obj) {
218        obj.value += 1;
219        return obj;
220      }
221    });
222
223    var Col = Backbone.Collection.extend({model: Model});
224    var col = new Col;
225    col.add({value: 1}, {parse: true});
226    equal(col.at(0).get('value'), 2);
227  });
228
229  test("add model to collection with sort()-style comparator", 3, function() {
230    var col = new Backbone.Collection;
231    col.comparator = function(a, b) {
232      return a.get('name') < b.get('name') ? -1 : 1;
233    };
234    var tom = new Backbone.Model({name: 'Tom'});
235    var rob = new Backbone.Model({name: 'Rob'});
236    var tim = new Backbone.Model({name: 'Tim'});
237    col.add(tom);
238    col.add(rob);
239    col.add(tim);
240    equal(col.indexOf(rob), 0);
241    equal(col.indexOf(tim), 1);
242    equal(col.indexOf(tom), 2);
243  });
244
245  test("comparator that depends on `this`", 2, function() {
246    var col = new Backbone.Collection;
247    col.negative = function(num) {
248      return -num;
249    };
250    col.comparator = function(a) {
251      return this.negative(a.id);
252    };
253    col.add([{id: 1}, {id: 2}, {id: 3}]);
254    deepEqual(col.pluck('id'), [3, 2, 1]);
255    col.comparator = function(a, b) {
256      return this.negative(b.id) - this.negative(a.id);
257    };
258    col.sort();
259    deepEqual(col.pluck('id'), [1, 2, 3]);
260  });
261
262  test("remove", 5, function() {
263    var removed = null;
264    var otherRemoved = null;
265    col.on('remove', function(model, col, options) {
266      removed = model.get('label');
267      equal(options.index, 3);
268    });
269    otherCol.on('remove', function(model, col, options) {
270      otherRemoved = true;
271    });
272    col.remove(d);
273    equal(removed, 'd');
274    equal(col.length, 3);
275    equal(col.first(), a);
276    equal(otherRemoved, null);
277  });
278
279  test("shift and pop", 2, function() {
280    var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
281    equal(col.shift().get('a'), 'a');
282    equal(col.pop().get('c'), 'c');
283  });
284
285  test("slice", 2, function() {
286    var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
287    var array = col.slice(1, 3);
288    equal(array.length, 2);
289    equal(array[0].get('b'), 'b');
290  });
291
292  test("events are unbound on remove", 3, function() {
293    var counter = 0;
294    var dj = new Backbone.Model();
295    var emcees = new Backbone.Collection([dj]);
296    emcees.on('change', function(){ counter++; });
297    dj.set({name : 'Kool'});
298    equal(counter, 1);
299    emcees.reset([]);
300    equal(dj.collection, undefined);
301    dj.set({name : 'Shadow'});
302    equal(counter, 1);
303  });
304
305  test("remove in multiple collections", 7, function() {
306    var modelData = {
307      id : 5,
308      title : 'Othello'
309    };
310    var passed = false;
311    var e = new Backbone.Model(modelData);
312    var f = new Backbone.Model(modelData);
313    f.on('remove', function() {
314      passed = true;
315    });
316    var colE = new Backbone.Collection([e]);
317    var colF = new Backbone.Collection([f]);
318    ok(e != f);
319    ok(colE.length === 1);
320    ok(colF.length === 1);
321    colE.remove(e);
322    equal(passed, false);
323    ok(colE.length === 0);
324    colF.remove(e);
325    ok(colF.length === 0);
326    equal(passed, true);
327  });
328
329  test("remove same model in multiple collection", 16, function() {
330    var counter = 0;
331    var e = new Backbone.Model({id: 5, title: 'Othello'});
332    e.on('remove', function(model, collection) {
333      counter++;
334      equal(e, model);
335      if (counter > 1) {
336        equal(collection, colE);
337      } else {
338        equal(collection, colF);
339      }
340    });
341    var colE = new Backbone.Collection([e]);
342    colE.on('remove', function(model, collection) {
343      equal(e, model);
344      equal(colE, collection);
345    });
346    var colF = new Backbone.Collection([e]);
347    colF.on('remove', function(model, collection) {
348      equal(e, model);
349      equal(colF, collection);
350    });
351    equal(colE, e.collection);
352    colF.remove(e);
353    ok(colF.length === 0);
354    ok(colE.length === 1);
355    equal(counter, 1);
356    equal(colE, e.collection);
357    colE.remove(e);
358    equal(null, e.collection);
359    ok(colE.length === 0);
360    equal(counter, 2);
361  });
362
363  test("model destroy removes from all collections", 3, function() {
364    var e = new Backbone.Model({id: 5, title: 'Othello'});
365    e.sync = function(method, model, options) { options.success({}); };
366    var colE = new Backbone.Collection([e]);
367    var colF = new Backbone.Collection([e]);
368    e.destroy();
369    ok(colE.length === 0);
370    ok(colF.length === 0);
371    equal(undefined, e.collection);
372  });
373
374  test("Colllection: non-persisted model destroy removes from all collections", 3, function() {
375    var e = new Backbone.Model({title: 'Othello'});
376    e.sync = function(method, model, options) { throw "should not be called"; };
377    var colE = new Backbone.Collection([e]);
378    var colF = new Backbone.Collection([e]);
379    e.destroy();
380    ok(colE.length === 0);
381    ok(colF.length === 0);
382    equal(undefined, e.collection);
383  });
384
385  test("fetch", 4, function() {
386    var collection = new Backbone.Collection;
387    collection.url = '/test';
388    collection.fetch();
389    equal(this.syncArgs.method, 'read');
390    equal(this.syncArgs.model, collection);
391    equal(this.syncArgs.options.parse, true);
392
393    collection.fetch({parse: false});
394    equal(this.syncArgs.options.parse, false);
395  });
396
397  test("ensure fetch only parses once", 1, function() {
398    var collection = new Backbone.Collection;
399    var counter = 0;
400    collection.parse = function(models) {
401      counter++;
402      return models;
403    };
404    collection.url = '/test';
405    collection.fetch();
406    this.syncArgs.options.success([]);
407    equal(counter, 1);
408  });
409
410  test("create", 4, function() {
411    var collection = new Backbone.Collection;
412    collection.url = '/test';
413    var model = collection.create({label: 'f'}, {wait: true});
414    equal(this.syncArgs.method, 'create');
415    equal(this.syncArgs.model, model);
416    equal(model.get('label'), 'f');
417    equal(model.collection, collection);
418  });
419
420  test("create with validate:true enforces validation", 1, function() {
421    var ValidatingModel = Backbone.Model.extend({
422      validate: function(attrs) {
423        return "fail";
424      }
425    });
426    var ValidatingCollection = Backbone.Collection.extend({
427      model: ValidatingModel
428    });
429    var col = new ValidatingCollection();
430    equal(col.create({"foo":"bar"}, {validate:true}), false);
431  });
432
433  test("a failing create returns model with errors", function() {
434    var ValidatingModel = Backbone.Model.extend({
435      validate: function(attrs) {
436        return "fail";
437      }
438    });
439    var ValidatingCollection = Backbone.Collection.extend({
440      model: ValidatingModel
441    });
442    var col = new ValidatingCollection();
443    var m = col.create({"foo":"bar"});
444    equal(m.validationError, 'fail');
445    equal(col.length, 1);
446  });
447
448  test("initialize", 1, function() {
449    var Collection = Backbone.Collection.extend({
450      initialize: function() {
451        this.one = 1;
452      }
453    });
454    var coll = new Collection;
455    equal(coll.one, 1);
456  });
457
458  test("toJSON", 1, function() {
459    equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
460  });
461
462  test("where", 6, function() {
463    var coll = new Backbone.Collection([
464      {a: 1},
465      {a: 1},
466      {a: 1, b: 2},
467      {a: 2, b: 2},
468      {a: 3}
469    ]);
470    equal(coll.where({a: 1}).length, 3);
471    equal(coll.where({a: 2}).length, 1);
472    equal(coll.where({a: 3}).length, 1);
473    equal(coll.where({b: 1}).length, 0);
474    equal(coll.where({b: 2}).length, 2);
475    equal(coll.where({a: 1, b: 2}).length, 1);
476  });
477
478  test("Underscore methods", 13, function() {
479    equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
480    equal(col.any(function(model){ return model.id === 100; }), false);
481    equal(col.any(function(model){ return model.id === 0; }), true);
482    equal(col.indexOf(b), 1);
483    equal(col.size(), 4);
484    equal(col.rest().length, 3);
485    ok(!_.include(col.rest()), a);
486    ok(!_.include(col.rest()), d);
487    ok(!col.isEmpty());
488    ok(!_.include(col.without(d)), d);
489    equal(col.max(function(model){ return model.id; }).id, 3);
490    equal(col.min(function(model){ return model.id; }).id, 0);
491    deepEqual(col.chain()
492            .filter(function(o){ return o.id % 2 === 0; })
493            .map(function(o){ return o.id * 2; })
494            .value(),
495         [4, 0]);
496  });
497
498  test("reset", 10, function() {
499    var resetCount = 0;
500    var models = col.models;
501    col.on('reset', function() { resetCount += 1; });
502    col.reset([]);
503    equal(resetCount, 1);
504    equal(col.length, 0);
505    equal(col.last(), null);
506    col.reset(models);
507    equal(resetCount, 2);
508    equal(col.length, 4);
509    equal(col.last(), d);
510    col.reset(_.map(models, function(m){ return m.attributes; }));
511    equal(resetCount, 3);
512    equal(col.length, 4);
513    ok(col.last() !== d);
514    ok(_.isEqual(col.last().attributes, d.attributes));
515  });
516
517  test("reset passes caller options", 3, function() {
518    var Model = Backbone.Model.extend({
519      initialize: function(attrs, options) {
520        this.model_parameter = options.model_parameter;
521      }
522    });
523    var col = new (Backbone.Collection.extend({ model: Model }))();
524    col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 }], { model_parameter: 'model parameter' });
525    equal(col.length, 2);
526    col.each(function(model) {
527      equal(model.model_parameter, 'model parameter');
528    });
529  });
530
531  test("trigger custom events on models", 1, function() {
532    var fired = null;
533    a.on("custom", function() { fired = true; });
534    a.trigger("custom");
535    equal(fired, true);
536  });
537
538  test("add does not alter arguments", 2, function(){
539    var attrs = {};
540    var models = [attrs];
541    new Backbone.Collection().add(models);
542    equal(models.length, 1);
543    ok(attrs === models[0]);
544  });
545
546  test("#714: access `model.collection` in a brand new model.", 2, function() {
547    var collection = new Backbone.Collection;
548    collection.url = '/test';
549    var Model = Backbone.Model.extend({
550      set: function(attrs) {
551        equal(attrs.prop, 'value');
552        equal(this.collection, collection);
553        return this;
554      }
555    });
556    collection.model = Model;
557    collection.create({prop: 'value'});
558  });
559
560  test("#574, remove its own reference to the .models array.", 2, function() {
561    var col = new Backbone.Collection([
562      {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
563    ]);
564    equal(col.length, 6);
565    col.remove(col.models);
566    equal(col.length, 0);
567  });
568
569  test("#861, adding models to a collection which do not pass validation, with validate:true", function() {
570      var Model = Backbone.Model.extend({
571        validate: function(attrs) {
572          if (attrs.id == 3) return "id can't be 3";
573        }
574      });
575
576      var Collection = Backbone.Collection.extend({
577        model: Model
578      });
579
580      var collection = new Collection;
581      collection.on("error", function() { ok(true); });
582
583      collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {validate:true});
584      deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
585  });
586
587  test("Invalid models are discarded with validate:true.", 5, function() {
588    var collection = new Backbone.Collection;
589    collection.on('test', function() { ok(true); });
590    collection.model = Backbone.Model.extend({
591      validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
592    });
593    var model = new collection.model({id: 1, valid: true});
594    collection.add([model, {id: 2}], {validate:true});
595    model.trigger('test');
596    ok(collection.get(model.cid));
597    ok(collection.get(1));
598    ok(!collection.get(2));
599    equal(collection.length, 1);
600  });
601
602  test("multiple copies of the same model", 3, function() {
603    var col = new Backbone.Collection();
604    var model = new Backbone.Model();
605    col.add([model, model]);
606    equal(col.length, 1);
607    col.add([{id: 1}, {id: 1}]);
608    equal(col.length, 2);
609    equal(col.last().id, 1);
610  });
611
612  test("#964 - collection.get return inconsistent", 2, function() {
613    var c = new Backbone.Collection();
614    ok(c.get(null) === undefined);
615    ok(c.get() === undefined);
616  });
617
618  test("#1112 - passing options.model sets collection.model", 2, function() {
619    var Model = Backbone.Model.extend({});
620    var c = new Backbone.Collection([{id: 1}], {model: Model});
621    ok(c.model === Model);
622    ok(c.at(0) instanceof Model);
623  });
624
625  test("null and undefined are invalid ids.", 2, function() {
626    var model = new Backbone.Model({id: 1});
627    var collection = new Backbone.Collection([model]);
628    model.set({id: null});
629    ok(!collection.get('null'));
630    model.set({id: 1});
631    model.set({id: undefined});
632    ok(!collection.get('undefined'));
633  });
634
635  test("falsy comparator", 4, function(){
636    var Col = Backbone.Collection.extend({
637      comparator: function(model){ return model.id; }
638    });
639    var col = new Col();
640    var colFalse = new Col(null, {comparator: false});
641    var colNull = new Col(null, {comparator: null});
642    var colUndefined = new Col(null, {comparator: undefined});
643    ok(col.comparator);
644    ok(!colFalse.comparator);
645    ok(!colNull.comparator);
646    ok(colUndefined.comparator);
647  });
648
649  test("#1355 - `options` is passed to success callbacks", 2, function(){
650    var m = new Backbone.Model({x:1});
651    var col = new Backbone.Collection();
652    var opts = {
653      success: function(collection, resp, options){
654        ok(options);
655      }
656    };
657    col.sync = m.sync = function( method, collection, options ){
658      options.success();
659    };
660    col.fetch(opts);
661    col.create(m, opts);
662  });
663
664  test("#1412 - Trigger 'sync' event.", 2, function() {
665    var collection = new Backbone.Collection;
666    collection.url = '/test';
667    collection.on('sync', function() { ok(true); });
668    Backbone.ajax = function(settings){ settings.success(); };
669    collection.fetch();
670    collection.create({id: 1});
671  });
672
673  test("#1447 - create with wait adds model.", 1, function() {
674    var collection = new Backbone.Collection;
675    var model = new Backbone.Model;
676    model.sync = function(method, model, options){ options.success(); };
677    collection.on('add', function(){ ok(true); });
678    collection.create(model, {wait: true});
679  });
680
681  test("#1448 - add sorts collection after merge.", 1, function() {
682    var collection = new Backbone.Collection([
683      {id: 1, x: 1},
684      {id: 2, x: 2}
685    ]);
686    collection.comparator = function(model){ return model.get('x'); };
687    collection.add({id: 1, x: 3}, {merge: true});
688    deepEqual(collection.pluck('id'), [2, 1]);
689  });
690
691  test("#1655 - groupBy can be used with a string argument.", 3, function() {
692    var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
693    var grouped = collection.groupBy('x');
694    strictEqual(_.keys(grouped).length, 2);
695    strictEqual(grouped[1][0].get('x'), 1);
696    strictEqual(grouped[2][0].get('x'), 2);
697  });
698
699  test("#1655 - sortBy can be used with a string argument.", 1, function() {
700    var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
701    var values = _.map(collection.sortBy('x'), function(model) {
702      return model.get('x');
703    });
704    deepEqual(values, [1, 2, 3]);
705  });
706
707  test("#1604 - Removal during iteration.", 0, function() {
708    var collection = new Backbone.Collection([{}, {}]);
709    collection.on('add', function() {
710      collection.at(0).destroy();
711    });
712    collection.add({}, {at: 0});
713  });
714
715  test("#1638 - `sort` during `add` triggers correctly.", function() {
716    var collection = new Backbone.Collection;
717    collection.comparator = function(model) { return model.get('x'); };
718    var added = [];
719    collection.on('add', function(model) {
720      model.set({x: 3});
721      collection.sort();
722      added.push(model.id);
723    });
724    collection.add([{id: 1, x: 1}, {id: 2, x: 2}]);
725    deepEqual(added, [1, 2]);
726  });
727
728  test("fetch parses models by default", 1, function() {
729    var model = {};
730    var Collection = Backbone.Collection.extend({
731      url: 'test',
732      model: Backbone.Model.extend({
733        parse: function(resp) {
734          strictEqual(resp, model);
735        }
736      })
737    });
738    new Collection().fetch();
739    this.ajaxSettings.success([model]);
740  });
741
742  test("`sort` shouldn't always fire on `add`", 1, function() {
743    var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
744      comparator: 'id'
745    });
746    c.sort = function(){ ok(true); };
747    c.add([]);
748    c.add({id: 1});
749    c.add([{id: 2}, {id: 3}]);
750    c.add({id: 4});
751  });
752
753  test("#1407 parse option on constructor parses collection and models", 2, function() {
754    var model = {
755      namespace : [{id: 1}, {id:2}]
756    };
757    var Collection = Backbone.Collection.extend({
758      model: Backbone.Model.extend({
759        parse: function(model) {
760          model.name = 'test';
761          return model;
762        }
763      }),
764      parse: function(model) {
765        return model.namespace;
766      }
767    });
768    var c = new Collection(model, {parse:true});
769
770    equal(c.length, 2);
771    equal(c.at(0).get('name'), 'test');
772  });
773
774  test("#1407 parse option on reset parses collection and models", 2, function() {
775    var model = {
776      namespace : [{id: 1}, {id:2}]
777    };
778    var Collection = Backbone.Collection.extend({
779      model: Backbone.Model.extend({
780        parse: function(model) {
781          model.name = 'test';
782          return model;
783        }
784      }),
785      parse: function(model) {
786        return model.namespace;
787      }
788    });
789    var c = new Collection();
790        c.reset(model, {parse:true});
791
792    equal(c.length, 2);
793    equal(c.at(0).get('name'), 'test');
794  });
795
796
797  test("Reset includes previous models in triggered event.", 1, function() {
798    var model = new Backbone.Model();
799    var collection = new Backbone.Collection([model])
800    .on('reset', function(collection, options) {
801      deepEqual(options.previousModels, [model]);
802    });
803    collection.reset([]);
804  });
805
806  test("update", function() {
807    var m1 = new Backbone.Model();
808    var m2 = new Backbone.Model({id: 2});
809    var m3 = new Backbone.Model();
810    var c = new Backbone.Collection([m1, m2]);
811
812    // Test add/change/remove events
813    c.on('add', function(model) {
814      strictEqual(model, m3);
815    });
816    c.on('change', function(model) {
817      strictEqual(model, m2);
818    });
819    c.on('remove', function(model) {
820      strictEqual(model, m1);
821    });
822
823    // remove: false doesn't remove any models
824    c.update([], {remove: false});
825    strictEqual(c.length, 2);
826
827    // add: false doesn't add any models
828    c.update([m1, m2, m3], {add: false});
829    strictEqual(c.length, 2);
830
831    // merge: false doesn't change any models
832    c.update([m1, {id: 2, a: 1}], {merge: false});
833    strictEqual(m2.get('a'), void 0);
834
835    // add: false, remove: false only merges existing models
836    c.update([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false});
837    strictEqual(c.length, 2);
838    strictEqual(m2.get('a'), 0);
839
840    // default options add/remove/merge as appropriate
841    c.update([{id: 2, a: 1}, m3]);
842    strictEqual(c.length, 2);
843    strictEqual(m2.get('a'), 1);
844
845    // Test removing models not passing an argument
846    c.off('remove').on('remove', function(model) {
847      ok(model === m2 || model === m3);
848    });
849    c.update([]);
850    strictEqual(c.length, 0);
851  });
852
853  test("update with only cids", 3, function() {
854    var m1 = new Backbone.Model;
855    var m2 = new Backbone.Model;
856    var c = new Backbone.Collection;
857    c.update([m1, m2]);
858    equal(c.length, 2);
859    c.update([m1]);
860    equal(c.length, 1);
861    c.update([m1, m1, m1, m2, m2], {remove: false});
862    equal(c.length, 2);
863  });
864
865  test("update with only idAttribute", 3, function() {
866    var m1 = { _id: 1 };
867    var m2 = { _id: 2 };
868    var col = Backbone.Collection.extend({
869      model: Backbone.Model.extend({
870        idAttribute: '_id'
871      })
872    });
873    var c = new col;
874    c.update([m1, m2]);
875    equal(c.length, 2);
876    c.update([m1]);
877    equal(c.length, 1);
878    c.update([m1, m1, m1, m2, m2], {remove: false});
879    equal(c.length, 2);
880  });
881
882  test("update + merge with default values defined", function() {
883    var Model = Backbone.Model.extend({
884      defaults: {
885        key: 'value'
886      }
887    });
888    var m = new Model({id: 1});
889    var col = new Backbone.Collection([m], {model: Model});
890    equal(col.first().get('key'), 'value');
891
892    col.update({id: 1, key: 'other'});
893    equal(col.first().get('key'), 'other');
894
895    col.update({id: 1, other: 'value'});
896    equal(col.first().get('key'), 'other');
897    equal(col.length, 1);
898  });
899
900  test("#1894 - Push should not trigger a sort", 0, function() {
901    var Collection = Backbone.Collection.extend({
902      comparator: 'id',
903      sort: function() {
904        ok(false);
905      }
906    });
907    new Collection().push({id: 1});
908  });
909
910  test("`update` with non-normal id", function() {
911    var Collection = Backbone.Collection.extend({
912      model: Backbone.Model.extend({idAttribute: '_id'})
913    });
914    var collection = new Collection({_id: 1});
915    collection.update([{_id: 1, a: 1}], {add: false});
916    equal(collection.first().get('a'), 1);
917  });
918
919  test("#1894 - `sort` can optionally be turned off", 0, function() {
920    var Collection = Backbone.Collection.extend({
921      comparator: 'id',
922      sort: function() { ok(true); }
923    });
924    new Collection().add({id: 1}, {sort: false});
925  });
926
927  test("#1915 - `parse` data in the right order in `update`", function() {
928    var collection = new (Backbone.Collection.extend({
929      parse: function (data) {
930        strictEqual(data.status, 'ok');
931        return data.data;
932      }
933    }));
934    var res = {status: 'ok', data:[{id: 1}]};
935    collection.update(res, {parse: true});
936  });
937
938  asyncTest("#1939 - `parse` is passed `options`", 1, function () {
939    var collection = new (Backbone.Collection.extend({
940      url: '/',
941      parse: function (data, options) {
942        strictEqual(options.xhr.someHeader, 'headerValue');
943        return data;
944      }
945    }));
946    var ajax = Backbone.ajax;
947    Backbone.ajax = function (params) {
948      _.defer(params.success);
949      return {someHeader: 'headerValue'};
950    };
951    collection.fetch({
952      success: function () { start(); }
953    });
954    Backbone.ajax = ajax;
955  });
956
957  test("`sort` is trigged on `add` when sorting occurs", 2, function () {
958    var collection = new (Backbone.Collection.extend({
959      comparator: 'id'
960    }))([{id: 1}, {id: 2}, {id: 3}]);
961    collection.on('sort', function () { ok(true); });
962    collection.add({id: 4}); // trigger
963    collection.add({id: 1}, {merge: true}); // trigger
964    collection.add({id: 1}); // don't trigger
965    collection.add({id: 5}, {silent: true}); // don't trigger
966  });
967
968});