PageRenderTime 102ms CodeModel.GetById 2ms app.highlight 84ms RepoModel.GetById 1ms app.codeStats 1ms

/test/collection.js

https://github.com/NickGardner/backbone
JavaScript | 782 lines | 705 code | 77 blank | 0 comment | 20 complexity | a6fb3f4ba757b273d028e7dff720f625 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", 7, function() {
 22    equal(col.first(), a, "a should be first");
 23    equal(col.last(), d, "d should be last");
 24    col.comparator = function(a, b) {
 25      return a.id > b.id ? -1 : 1;
 26    };
 27    col.sort();
 28    equal(col.first(), a, "a should be first");
 29    equal(col.last(), d, "d should be last");
 30    col.comparator = function(model) { return model.id; };
 31    col.sort();
 32    equal(col.first(), d, "d should be first");
 33    equal(col.last(), a, "a should be last");
 34    equal(col.length, 4);
 35  });
 36
 37  test("String comparator.", 1, function() {
 38    var collection = new Backbone.Collection([
 39      {id: 3},
 40      {id: 1},
 41      {id: 2}
 42    ], {comparator: 'id'});
 43    deepEqual(collection.pluck('id'), [1, 2, 3]);
 44  });
 45
 46  test("new and parse", 3, function() {
 47    var Collection = Backbone.Collection.extend({
 48      parse : function(data) {
 49        return _.filter(data, function(datum) {
 50          return datum.a % 2 === 0;
 51        });
 52      }
 53    });
 54    var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
 55    var collection = new Collection(models, {parse: true});
 56    strictEqual(collection.length, 2);
 57    strictEqual(collection.first().get('a'), 2);
 58    strictEqual(collection.last().get('a'), 4);
 59  });
 60
 61  test("get, getByCid", 3, function() {
 62    equal(col.get(0), d);
 63    equal(col.get(2), b);
 64    equal(col.getByCid(col.first().cid), col.first());
 65  });
 66
 67  test("get with non-default ids", 2, function() {
 68    var col = new Backbone.Collection();
 69    var MongoModel = Backbone.Model.extend({
 70      idAttribute: '_id'
 71    });
 72    var model = new MongoModel({_id: 100});
 73    col.push(model);
 74    equal(col.get(100), model);
 75    model.set({_id: 101});
 76    equal(col.get(101), model);
 77  });
 78
 79  test("update index when id changes", 3, function() {
 80    var col = new Backbone.Collection();
 81    col.add([
 82      {id : 0, name : 'one'},
 83      {id : 1, name : 'two'}
 84    ]);
 85    var one = col.get(0);
 86    equal(one.get('name'), 'one');
 87    one.set({id : 101});
 88    equal(col.get(0), null);
 89    equal(col.get(101).get('name'), 'one');
 90  });
 91
 92  test("at", 1, function() {
 93    equal(col.at(2), c);
 94  });
 95
 96  test("pluck", 1, function() {
 97    equal(col.pluck('label').join(' '), 'a b c d');
 98  });
 99
