PageRenderTime 389ms CodeModel.GetById 141ms app.highlight 155ms RepoModel.GetById 87ms app.codeStats 0ms

/test/collection.js

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