PageRenderTime 952ms CodeModel.GetById 161ms app.highlight 169ms RepoModel.GetById 616ms app.codeStats 0ms

/test/collection.js

https://github.com/Flushot/backbone
JavaScript | 592 lines | 533 code | 59 blank | 0 comment | 24 complexity | 68df986572c211dec5c724a7cff30676 MD5 | raw file
  1$(document).ready(function() {
  2
  3  var lastRequest = null;
  4  var sync = Backbone.sync;
  5
  6  var a, b, c, d, e, col, otherCol;
  7
  8  module("Backbone.Collection", {
  9
 10    setup: function() {
 11      a         = new Backbone.Model({id: 3, label: 'a'});
 12      b         = new Backbone.Model({id: 2, label: 'b'});
 13      c         = new Backbone.Model({id: 1, label: 'c'});
 14      d         = new Backbone.Model({id: 0, label: 'd'});
 15      e         = null;
 16      col       = new Backbone.Collection([a,b,c,d]);
 17      otherCol  = new Backbone.Collection();
 18
 19      Backbone.sync = function(method, model, options) {
 20        lastRequest = {
 21          method: method,
 22          model: model,
 23          options: options
 24        };
 25      };
 26    },
 27
 28    teardown: function() {
 29      Backbone.sync = sync;
 30    }
 31
 32  });
 33
 34  test("Collection: new and sort", function() {
 35    equal(col.first(), a, "a should be first");
 36    equal(col.last(), d, "d should be last");
 37    col.comparator = function(a, b) {
 38      return a.id > b.id ? -1 : 1;
 39    };
 40    col.sort();
 41    equal(col.first(), a, "a should be first");
 42    equal(col.last(), d, "d should be last");
 43    col.comparator = function(model) { return model.id; };
 44    col.sort();
 45    equal(col.first(), d, "d should be first");
 46    equal(col.last(), a, "a should be last");
 47    equal(col.length, 4);
 48  });
 49
 50  test("Collection: get, getByCid", function() {
 51    equal(col.get(0), d);
 52    equal(col.get(2), b);
 53    equal(col.getByCid(col.first().cid), col.first());
 54  });
 55
 56  test("Collection: get with non-default ids", function() {
 57    var col = new Backbone.Collection();
 58    var MongoModel = Backbone.Model.extend({
 59      idAttribute: '_id'
 60    });
 61    var model = new MongoModel({_id: 100});
 62    col.push(model);
 63    equal(col.get(100), model);
 64    model.set({_id: 101});
 65    equal(col.get(101), model);
 66  });
 67
 68  test("Collection: update index when id changes", function() {
 69    var col = new Backbone.Collection();
 70    col.add([
 71      {id : 0, name : 'one'},
 72      {id : 1, name : 'two'}
 73    ]);
 74    var one = col.get(0);
 75    equal(one.get('name'), 'one');
 76    one.set({id : 101});
 77    equal(col.get(0), null);
 78    equal(col.get(101).get('name'), 'one');
 79  });
 80
 81  test("Collection: at", function() {
 82    equal(col.at(2), c);
 83  });
 84
 85  test("Collection: pluck", function() {
 86    equal(col.pluck('label').join(' '), 'a b c d');
 87  });
 88
 89  test("Collection: add", function() {
 90    var added, opts, secondAdded;
 91    added = opts = secondAdded = null;
 92    e = new Backbone.Model({id: 10, label : 'e'});
 93    otherCol.add(e);
 94    otherCol.bind('add', function() {
 95      secondAdded = true;
 96    });
 97    col.bind('add', function(model, collection, options){
 98      added = model.get('label');
 99      equal(options.index, 4);
100      opts = options;
101    });
102    col.add(e, {amazing: true});
103    equal(added, 'e');
104    equal(col.length, 5);
105    equal(col.last(), e);
106    equal(otherCol.length, 1);
107    equal(secondAdded, null);
108    ok(opts.amazing);
109
110    var f = new Backbone.Model({id: 20, label : 'f'});
111    var g = new Backbone.Model({id: 21, label : 'g'});
112    var h = new Backbone.Model({id: 22, label : 'h'});
113    var atCol = new Backbone.Collection([f, g, h]);
114    equal(atCol.length, 3);
115    atCol.add(e, {at: 1});
116    equal(atCol.length, 4);
117    equal(atCol.at(1), e);
118    equal(atCol.last(), h);
119  });
120
121  test("Collection: add multiple models", function() {
122    var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
123    col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
124    for (var i = 0; i <= 5; i++) {
125      equal(col.at(i).get('at'), i);
126    }
127  });
128
129  test("Collection: add; at should have preference over comparator", function() {
130    var Col = Backbone.Collection.extend({
131      comparator: function(a,b) {
132        return a.id > b.id ? -1 : 1;
133      }
134    });
135
136    var col = new Col([{id: 2}, {id: 3}]);
137    col.add(new Backbone.Model({id: 1}), {at:   1});
138
139    equal(col.pluck('id').join(' '), '3 1 2');
140  });
141
142  test("Collection: can't add model to collection twice", function() {
143    var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
144    equal(col.pluck('id').join(' '), '1 2 3');
145  });
146
147  test("Collection: can't add different model with same id to collection twice", function() {
148    var col = new Backbone.Collection;
149    col.unshift({id: 101});
150    col.add({id: 101});
151    equal(col.length, 1);
152  });
153
154  test("Collection: merge in duplicate models with {merge: true}", function() {
155    var col = new Backbone.Collection;
156    col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]);
157    col.add({id: 1, name: 'Moses'});
158    equal(col.first().get('name'), 'Moe');
159    col.add({id: 1, name: 'Moses'}, {merge: true});
160    equal(col.first().get('name'), 'Moses');
161  });
162
163  test("Collection: add model to multiple collections", function() {
164    var counter = 0;
165    var e = new Backbone.Model({id: 10, label : 'e'});
166    e.bind('add', function(model, collection) {
167      counter++;
168      equal(e, model);
169      if (counter > 1) {
170        equal(collection, colF);
171      } else {
172        equal(collection, colE);
173      }
174    });
175    var colE = new Backbone.Collection([]);
176    colE.bind('add', function(model, collection) {
177      equal(e, model);
178      equal(colE, collection);
179    });
180    var colF = new Backbone.Collection([]);
181    colF.bind('add', function(model, collection) {
182      equal(e, model);
183      equal(colF, collection);
184    });
185    colE.add(e);
186    equal(e.collection, colE);
187    colF.add(e);
188    equal(e.collection, colE);
189  });
190
191  test("Collection: add model with parse", function() {
192    var Model = Backbone.Model.extend({
193      parse: function(obj) {
194        obj.value += 1;
195        return obj;
196      }
197    });
198
199    var Col = Backbone.Collection.extend({model: Model});
200    var col = new Col;
201    col.add({value: 1}, {parse: true});
202    equal(col.at(0).get('value'), 2);
203  });
204
205  test("Collection: add model to collection with sort()-style comparator", function() {
206    var col = new Backbone.Collection;
207    col.comparator = function(a, b) {
208      return a.get('name') < b.get('name') ? -1 : 1;
209    };
210    var tom = new Backbone.Model({name: 'Tom'});
211    var rob = new Backbone.Model({name: 'Rob'});
212    var tim = new Backbone.Model({name: 'Tim'});
213    col.add(tom);
214    col.add(rob);
215    col.add(tim);
216    equal(col.indexOf(rob), 0);
217    equal(col.indexOf(tim), 1);
218    equal(col.indexOf(tom), 2);
219  });
220
221  test("Collection: comparator that depends on `this`", function() {
222    var col = new Backbone.Collection;
223    col.negative = function(num) {
224      return -num;
225    };
226    col.comparator = function(a) {
227      return this.negative(a.id);
228    };
229    col.add([{id: 1}, {id: 2}, {id: 3}]);
230    equal(col.pluck('id').join(' '), '3 2 1');
231  });
232
233  test("Collection: remove", function() {
234    var removed = null;
235    var otherRemoved = null;
236    col.bind('remove', function(model, col, options) {
237      removed = model.get('label');
238      equal(options.index, 3);
239    });
240    otherCol.bind('remove', function(model, col, options) {
241      otherRemoved = true;
242    });
243    col.remove(d);
244    equal(removed, 'd');
245    equal(col.length, 3);
246    equal(col.first(), a);
247    equal(otherRemoved, null);
248  });
249
250  test("Collection: shift and pop", function() {
251    var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
252    equal(col.shift().get('a'), 'a');
253    equal(col.pop().get('c'), 'c');
254  });
255
256  test("Collection: events are unbound on remove", function() {
257    var counter = 0;
258    var dj = new Backbone.Model();
259    var emcees = new Backbone.Collection([dj]);
260    emcees.bind('change', function(){ counter++; });
261    dj.set({name : 'Kool'});
262    equal(counter, 1);
263    emcees.reset([]);
264    equal(dj.collection, undefined);
265    dj.set({name : 'Shadow'});
266    equal(counter, 1);
267  });
268
269  test("Collection: remove in multiple collections", function() {
270    var modelData = {
271      id : 5,
272      title : 'Othello'
273    };
274    var passed = false;
275    var e = new Backbone.Model(modelData);
276    var f = new Backbone.Model(modelData);
277    f.bind('remove', function() {
278      passed = true;
279    });
280    var colE = new Backbone.Collection([e]);
281    var colF = new Backbone.Collection([f]);
282    ok(e != f);
283    ok(colE.length == 1);
284    ok(colF.length == 1);
285    colE.remove(e);
286    equal(passed, false);
287    ok(colE.length == 0);
288    colF.remove(e);
289    ok(colF.length == 0);
290    equal(passed, true);
291  });
292
293  test("Collection: remove same model in multiple collection", function() {
294    var counter = 0;
295    var e = new Backbone.Model({id: 5, title: 'Othello'});
296    e.bind('remove', function(model, collection) {
297      counter++;
298      equal(e, model);
299      if (counter > 1) {
300        equal(collection, colE);
301      } else {
302        equal(collection, colF);
303      }
304    });
305    var colE = new Backbone.Collection([e]);
306    colE.bind('remove', function(model, collection) {
307      equal(e, model);
308      equal(colE, collection);
309    });
310    var colF = new Backbone.Collection([e]);
311    colF.bind('remove', function(model, collection) {
312      equal(e, model);
313      equal(colF, collection);
314    });
315    equal(colE, e.collection);
316    colF.remove(e);
317    ok(colF.length == 0);
318    ok(colE.length == 1);
319    equal(counter, 1);
320    equal(colE, e.collection);
321    colE.remove(e);
322    equal(null, e.collection);
323    ok(colE.length == 0);
324    equal(counter, 2);
325  });
326
327  test("Collection: model destroy removes from all collections", function() {
328    var e = new Backbone.Model({id: 5, title: 'Othello'});
329    e.sync = function(method, model, options) { options.success({}); };
330    var colE = new Backbone.Collection([e]);
331    var colF = new Backbone.Collection([e]);
332    e.destroy();
333    ok(colE.length == 0);
334    ok(colF.length == 0);
335    equal(undefined, e.collection);
336  });
337
338  test("Colllection: non-persisted model destroy removes from all collections", function() {
339    var e = new Backbone.Model({title: 'Othello'});
340    e.sync = function(method, model, options) { throw "should not be called"; };
341    var colE = new Backbone.Collection([e]);
342    var colF = new Backbone.Collection([e]);
343    e.destroy();
344    ok(colE.length == 0);
345    ok(colF.length == 0);
346    equal(undefined, e.collection);
347  });
348
349  test("Collection: fetch", function() {
350    col.fetch();
351    equal(lastRequest.method, 'read');
352    equal(lastRequest.model, col);
353    equal(lastRequest.options.parse, true);
354
355    col.fetch({parse: false});
356    equal(lastRequest.options.parse, false);
357  });
358
359  test("Collection: create", function() {
360    var model = col.create({label: 'f'}, {wait: true});
361    equal(lastRequest.method, 'create');
362    equal(lastRequest.model, model);
363    equal(model.get('label'), 'f');
364    equal(model.collection, col);
365  });
366
367  test("Collection: create enforces validation", function() {
368    var ValidatingModel = Backbone.Model.extend({
369      validate: function(attrs) {
370        return "fail";
371      }
372    });
373    var ValidatingCollection = Backbone.Collection.extend({
374      model: ValidatingModel
375    });
376    var col = new ValidatingCollection();
377    equal(col.create({"foo":"bar"}), false);
378  });
379
380  test("Collection: a failing create runs the error callback", function() {
381    var ValidatingModel = Backbone.Model.extend({
382      validate: function(attrs) {
383        return "fail";
384      }
385    });
386    var ValidatingCollection = Backbone.Collection.extend({
387      model: ValidatingModel
388    });
389    var flag = false;
390    var callback = function(model, error) { flag = true; };
391    var col = new ValidatingCollection();
392    col.create({"foo":"bar"}, { error: callback });
393    equal(flag, true);
394  });
395
396  test("collection: initialize", function() {
397    var Collection = Backbone.Collection.extend({
398      initialize: function() {
399        this.one = 1;
400      }
401    });
402    var coll = new Collection;
403    equal(coll.one, 1);
404  });
405
406  test("Collection: toJSON", function() {
407    equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
408  });
409
410  test("Collection: where", function() {
411    var coll = new Backbone.Collection([
412      {a: 1},
413      {a: 1},
414      {a: 1, b: 2},
415      {a: 2, b: 2},
416      {a: 3}
417    ]);
418    equal(coll.where({a: 1}).length, 3);
419    equal(coll.where({a: 2}).length, 1);
420    equal(coll.where({a: 3}).length, 1);
421    equal(coll.where({b: 1}).length, 0);
422    equal(coll.where({b: 2}).length, 2);
423    equal(coll.where({a: 1, b: 2}).length, 1);
424  });
425
426  test("Collection: Underscore methods", function() {
427    equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
428    equal(col.any(function(model){ return model.id === 100; }), false);
429    equal(col.any(function(model){ return model.id === 0; }), true);
430    equal(col.indexOf(b), 1);
431    equal(col.size(), 4);
432    equal(col.rest().length, 3);
433    ok(!_.include(col.rest()), a);
434    ok(!_.include(col.rest()), d);
435    ok(!col.isEmpty());
436    ok(!_.include(col.without(d)), d);
437    equal(col.max(function(model){ return model.id; }).id, 3);
438    equal(col.min(function(model){ return model.id; }).id, 0);
439    same(col.chain()
440            .filter(function(o){ return o.id % 2 === 0; })
441            .map(function(o){ return o.id * 2; })
442            .value(),
443         [4, 0]);
444  });
445
446  test("Collection: reset", function() {
447    var resetCount = 0;
448    var models = col.models;
449    col.bind('reset', function() { resetCount += 1; });
450    col.reset([]);
451    equal(resetCount, 1);
452    equal(col.length, 0);
453    equal(col.last(), null);
454    col.reset(models);
455    equal(resetCount, 2);
456    equal(col.length, 4);
457    equal(col.last(), d);
458    col.reset(_.map(models, function(m){ return m.attributes; }));
459    equal(resetCount, 3);
460    equal(col.length, 4);
461    ok(col.last() !== d);
462    ok(_.isEqual(col.last().attributes, d.attributes));
463  });
464
465  test("Collection: reset passes caller options", function() {
466    var Model = Backbone.Model.extend({
467      initialize: function(attrs, options) {
468        this.model_parameter = options.model_parameter;
469      }
470    });
471    var col = new (Backbone.Collection.extend({ model: Model }))();
472    col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 }], { model_parameter: 'model parameter' });
473    equal(col.length, 2);
474    col.each(function(model) {
475      equal(model.model_parameter, 'model parameter');
476    });
477  });
478
479  test("Collection: trigger custom events on models", function() {
480    var fired = null;
481    a.bind("custom", function() { fired = true; });
482    a.trigger("custom");
483    equal(fired, true);
484  });
485
486  test("Collection: add does not alter arguments", function(){
487    var attrs = {};
488    var models = [attrs];
489    new Backbone.Collection().add(models);
490    equal(models.length, 1);
491    ok(attrs === models[0]);
492  });
493
494  test("#714: access `model.collection` in a brand new model.", 2, function() {
495    var col = new Backbone.Collection;
496    var Model = Backbone.Model.extend({
497      set: function(attrs) {
498        equal(attrs.prop, 'value');
499        equal(this.collection, col);
500        return this;
501      }
502    });
503    col.model = Model;
504    col.create({prop: 'value'});
505  });
506
507  test("#574, remove its own reference to the .models array.", function() {
508    var col = new Backbone.Collection([
509      {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
510    ]);
511    equal(col.length, 6);
512    col.remove(col.models);
513    equal(col.length, 0);
514  });
515
516  test("#861, adding models to a collection which do not pass validation", function() {
517    raises(function() {
518      var Model = Backbone.Model.extend({
519        validate: function(attrs) {
520          if (attrs.id == 3) return "id can't be 3";
521        }
522      });
523
524      var Collection = Backbone.Collection.extend({
525        model: Model
526      });
527
528      var col = new Collection;
529
530      col.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
531    }, function(e) {
532      return e.message === "Can't add an invalid model to a collection";
533    });
534  });
535
536  test("Collection: index with comparator", function() {
537    expect(4);
538    var counter = 0;
539    var col = new Backbone.Collection([{id: 2}, {id: 4}], {
540      comparator: function(model){ return model.id; }
541    }).on('add', function(model, colleciton, options){
542      if (model.id == 1) {
543        equal(options.index, 0);
544        equal(counter++, 0);
545      }
546      if (model.id == 3) {
547        equal(options.index, 2);
548        equal(counter++, 1);
549      }
550    });
551    col.add([{id: 3}, {id: 1}]);
552  });
553
554  test("Collection: throwing during add leaves consistent state", function() {
555    expect(4);
556    var col = new Backbone.Collection();
557    col.bind('test', function() { ok(false); });
558    col.model = Backbone.Model.extend({
559      validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
560    });
561    var model = new col.model({id: 1, valid: true});
562    raises(function() { col.add([model, {id: 2}]); });
563    model.trigger('test');
564    ok(!col.getByCid(model.cid));
565    ok(!col.get(1));
566    equal(col.length, 0);
567  });
568
569  test("Collection: multiple copies of the same model", function() {
570    var col = new Backbone.Collection();
571    var model = new Backbone.Model();
572    col.add([model, model]);
573    equal(col.length, 1);
574    col.add([{id: 1}, {id: 1}]);
575    equal(col.length, 2);
576    equal(col.last().id, 1);
577  });
578
579  test("#964 - collection.get return in consistent", function() {
580    var c = new Backbone.Collection();
581    ok(c.get(null) === undefined);
582    ok(c.get() === undefined);
583  });
584
585  test("#1112 - passing options.model sets collection.model", function() {
586    var Model = Backbone.Model.extend({});
587    var c = new Backbone.Collection([{id: 1}], {model: Model});
588    ok(c.model === Model);
589    ok(c.at(0) instanceof Model);
590  });
591
592});