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

/test/collection.js

https://github.com/pmichelberger/backbone
JavaScript | 424 lines | 380 code | 43 blank | 1 comment | 18 complexity | 3fffad55591140a95144fa9e9ce7a8c4 MD5 | raw file
  1$(document).ready(function() {
  2
  3  module("Backbone.Collection");
  4
  5  window.lastRequest = null;
  6
  7  Backbone.sync = function() {
  8    lastRequest = _.toArray(arguments);
  9  };
 10
 11  var a         = new Backbone.Model({id: 3, label: 'a'});
 12  var b         = new Backbone.Model({id: 2, label: 'b'});
 13  var c         = new Backbone.Model({id: 1, label: 'c'});
 14  var d         = new Backbone.Model({id: 0, label: 'd'});
 15  var e         = null;
 16  var col       = new Backbone.Collection([a,b,c,d]);
 17  var otherCol  = new Backbone.Collection();
 18
 19  test("Collection: new and sort", function() {
 20    equals(col.first(), a, "a should be first");
 21    equals(col.last(), d, "d should be last");
 22    col.comparator = function(model) { return model.id; };
 23    col.sort();
 24    equals(col.first(), d, "d should be first");
 25    equals(col.last(), a, "a should be last");
 26    equals(col.length, 4);
 27  });
 28
 29  test("Collection: get, getByCid", function() {
 30    equals(col.get(0), d);
 31    equals(col.get(2), b);
 32    equals(col.getByCid(col.first().cid), col.first());
 33  });
 34
 35  test("Collection: get with non-default ids", function() {
 36    var col = new Backbone.Collection();
 37    var MongoModel = Backbone.Model.extend({
 38      idAttribute: '_id'
 39    });
 40    var model = new MongoModel({_id: 100});
 41    col.add(model);
 42    equals(col.get(100), model);
 43    model.set({_id: 101});
 44    equals(col.get(101), model);
 45  });
 46
 47  test("Collection: add model with attributes modified by set", function() {
 48    var CustomSetModel = Backbone.Model.extend({
 49      defaults: {
 50        number_as_string: null //presence of defaults forces extend
 51      },
 52
 53      validate: function (attributes) {
 54        if (!_.isString(attributes.num_as_string)) {
 55          return 'fail';
 56        }
 57      },
 58
 59      set: function (attributes, options) {
 60        if (attributes.num_as_string) {
 61          attributes.num_as_string = attributes.num_as_string.toString();
 62        }
 63        Backbone.Model.prototype.set.call(this, attributes, options);
 64      }
 65    });
 66
 67    var CustomSetCollection = Backbone.Collection.extend({
 68      model: CustomSetModel
 69    });
 70    var col = new CustomSetCollection([{ num_as_string: 2 }]);
 71    equals(col.length, 1);
 72  });
 73
 74  test("Collection: update index when id changes", function() {
 75    var col = new Backbone.Collection();
 76    col.add([
 77      {id : 0, name : 'one'},
 78      {id : 1, name : 'two'}
 79    ]);
 80    var one = col.get(0);
 81    equals(one.get('name'), 'one');
 82    one.set({id : 101});
 83    equals(col.get(0), null);
 84    equals(col.get(101).get('name'), 'one');
 85  });
 86
 87  test("Collection: at", function() {
 88    equals(col.at(2), b);
 89  });
 90
 91  test("Collection: pluck", function() {
 92    equals(col.pluck('label').join(' '), 'd c b a');
 93  });
 94
 95  test("Collection: add", function() {
 96    var added = opts = secondAdded = null;
 97    e = new Backbone.Model({id: 10, label : 'e'});
 98    otherCol.add(e);
 99    otherCol.bind('add', function() {
100      secondAdded = true;
101    });
102    col.bind('add', function(model, collection, options){
103      added = model.get('label');
104      opts = options;
105    });
106    col.add(e, {amazing: true});
107    equals(added, 'e');
108    equals(col.length, 5);
109    equals(col.last(), e);
110    equals(otherCol.length, 1);
111    equals(secondAdded, null);
112    ok(opts.amazing);
113
114    var f = new Backbone.Model({id: 20, label : 'f'});
115    var g = new Backbone.Model({id: 21, label : 'g'});
116    var h = new Backbone.Model({id: 22, label : 'h'});
117    var atCol = new Backbone.Collection([f, g, h]);
118    equals(atCol.length, 3);
119    atCol.add(e, {at: 1});
120    equals(atCol.length, 4);
121    equals(atCol.at(1), e);
122    equals(atCol.last(), h);
123  });
124
125  test("Collection: add model to collection and verify index updates", function() {
126    var f = new Backbone.Model({id: 20, label : 'f'});
127    var g = new Backbone.Model({id: 21, label : 'g'});
128    var h = new Backbone.Model({id: 22, label : 'h'});
129    var col = new Backbone.Collection();
130    
131    var counts = [];
132     
133    col.bind('add', function(model, collection, options) {
134      counts.push(options.index);  
135    });
136    col.add(f); 
137    col.add(g); 
138    col.add(h); 
139    ok(_.isEqual(counts, [0,1,2]));
140  });
141
142  test("Collection: add model to collection twice", function() {
143    try {
144      // no id, same cid
145      var a2 = new Backbone.Model({label: a.label});
146      a2.cid = a.cid;
147      col.add(a2);
148      ok(false, "duplicate; expected add to fail");
149    } catch (e) {
150      equals(e.message, "Can't add the same model to a set twice,3");
151    }
152  });
153
154  test("Collection: add model to multiple collections", function() {
155    var counter = 0;
156    var e = new Backbone.Model({id: 10, label : 'e'});
157    e.bind('add', function(model, collection) {
158      counter++;
159      equals(e, model);
160      if (counter > 1) {
161        equals(collection, colF);
162      } else {
163        equals(collection, colE);
164      }
165    });
166    var colE = new Backbone.Collection([]);
167    colE.bind('add', function(model, collection) {
168      equals(e, model);
169      equals(colE, collection);
170    });
171    var colF = new Backbone.Collection([]);
172    colF.bind('add', function(model, collection) {
173      equals(e, model);
174      equals(colF, collection);
175    });
176    colE.add(e);
177    equals(e.collection, colE);
178    colF.add(e);
179    equals(e.collection, colE);
180  });
181
182  test("Collection: add model with parse", function() {
183    var Model = Backbone.Model.extend({
184      parse: function(obj) {
185        obj.value += 1;
186        return obj;
187      }
188    });
189
190    var Col = Backbone.Collection.extend({model: Model});
191    var col = new Col;
192    col.add({value: 1}, {parse: true});
193    equals(col.at(0).get('value'), 2);
194  });
195
196  test("Collection: remove", function() {
197    var removed = otherRemoved = null;
198    col.bind('remove', function(model){ removed = model.get('label'); });
199    otherCol.bind('remove', function(){ otherRemoved = true; });
200    col.remove(e);
201    equals(removed, 'e');
202    equals(col.length, 4);
203    equals(col.first(), d);
204    equals(otherRemoved, null);
205  });
206
207  test("Collection: remove should return correct index events", function() {
208    var f = new Backbone.Model({id: 20, label : 'f'});
209    var g = new Backbone.Model({id: 21, label : 'g'});
210    var h = new Backbone.Model({id: 22, label : 'h'});
211    var col = new Backbone.Collection([f,g,h]);
212    
213    var counts = [];
214     
215    col.bind('remove', function(model, collection, options) {
216      counts.push(options.index);  
217    });
218    col.remove(h); 
219    col.remove(g); 
220    col.remove(f); 
221    ok(_.isEqual(counts, [2,1,0]));
222  });
223
224  test("Collection: events are unbound on remove", function() {
225    var counter = 0;
226    var dj = new Backbone.Model();
227    var emcees = new Backbone.Collection([dj]);
228    emcees.bind('change', function(){ counter++; });
229    dj.set({name : 'Kool'});
230    equals(counter, 1);
231    emcees.reset([]);
232    equals(dj.collection, undefined);
233    dj.set({name : 'Shadow'});
234    equals(counter, 1);
235  });
236
237  test("Collection: remove in multiple collections", function() {
238    var modelData = {
239      id : 5,
240      title : 'Othello'
241    };
242    var passed = false;
243    var e = new Backbone.Model(modelData);
244    var f = new Backbone.Model(modelData);
245    f.bind('remove', function() {
246      passed = true;
247    });
248    var colE = new Backbone.Collection([e]);
249    var colF = new Backbone.Collection([f]);
250    ok(e != f);
251    ok(colE.length == 1);
252    ok(colF.length == 1);
253    colE.remove(e);
254    equals(passed, false);
255    ok(colE.length == 0);
256    colF.remove(e);
257    ok(colF.length == 0);
258    equals(passed, true);
259  });
260
261  test("Collection: remove same model in multiple collection", function() {
262    var counter = 0;
263    var e = new Backbone.Model({id: 5, title: 'Othello'});
264    e.bind('remove', function(model, collection) {
265      counter++;
266      equals(e, model);
267      if (counter > 1) {
268        equals(collection, colE);
269      } else {
270        equals(collection, colF);
271      }
272    });
273    var colE = new Backbone.Collection([e]);
274    colE.bind('remove', function(model, collection) {
275      equals(e, model);
276      equals(colE, collection);
277    });
278    var colF = new Backbone.Collection([e]);
279    colF.bind('remove', function(model, collection) {
280      equals(e, model);
281      equals(colF, collection);
282    });
283    equals(colE, e.collection);
284    colF.remove(e);
285    ok(colF.length == 0);
286    ok(colE.length == 1);
287    equals(counter, 1);
288    equals(colE, e.collection);
289    colE.remove(e);
290    equals(null, e.collection);
291    ok(colE.length == 0);
292    equals(counter, 2);
293  });
294
295  test("Collection: model destroy removes from all collections", function() {
296    var e = new Backbone.Model({id: 5, title: 'Othello'});
297    e.sync = function(method, model, options) { options.success({}); };
298    var colE = new Backbone.Collection([e]);
299    var colF = new Backbone.Collection([e]);
300    e.destroy();
301    ok(colE.length == 0);
302    ok(colF.length == 0);
303    equals(null, e.collection);
304  });
305
306  test("Colllection: non-persisted model destroy removes from all collections", function() {
307    var e = new Backbone.Model({title: 'Othello'});
308    e.sync = function(method, model, options) { throw "should not be called"; };
309    var colE = new Backbone.Collection([e]);
310    var colF = new Backbone.Collection([e]);
311    e.destroy();
312    ok(colE.length == 0);
313    ok(colF.length == 0);
314    equals(null, e.collection);
315  });
316
317  test("Collection: fetch", function() {
318    col.fetch();
319    equals(lastRequest[0], 'read');
320    equals(lastRequest[1], col);
321    equals(lastRequest[2].parse, true);
322
323    col.fetch({parse: false});
324    equals(lastRequest[2].parse, false);
325  });
326
327  test("Collection: create", function() {
328    var model = col.create({label: 'f'});
329    equals(lastRequest[0], 'create');
330    equals(lastRequest[1], model);
331    equals(model.get('label'), 'f');
332    equals(model.collection, col);
333  });
334
335  test("Collection: create enforces validation", function() {
336    var ValidatingModel = Backbone.Model.extend({
337      validate: function(attrs) {
338        return "fail";
339      }
340    });
341    var ValidatingCollection = Backbone.Collection.extend({
342      model: ValidatingModel
343    });
344    var col = new ValidatingCollection();
345    equals(col.create({"foo":"bar"}),false);
346  });
347
348  test("Collection: a failing create runs the error callback", function() {
349    var ValidatingModel = Backbone.Model.extend({
350      validate: function(attrs) {
351        return "fail";
352      }
353    });
354    var ValidatingCollection = Backbone.Collection.extend({
355      model: ValidatingModel
356    });
357    var flag = false;
358    var callback = function(model, error) { flag = true; };
359    var col = new ValidatingCollection();
360    col.create({"foo":"bar"}, { error: callback });
361    equals(flag, true);
362  });
363
364  test("collection: initialize", function() {
365    var Collection = Backbone.Collection.extend({
366      initialize: function() {
367        this.one = 1;
368      }
369    });
370    var coll = new Collection;
371    equals(coll.one, 1);
372  });
373
374  test("Collection: toJSON", function() {
375    equals(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
376  });
377
378  test("Collection: Underscore methods", function() {
379    equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
380    equals(col.any(function(model){ return model.id === 100; }), false);
381    equals(col.any(function(model){ return model.id === 0; }), true);
382    equals(col.indexOf(b), 2);
383    equals(col.size(), 4);
384    equals(col.rest().length, 3);
385    ok(!_.include(col.rest()), a);
386    ok(!_.include(col.rest()), d);
387    ok(!col.isEmpty());
388    ok(!_.include(col.without(d)), d);
389    equals(col.max(function(model){ return model.id; }).id, 3);
390    equals(col.min(function(model){ return model.id; }).id, 0);
391    same(col.chain()
392            .filter(function(o){ return o.id % 2 === 0; })
393            .map(function(o){ return o.id * 2; })
394            .value(),
395         [0, 4]);
396  });
397
398  test("Collection: reset", function() {
399    var resetCount = 0;
400    var models = col.models;
401    col.bind('reset', function() { resetCount += 1; });
402    col.reset([]);
403    equals(resetCount, 1);
404    equals(col.length, 0);
405    equals(col.last(), null);
406    col.reset(models);
407    equals(resetCount, 2);
408    equals(col.length, 4);
409    equals(col.last(), a);
410    col.reset(_.map(models, function(m){ return m.attributes; }));
411    equals(resetCount, 3);
412    equals(col.length, 4);
413    ok(col.last() !== a);
414    ok(_.isEqual(col.last().attributes, a.attributes));
415  });
416
417  test("Collection: trigger custom events on models", function() {
418    var fired = null;
419    a.bind("custom", function() { fired = true; });
420    a.trigger("custom");
421    equals(fired, true);
422  });
423
424});