PageRenderTime 838ms CodeModel.GetById 181ms app.highlight 473ms RepoModel.GetById 171ms app.codeStats 1ms

/vendor/assets/components/backbone/test/collection.js

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