PageRenderTime 1130ms CodeModel.GetById 201ms app.highlight 682ms RepoModel.GetById 234ms app.codeStats 1ms

/test/collection.js

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