100  test("add", 10, function() {
101    var added, opts, secondAdded;
102    added = opts = secondAdded = null;
103    e = new Backbone.Model({id: 10, label : 'e'});
104    otherCol.add(e);
105    otherCol.on('add', function() {
106      secondAdded = true;
107    });
108    col.on('add', function(model, collection, options){
109      added = model.get('label');
110      opts = options;
111    });
112    col.add(e, {amazing: true});
113    equal(added, 'e');
114    equal(col.length, 5);
115    equal(col.last(), e);
116    equal(otherCol.length, 1);
117    equal(secondAdded, null);
118    ok(opts.amazing);
119
120    var f = new Backbone.Model({id: 20, label : 'f'});
121    var g = new Backbone.Model({id: 21, label : 'g'});
122    var h = new Backbone.Model({id: 22, label : 'h'});
123    var atCol = new Backbone.Collection([f, g, h]);
124    equal(atCol.length, 3);
125    atCol.add(e, {at: 1});
126    equal(atCol.length, 4);
127    equal(atCol.at(1), e);
128    equal(atCol.last(), h);
129  });
130
131  test("add multiple models", 6, function() {
132    var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
133    col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
134    for (var i = 0; i <= 5; i++) {
135      equal(col.at(i).get('at'), i);
136    }
137  });
138
139  test("add; at should have preference over comparator", 1, function() {
140    var Col = Backbone.Collection.extend({
141      comparator: function(a,b) {
142        return a.id > b.id ? -1 : 1;
143      }
144    });
145
146    var col = new Col([{id: 2}, {id: 3}]);
147    col.add(new Backbone.Model({id: 1}), {at:   1});
148
149    equal(col.pluck('id').join(' '), '3 1 2');
150  });
151
152  test("can't add model to collection twice", function() {
153    var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
154    equal(col.pluck('id').join(' '), '1 2 3');
155  });
156
157  test("can't add different model with same id to collection twice", 1, function() {
158    var col = new Backbone.Collection;
159    col.unshift({id: 101});
160    col.add({id: 101});
161    equal(col.length, 1);
162  });
163
164  test("merge in duplicate models with {merge: true}", 3, function() {
165    var col = new Backbone.Collection;
166    col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]);
167    col.add({id: 1, name: 'Moses'});
168    equal(col.first().get('name'), 'Moe');
169    col.add({id: 1, name: 'Moses'}, {merge: true});
170    equal(col.first().get('name'), 'Moses');
171    col.add({id: 1, name: 'Tim'}, {merge: true, silent: true});
172    equal(col.first().get('name'), 'Tim');
173  });
174
175  test("add model to multiple collections", 10, function() {
176    var counter = 0;
177    var e = new Backbone.Model({id: 10, label : 'e'});
178    e.on('add', function(model, collection) {
179      counter++;
180      equal(e, model);
181      if (counter > 1) {
182        equal(collection, colF);
183      } else {
184        equal(collection, colE);
185      }
186    });
187    var colE = new Backbone.Collection([]);
188    colE.on('add', function(model, collection) {
189      equal(e, model);
190      equal(colE, collection);
191    });
192    var colF = new Backbone.Collection([]);
193    colF.on('add', function(model, collection) {
194      equal(e, model);
195      equal(colF, collection);
196    });
197    colE.add(e);
198    equal(e.collection, colE);
199    colF.add(e);
200    equal(e.collection, colE);
201  });
202
203  test("add model with parse", 1, function() {
204    var Model = Backbone.Model.extend({
205      parse: function(obj) {
206        obj.value += 1;
207        return obj;
208      }
209    });
210
211    var Col = Backbone.Collection.extend({model: Model});
212    var col = new Col;
213    col.add({value: 1}, {parse: true});
214    equal(col.at(0).get('value'), 2);
215  });
216
217  test("add model to collection with sort()-style comparator", 3, function() {
218    var col = new Backbone.Collection;
219    col.comparator = function(a, b) {
220      return a.get('name') < b.get('name') ? -1 : 1;
221    };
222    var tom = new Backbone.Model({name: 'Tom'});
223    var rob = new Backbone.Model({name: 'Rob'});
224    var tim = new Backbone.Model({name: 'Tim'});
225    col.add(tom);
226    col.add(rob);
227    col.add(tim);
228    equal(col.indexOf(rob), 0);
229    equal(col.indexOf(tim), 1);
230    equal(col.indexOf(tom), 2);
231  });
232
233  test("comparator that depends on `this`", 2, function() {
234    var col = new Backbone.Collection;
235    col.negative = function(num) {
236      return -num;
237    };
238    col.comparator = function(a) {
239      return this.negative(a.id);
240    };
241    col.add([{id: 1}, {id: 2}, {id: 3}]);
242    deepEqual(col.pluck('id'), [3, 2, 1]);
243    col.comparator = function(a, b) {
244      return this.negative(b.id) - this.negative(a.id);
245    };
246    col.sort();
247    deepEqual(col.pluck('id'), [1, 2, 3]);
248  });
249
250  test("remove", 5, function() {
251    var removed = null;
252    var otherRemoved = null;
253    col.on('remove', function(model, col, options) {
254      removed = model.get('label');
255      equal(options.index, 3);
256    });
257    otherCol.on('remove', function(model, col, options) {
258      otherRemoved = true;
259    });
260    col.remove(d);
261    equal(removed, 'd');
262    equal(col.length, 3);
263    equal(col.first(), a);
264    equal(otherRemoved, null);
265  });
266
267  test("shift and pop", 2, function() {
268    var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
269    equal(col.shift().get('a'), 'a');
270    equal(col.pop().get('c'), 'c');
271  });
272
273  test("slice", 2, function() {
274    var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
275    var array = col.slice(1, 3);
276    equal(array.length, 2);
277    equal(array[0].get('b'), 'b');
278  });
279
280  test("events are unbound on remove", 3, function() {
281    var counter = 0;
282    var dj = new Backbone.Model();
283    var emcees = new Backbone.Collection([dj]);
284    emcees.on('change', function(){ counter++; });
285    dj.set({name : 'Kool'});
286    equal(counter, 1);
287    emcees.reset([]);
288    equal(dj.collection, undefined);
289    dj.set({name : 'Shadow'});
290    equal(counter, 1);
291  });
292
293  test("remove in multiple collections", 7, function() {
294    var modelData = {
295      id : 5,
296      title : 'Othello'
297    };
298    var passed = false;
299    var e = new Backbone.Model(modelData);
300    var f = new Backbone.Model(modelData);
301    f.on('remove', function() {
302      passed = true;
303    });
304    var colE = new Backbone.Collection([e]);
305    var colF = new Backbone.Collection([f]);
306    ok(e != f);
307    ok(colE.length == 1);
308    ok(colF.length == 1);
309    colE.remove(e);
310    equal(passed, false);
311    ok(colE.length == 0);
312    colF.remove(e);
313    ok(colF.length == 0);
314    equal(passed, true);
315  });
316
317  test("remove same model in multiple collection", 16, function() {
318    var counter = 0;
319    var e = new Backbone.Model({id: 5, title: 'Othello'});
320    e.on('remove', function(model, collection) {
321      counter++;
322      equal(e, model);
323      if (counter > 1) {
324        equal(collection, colE);
325      } else {
326        equal(collection, colF);
327      }
328    });
329    var colE = new Backbone.Collection([e]);
330    colE.on('remove', function(model, collection) {
331      equal(e, model);
332      equal(colE, collection);
333    });
334    var colF = new Backbone.Collection([e]);
335    colF.on('remove', function(model, collection) {
336      equal(e, model);
337      equal(colF, collection);
338    });
339    equal(colE, e.collection);
340    colF.remove(e);
341    ok(colF.length == 0);
342    ok(colE.length == 1);
343    equal(counter, 1);
344    equal(colE, e.collection);
345    colE.remove(e);
346    equal(null, e.collection);
347    ok(colE.length == 0);
348    equal(counter, 2);
349  });
350
351  test("model destroy removes from all collections", 3, function() {
352    var e = new Backbone.Model({id: 5, title: 'Othello'});
353    e.sync = function(method, model, options) { options.success({}); };
354    var colE = new Backbone.Collection([e]);
355    var colF = new Backbone.Collection([e]);
356    e.destroy();
357    ok(colE.length == 0);
358    ok(colF.length == 0);
359    equal(undefined, e.collection);
360  });
361
362  test("Colllection: non-persisted model destroy removes from all collections", 3, function() {
363    var e = new Backbone.Model({title: 'Othello'});
364    e.sync = function(method, model, options) { throw "should not be called"; };
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("fetch", 4, function() {
374    var collection = new Backbone.Collection;
375    collection.url = '/test';
376    collection.fetch();
377    equal(this.syncArgs.method, 'read');
378    equal(this.syncArgs.model, collection);
379    equal(this.syncArgs.options.parse, true);
380
381    collection.fetch({parse: false});
382    equal(this.syncArgs.options.parse, false);
383  });
384
385  test("create", 4, function() {
386    var collection = new Backbone.Collection;
387    collection.url = '/test';
388    var model = collection.create({label: 'f'}, {wait: true});
389    equal(this.syncArgs.method, 'create');
390    equal(this.syncArgs.model, model);
391    equal(model.get('label'), 'f');
392    equal(model.collection, collection);
393  });
394
395  test("create enforces validation", 1, function() {
396    var ValidatingModel = Backbone.Model.extend({
397      validate: function(attrs) {
398        return "fail";
399      }
400    });
401    var ValidatingCollection = Backbone.Collection.extend({
402      model: ValidatingModel
403    });
404    var col = new ValidatingCollection();
405    equal(col.create({"foo":"bar"}), false);
406  });
407
408  test("a failing create runs the error callback", 1, function() {
409    var ValidatingModel = Backbone.Model.extend({
410      validate: function(attrs) {
411        return "fail";
412      }
413    });
414    var ValidatingCollection = Backbone.Collection.extend({
415      model: ValidatingModel
416    });
417    var flag = false;
418    var callback = function(model, error) { flag = true; };
419    var col = new ValidatingCollection();
420    col.create({"foo":"bar"}, { error: callback });
421    equal(flag, true);
422  });
423
424  test("initialize", 1, function() {
425    var Collection = Backbone.Collection.extend({
426      initialize: function() {
427        this.one = 1;
428      }
429    });
430    var coll = new Collection;
431    equal(coll.one, 1);
432  });
433
434  test("toJSON", 1, function() {
435    equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
436  });
437
438  test("where", 6, function() {
439    var coll = new Backbone.Collection([
440      {a: 1},
441      {a: 1},
442      {a: 1, b: 2},
443      {a: 2, b: 2},
444      {a: 3}
445    ]);
446    equal(coll.where({a: 1}).length, 3);
447    equal(coll.where({a: 2}).length, 1);
448    equal(coll.where({a: 3}).length, 1);
449    equal(coll.where({b: 1}).length, 0);
450    equal(coll.where({b: 2}).length, 2);
451    equal(coll.where({a: 1, b: 2}).length, 1);
452  });
453
454  test("Underscore methods", 13, function() {
455    equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
456    equal(col.any(function(model){ return model.id === 100; }), false);
457    equal(col.any(function(model){ return model.id === 0; }), true);
458    equal(col.indexOf(b), 1);
459    equal(col.size(), 4);
460    equal(col.rest().length, 3);
461    ok(!_.include(col.rest()), a);
462    ok(!_.include(col.rest()), d);
463    ok(!col.isEmpty());
464    ok(!_.include(col.without(d)), d);
465    equal(col.max(function(model){ return model.id; }).id, 3);
466    equal(col.min(function(model){ return model.id; }).id, 0);
467    deepEqual(col.chain()
468            .filter(function(o){ return o.id % 2 === 0; })
469            .map(function(o){ return o.id * 2; })
470            .value(),
471         [4, 0]);
472  });
473
474  test("reset", 10, function() {
475    var resetCount = 0;
476    var models = col.models;
477    col.on('reset', function() { resetCount += 1; });
478    col.reset([]);
479    equal(resetCount, 1);
480    equal(col.length, 0);
481    equal(col.last(), null);
482    col.reset(models);
483    equal(resetCount, 2);
484    equal(col.length, 4);
485    equal(col.last(), d);
486    col.reset(_.map(models, function(m){ return m.attributes; }));
487    equal(resetCount, 3);
488    equal(col.length, 4);
489    ok(col.last() !== d);
490    ok(_.isEqual(col.last().attributes, d.attributes));
491  });
492
493  test("reset passes caller options", 3, function() {
494    var Model = Backbone.Model.extend({
495      initialize: function(attrs, options) {
496        this.model_parameter = options.model_parameter;
497      }
498    });
499    var col = new (Backbone.Collection.extend({ model: Model }))();
500    col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 }], { model_parameter: 'model parameter' });
501    equal(col.length, 2);
502    col.each(function(model) {
503      equal(model.model_parameter, 'model parameter');
504    });
505  });
506
507  test("trigger custom events on models", 1, function() {
508    var fired = null;
509    a.on("custom", function() { fired = true; });
510    a.trigger("custom");
511    equal(fired, true);
512  });
513
514  test("add does not alter arguments", 2, function(){
515    var attrs = {};
516    var models = [attrs];
517    new Backbone.Collection().add(models);
518    equal(models.length, 1);
519    ok(attrs === models[0]);
520  });
521
522  test("#714: access `model.collection` in a brand new model.", 2, function() {
523    var collection = new Backbone.Collection;
524    collection.url = '/test';
525    var Model = Backbone.Model.extend({
526      set: function(attrs) {
527        equal(attrs.prop, 'value');
528        equal(this.collection, collection);
529        return this;
530      }
531    });
532    collection.model = Model;
533    collection.create({prop: 'value'});
534  });
535
536  test("#574, remove its own reference to the .models array.", 2, function() {
537    var col = new Backbone.Collection([
538      {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
539    ]);
540    equal(col.length, 6);
541    col.remove(col.models);
542    equal(col.length, 0);
543  });
544
545  test("#861, adding models to a collection which do not pass validation", function() {
546      var Model = Backbone.Model.extend({
547        validate: function(attrs) {
548          if (attrs.id == 3) return "id can't be 3";
549        }
550      });
551
552      var Collection = Backbone.Collection.extend({
553        model: Model
554      });
555
556      var collection = new Collection;
557      collection.on("error", function() { ok(true); });
558
559      collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
560      deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
561  });
562
563  test("Invalid models are discarded.", 5, function() {
564    var collection = new Backbone.Collection;
565    collection.on('test', function() { ok(true); });
566    collection.model = Backbone.Model.extend({
567      validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
568    });
569    var model = new collection.model({id: 1, valid: true});
570    collection.add([model, {id: 2}]);;
571    model.trigger('test');
572    ok(collection.getByCid(model.cid));
573    ok(collection.get(1));
574    ok(!collection.get(2));
575    equal(collection.length, 1);
576  });
577
578  test("multiple copies of the same model", 3, function() {
579    var col = new Backbone.Collection();
580    var model = new Backbone.Model();
581    col.add([model, model]);
582    equal(col.length, 1);
583    col.add([{id: 1}, {id: 1}]);
584    equal(col.length, 2);
585    equal(col.last().id, 1);
586  });
587
588  test("#964 - collection.get return inconsistent", 2, function() {
589    var c = new Backbone.Collection();
590    ok(c.get(null) === undefined);
591    ok(c.get() === undefined);
592  });
593
594  test("#1112 - passing options.model sets collection.model", 2, function() {
595    var Model = Backbone.Model.extend({});
596    var c = new Backbone.Collection([{id: 1}], {model: Model});
597    ok(c.model === Model);
598    ok(c.at(0) instanceof Model);
599  });
600
601  test("null and undefined are invalid ids.", 2, function() {
602    var model = new Backbone.Model({id: 1});
603    var collection = new Backbone.Collection([model]);
604    model.set({id: null});
605    ok(!collection.get('null'));
606    model.set({id: 1});
607    model.set({id: undefined});
608    ok(!collection.get('undefined'));
609  });
610
611  test("falsy comparator", 4, function(){
612    var Col = Backbone.Collection.extend({
613      comparator: function(model){ return model.id; }
614    });
615    var col = new Col();
616    var colFalse = new Col(null, {comparator: false});
617    var colNull = new Col(null, {comparator: null});
618    var colUndefined = new Col(null, {comparator: undefined});
619    ok(col.comparator);
620    ok(!colFalse.comparator);
621    ok(!colNull.comparator);
622    ok(colUndefined.comparator);
623  });
624
625  test("#1355 - `options` is passed to success callbacks", 2, function(){
626    var m = new Backbone.Model({x:1});
627    var col = new Backbone.Collection();
628    var opts = {
629      success: function(collection, resp, options){
630        ok(options);
631      }
632    };
633    col.sync = m.sync = function( method, collection, options ){
634      options.success();
635    };
636    col.fetch(opts);
637    col.create(m, opts);
638  });
639
640  test("#1412 - Trigger 'sync' event.", 2, function() {
641    var collection = new Backbone.Collection;
642    collection.url = '/test';
643    collection.on('sync', function() { ok(true); });
644    Backbone.ajax = function(settings){ settings.success(); };
645    collection.fetch();
646    collection.create({id: 1});
647  });
648
649  test("#1447 - create with wait adds model.", 1, function() {
650    var collection = new Backbone.Collection;
651    var model = new Backbone.Model;
652    model.sync = function(method, model, options){ options.success(); };
653    collection.on('add', function(){ ok(true); });
654    collection.create(model, {wait: true});
655  });
656
657  test("#1448 - add sorts collection after merge.", 1, function() {
658    var collection = new Backbone.Collection([
659      {id: 1, x: 1},
660      {id: 2, x: 2}
661    ]);
662    collection.comparator = function(model){ return model.get('x'); };
663    collection.add({id: 1, x: 3}, {merge: true});
664    deepEqual(collection.pluck('id'), [2, 1]);
665  });
666
667  test("#1655 - groupBy can be used with a string argument.", 3, function() {
668    var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
669    var grouped = collection.groupBy('x');
670    strictEqual(_.keys(grouped).length, 2);
671    strictEqual(grouped[1][0].get('x'), 1);
672    strictEqual(grouped[2][0].get('x'), 2);
673  });
674
675  test("#1655 - sortBy can be used with a string argument.", 1, function() {
676    var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
677    var values = _.map(collection.sortBy('x'), function(model) {
678      return model.get('x');
679    });
680    deepEqual(values, [1, 2, 3]);
681  });
682
683  test("#1604 - Removal during iteration.", 0, function() {
684    var collection = new Backbone.Collection([{}, {}]);
685    collection.on('add', function() {
686      collection.at(0).destroy();
687    });
688    collection.add({}, {at: 0});
689  });
690
691  test("#1638 - `sort` during `add` triggers correctly.", function() {
692    var collection = new Backbone.Collection;
693    collection.comparator = function(model) { return model.get('x'); };
694    var added = [];
695    collection.on('add', function(model) {
696      model.set({x: 3});
697      collection.sort();
698      added.push(model.id);
699    });
700    collection.add([{id: 1, x: 1}, {id: 2, x: 2}]);
701    deepEqual(added, [1, 2]);
702  });
703
704  test("fetch parses models by default", 1, function() {
705    var model = {};
706    var Collection = Backbone.Collection.extend({
707      url: 'test',
708      model: Backbone.Model.extend({
709        parse: function(resp) {
710          strictEqual(resp, model);
711        }
712      })
713    });
714    new Collection().fetch();
715    this.ajaxSettings.success([model]);
716  });
717
718  test("`sort` shouldn't always fire on `add`", 1, function() {
719    var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
720      comparator: 'id'
721    });
722    c.sort = function(){ ok(true); };
723    c.add([]);
724    c.add({id: 1});
725    c.add([{id: 2}, {id: 3}]);
726    c.add({id: 4});
727  });
728
729  test("#1407 parse option on constructor parses collection and models", 2, function() {
730    var model = {
731      namespace : [{id: 1}, {id:2}]
732    };
733    var Collection = Backbone.Collection.extend({
734      model: Backbone.Model.extend({
735        parse: function(model) {
736          model.name = 'test';
737          return model;
738        }
739      }),
740      parse: function(model) {
741        return model.namespace;
742      }
743    });
744    var c = new Collection(model, {parse:true});
745
746    equal(c.length, 2);
747    equal(c.at(0).get('name'), 'test');
748  });
749
750  test("#1407 parse option on reset parses collection and models", 2, function() {
751    var model = {
752      namespace : [{id: 1}, {id:2}]
753    };
754    var Collection = Backbone.Collection.extend({
755      model: Backbone.Model.extend({
756        parse: function(model) {
757          model.name = 'test';
758          return model;
759        }
760      }),
761      parse: function(model) {
762        return model.namespace;
763      }
764    });
765    var c = new Collection();
766        c.reset(model, {parse:true});
767
768    equal(c.length, 2);
769    equal(c.at(0).get('name'), 'test');
770  });
771
772
773  test("Reset includes previous models in triggered event.", 1, function() {
774    var model = new Backbone.Model();
775    var collection = new Backbone.Collection([model])
776    .on('reset', function(collection, options) {
777      deepEqual(options.previousModels, [model]);
778    });
779    collection.reset([]);
780  });
781
782});