PageRenderTime 5ms CodeModel.GetById 2ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

/shortener/static/script/node_modules/backbone/test/collection.js

https://bitbucket.org/ldiamond/urlshortener
JavaScript | 568 lines | 513 code | 55 blank | 0 comment | 24 complexity | 9778736e23154d5f59d1db4cdfcc8a20 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: can't add model to collection twice", function() {
130    var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
131    equal(col.pluck('id').join(' '), '1 2 3');
132  });
133
134  test("Collection: can't add different model with same id to collection twice", function() {
135    var col = new Backbone.Collection;
136    col.unshift({id: 101});
137    col.add({id: 101});
138    equal(col.length, 1);
139  });
140
141  test("Collection: add model to multiple collections", function() {
142    var counter = 0;
143    var e = new Backbone.Model({id: 10, label : 'e'});
144    e.bind('add', function(model, collection) {
145      counter++;
146      equal(e, model);
147      if (counter > 1) {
148        equal(collection, colF);
149      } else {
150        equal(collection, colE);
151      }
152    });
153    var colE = new Backbone.Collection([]);
154    colE.bind('add', function(model, collection) {
155      equal(e, model);
156      equal(colE, collection);
157    });
158    var colF = new Backbone.Collection([]);
159    colF.bind('add', function(model, collection) {
160      equal(e, model);
161      equal(colF, collection);
162    });
163    colE.add(e);
164    equal(e.collection, colE);
165    colF.add(e);
166    equal(e.collection, colE);
167  });
168
169  test("Collection: add model with parse", function() {
170    var Model = Backbone.Model.extend({
171      parse: function(obj) {
172        obj.value += 1;
173        return obj;
174      }
175    });
176
177    var Col = Backbone.Collection.extend({model: Model});
178    var col = new Col;
179    col.add({value: 1}, {parse: true});
180    equal(col.at(0).get('value'), 2);
181  });
182
183  test("Collection: add model to collection with sort()-style comparator", function() {
184    var col = new Backbone.Collection;
185    col.comparator = function(a, b) {
186      return a.get('name') < b.get('name') ? -1 : 1;
187    };
188    var tom = new Backbone.Model({name: 'Tom'});
189    var rob = new Backbone.Model({name: 'Rob'});
190    var tim = new Backbone.Model({name: 'Tim'});
191    col.add(tom);
192    col.add(rob);
193    col.add(tim);
194    equal(col.indexOf(rob), 0);
195    equal(col.indexOf(tim), 1);
196    equal(col.indexOf(tom), 2);
197  });
198
199  test("Collection: comparator that depends on `this`", function() {
200    var col = new Backbone.Collection;
201    col.negative = function(num) {
202      return -num;
203    };
204    col.comparator = function(a) {
205      return this.negative(a.id);
206    };
207    col.add([{id: 1}, {id: 2}, {id: 3}]);
208    equal(col.pluck('id').join(' '), '3 2 1');
209  });
210
211  test("Collection: remove", function() {
212    var removed = null;
213    var otherRemoved = null;
214    col.bind('remove', function(model, col, options) {
215      removed = model.get('label');
216      equal(options.index, 3);
217    });
218    otherCol.bind('remove', function(model, col, options) {
219      otherRemoved = true;
220    });
221    col.remove(d);
222    equal(removed, 'd');
223    equal(col.length, 3);
224    equal(col.first(), a);
225    equal(otherRemoved, null);
226  });
227
228  test("Collection: shift and pop", function() {
229    var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
230    equal(col.shift().get('a'), 'a');
231    equal(col.pop().get('c'), 'c');
232  });
233
234  test("Collection: events are unbound on remove", function() {
235    var counter = 0;
236    var dj = new Backbone.Model();
237    var emcees = new Backbone.Collection([dj]);
238    emcees.bind('change', function(){ counter++; });
239    dj.set({name : 'Kool'});
240    equal(counter, 1);
241    emcees.reset([]);
242    equal(dj.collection, undefined);
243    dj.set({name : 'Shadow'});
244    equal(counter, 1);
245  });
246
247  test("Collection: remove in multiple collections", function() {
248    var modelData = {
249      id : 5,
250      title : 'Othello'
251    };
252    var passed = false;
253    var e = new Backbone.Model(modelData);
254    var f = new Backbone.Model(modelData);
255    f.bind('remove', function() {
256      passed = true;
257    });
258    var colE = new Backbone.Collection([e]);
259    var colF = new Backbone.Collection([f]);
260    ok(e != f);
261    ok(colE.length == 1);
262    ok(colF.length == 1);
263    colE.remove(e);
264    equal(passed, false);
265    ok(colE.length == 0);
266    colF.remove(e);
267    ok(colF.length == 0);
268    equal(passed, true);
269  });
270
271  test("Collection: remove same model in multiple collection", function() {
272    var counter = 0;
273    var e = new Backbone.Model({id: 5, title: 'Othello'});
274    e.bind('remove', function(model, collection) {
275      counter++;
276      equal(e, model);
277      if (counter > 1) {
278        equal(collection, colE);
279      } else {
280        equal(collection, colF);
281      }
282    });
283    var colE = new Backbone.Collection([e]);
284    colE.bind('remove', function(model, collection) {
285      equal(e, model);
286      equal(colE, collection);
287    });
288    var colF = new Backbone.Collection([e]);
289    colF.bind('remove', function(model, collection) {
290      equal(e, model);
291      equal(colF, collection);
292    });
293    equal(colE, e.collection);
294    colF.remove(e);
295    ok(colF.length == 0);
296    ok(colE.length == 1);
297    equal(counter, 1);
298    equal(colE, e.collection);
299    colE.remove(e);
300    equal(null, e.collection);
301    ok(colE.length == 0);
302    equal(counter, 2);
303  });
304
305  test("Collection: model destroy removes from all collections", function() {
306    var e = new Backbone.Model({id: 5, title: 'Othello'});
307    e.sync = function(method, model, options) { options.success({}); };
308    var colE = new Backbone.Collection([e]);
309    var colF = new Backbone.Collection([e]);
310    e.destroy();
311    ok(colE.length == 0);
312    ok(colF.length == 0);
313    equal(undefined, e.collection);
314  });
315
316  test("Colllection: non-persisted model destroy removes from all collections", function() {
317    var e = new Backbone.Model({title: 'Othello'});
318    e.sync = function(method, model, options) { throw "should not be called"; };
319    var colE = new Backbone.Collection([e]);
320    var colF = new Backbone.Collection([e]);
321    e.destroy();
322    ok(colE.length == 0);
323    ok(colF.length == 0);
324    equal(undefined, e.collection);
325  });
326
327  test("Collection: fetch", function() {
328    col.fetch();
329    equal(lastRequest.method, 'read');
330    equal(lastRequest.model, col);
331    equal(lastRequest.options.parse, true);
332
333    col.fetch({parse: false});
334    equal(lastRequest.options.parse, false);
335  });
336
337  test("Collection: create", function() {
338    var model = col.create({label: 'f'}, {wait: true});
339    equal(lastRequest.method, 'create');
340    equal(lastRequest.model, model);
341    equal(model.get('label'), 'f');
342    equal(model.collection, col);
343  });
344
345  test("Collection: create enforces validation", function() {
346    var ValidatingModel = Backbone.Model.extend({
347      validate: function(attrs) {
348        return "fail";
349      }
350    });
351    var ValidatingCollection = Backbone.Collection.extend({
352      model: ValidatingModel
353    });
354    var col = new ValidatingCollection();
355    equal(col.create({"foo":"bar"}), false);
356  });
357
358  test("Collection: a failing create runs the error callback", function() {
359    var ValidatingModel = Backbone.Model.extend({
360      validate: function(attrs) {
361        return "fail";
362      }
363    });
364    var ValidatingCollection = Backbone.Collection.extend({
365      model: ValidatingModel
366    });
367    var flag = false;
368    var callback = function(model, error) { flag = true; };
369    var col = new ValidatingCollection();
370    col.create({"foo":"bar"}, { error: callback });
371    equal(flag, true);
372  });
373
374  test("collection: initialize", function() {
375    var Collection = Backbone.Collection.extend({
376      initialize: function() {
377        this.one = 1;
378      }
379    });
380    var coll = new Collection;
381    equal(coll.one, 1);
382  });
383
384  test("Collection: toJSON", function() {
385    equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
386  });
387
388  test("Collection: where", function() {
389    var coll = new Backbone.Collection([
390      {a: 1},
391      {a: 1},
392      {a: 1, b: 2},
393      {a: 2, b: 2},
394      {a: 3}
395    ]);
396    equal(coll.where({a: 1}).length, 3);
397    equal(coll.where({a: 2}).length, 1);
398    equal(coll.where({a: 3}).length, 1);
399    equal(coll.where({b: 1}).length, 0);
400    equal(coll.where({b: 2}).length, 2);
401    equal(coll.where({a: 1, b: 2}).length, 1);
402  });
403
404  test("Collection: Underscore methods", function() {
405    equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
406    equal(col.any(function(model){ return model.id === 100; }), false);
407    equal(col.any(function(model){ return model.id === 0; }), true);
408    equal(col.indexOf(b), 1);
409    equal(col.size(), 4);
410    equal(col.rest().length, 3);
411    ok(!_.include(col.rest()), a);
412    ok(!_.include(col.rest()), d);
413    ok(!col.isEmpty());
414    ok(!_.include(col.without(d)), d);
415    equal(col.max(function(model){ return model.id; }).id, 3);
416    equal(col.min(function(model){ return model.id; }).id, 0);
417    same(col.chain()
418            .filter(function(o){ return o.id % 2 === 0; })
419            .map(function(o){ return o.id * 2; })
420            .value(),
421         [4, 0]);
422  });
423
424  test("Collection: reset", function() {
425    var resetCount = 0;
426    var models = col.models;
427    col.bind('reset', function() { resetCount += 1; });
428    col.reset([]);
429    equal(resetCount, 1);
430    equal(col.length, 0);
431    equal(col.last(), null);
432    col.reset(models);
433    equal(resetCount, 2);
434    equal(col.length, 4);
435    equal(col.last(), d);
436    col.reset(_.map(models, function(m){ return m.attributes; }));
437    equal(resetCount, 3);
438    equal(col.length, 4);
439    ok(col.last() !== d);
440    ok(_.isEqual(col.last().attributes, d.attributes));
441  });
442
443  test("Collection: reset passes caller options", function() {
444    var Model = Backbone.Model.extend({
445      initialize: function(attrs, options) {
446        this.model_parameter = options.model_parameter;
447      }
448    });
449    var col = new (Backbone.Collection.extend({ model: Model }))();
450    col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 }], { model_parameter: 'model parameter' });
451    equal(col.length, 2);
452    col.each(function(model) {
453      equal(model.model_parameter, 'model parameter');
454    });
455  });
456
457  test("Collection: trigger custom events on models", function() {
458    var fired = null;
459    a.bind("custom", function() { fired = true; });
460    a.trigger("custom");
461    equal(fired, true);
462  });
463
464  test("Collection: add does not alter arguments", function(){
465    var attrs = {};
466    var models = [attrs];
467    new Backbone.Collection().add(models);
468    equal(models.length, 1);
469    ok(attrs === models[0]);
470  });
471
472  test("#714: access `model.collection` in a brand new model.", 2, function() {
473    var col = new Backbone.Collection;
474    var Model = Backbone.Model.extend({
475      set: function(attrs) {
476        equal(attrs.prop, 'value');
477        equal(this.collection, col);
478        return this;
479      }
480    });
481    col.model = Model;
482    col.create({prop: 'value'});
483  });
484
485  test("#574, remove its own reference to the .models array.", function() {
486    var col = new Backbone.Collection([
487      {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
488    ]);
489    equal(col.length, 6);
490    col.remove(col.models);
491    equal(col.length, 0);
492  });
493
494  test("#861, adding models to a collection which do not pass validation", function() {
495    raises(function() {
496      var Model = Backbone.Model.extend({
497        validate: function(attrs) {
498          if (attrs.id == 3) return "id can't be 3";
499        }
500      });
501
502      var Collection = Backbone.Collection.extend({
503        model: Model
504      });
505
506      var col = new Collection;
507
508      col.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
509    }, "Can't add an invalid model to a collection");
510  });
511
512  test("Collection: index with comparator", function() {
513    expect(4);
514    var counter = 0;
515    var col = new Backbone.Collection([{id: 2}, {id: 4}], {
516      comparator: function(model){ return model.id; }
517    }).on('add', function(model, colleciton, options){
518      if (model.id == 1) {
519        equal(options.index, 0);
520        equal(counter++, 0);
521      }
522      if (model.id == 3) {
523        equal(options.index, 2);
524        equal(counter++, 1);
525      }
526    });
527    col.add([{id: 3}, {id: 1}]);
528  });
529
530  test("Collection: throwing during add leaves consistent state", function() {
531    expect(4);
532    var col = new Backbone.Collection();
533    col.bind('test', function() { ok(false); });
534    col.model = Backbone.Model.extend({
535      validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
536    });
537    var model = new col.model({id: 1, valid: true});
538    raises(function() { col.add([model, {id: 2}]); });
539    model.trigger('test');
540    ok(!col.getByCid(model.cid));
541    ok(!col.get(1));
542    equal(col.length, 0);
543  });
544
545  test("Collection: multiple copies of the same model", function() {
546    var col = new Backbone.Collection();
547    var model = new Backbone.Model();
548    col.add([model, model]);
549    equal(col.length, 1);
550    col.add([{id: 1}, {id: 1}]);
551    equal(col.length, 2);
552    equal(col.last().id, 1);
553  });
554
555  test("#964 - collection.get return in consistent", function() {
556    var c = new Backbone.Collection();
557    ok(c.get(null) === undefined);
558    ok(c.get() === undefined);
559  });
560
561  test("#1112 - passing options.model sets collection.model", function() {
562    var Model = Backbone.Model.extend({});
563    var c = new Backbone.Collection([{id: 1}], {model: Model});
564    ok(c.model === Model);
565    ok(c.at(0) instanceof Model);
566  });
567
568});