backbone /test/collection.js

Language Javascript Lines 407
MD5 Hash 7458c805096e5a310d5dfb67ffaaa07a
Repository https://github.com/parkerproject/backbone.git View Raw File
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
$(document).ready(function() {

  module("Backbone.Collection");

  window.lastRequest = null;

  Backbone.sync = function() {
    lastRequest = _.toArray(arguments);
  };

  var a         = new Backbone.Model({id: 3, label: 'a'});
  var b         = new Backbone.Model({id: 2, label: 'b'});
  var c         = new Backbone.Model({id: 1, label: 'c'});
  var d         = new Backbone.Model({id: 0, label: 'd'});
  var e         = null;
  var col       = new Backbone.Collection([a,b,c,d]);
  var otherCol  = new Backbone.Collection();

  test("Collection: new and sort", function() {
    equals(col.first(), a, "a should be first");
    equals(col.last(), d, "d should be last");
    col.comparator = function(model) { return model.id; };
    col.sort();
    equals(col.first(), d, "d should be first");
    equals(col.last(), a, "a should be last");
    equals(col.length, 4);
  });

  test("Collection: get, getByCid", function() {
    equals(col.get(0), d);
    equals(col.get(2), b);
    equals(col.getByCid(col.first().cid), col.first());
  });

  test("Collection: get with non-default ids", function() {
    var col = new Backbone.Collection();
    var MongoModel = Backbone.Model.extend({
      idAttribute: '_id'
    });
    var model = new MongoModel({_id: 100});
    col.add(model);
    equals(col.get(100), model);
    model.set({_id: 101});
    equals(col.get(101), model);
  });

  test("Collection: add model with attributes modified by set", function() {
    var CustomSetModel = Backbone.Model.extend({
      defaults: {
        number_as_string: null //presence of defaults forces extend
      },

      validate: function (attributes) {
        if (!_.isString(attributes.num_as_string)) {
          return 'fail';
        }
      },

      set: function (attributes, options) {
        if (attributes.num_as_string) {
          attributes.num_as_string = attributes.num_as_string.toString();
        }
        Backbone.Model.prototype.set.call(this, attributes, options);
      }
    });

    var CustomSetCollection = Backbone.Collection.extend({
      model: CustomSetModel
    });
    var col = new CustomSetCollection([{ num_as_string: 2 }]);
    equals(col.length, 1);
  });

  test("Collection: update index when id changes", function() {
    var col = new Backbone.Collection();
    col.add([
      {id : 0, name : 'one'},
      {id : 1, name : 'two'}
    ]);
    var one = col.get(0);
    equals(one.get('name'), 'one');
    one.set({id : 101});
    equals(col.get(0), null);
    equals(col.get(101).get('name'), 'one');
  });

  test("Collection: at", function() {
    equals(col.at(2), b);
  });

  test("Collection: pluck", function() {
    equals(col.pluck('label').join(' '), 'd c b a');
  });

  test("Collection: add", function() {
    var added = opts = secondAdded = null;
    e = new Backbone.Model({id: 10, label : 'e'});
    otherCol.add(e);
    otherCol.bind('add', function() {
      secondAdded = true;
    });
    col.bind('add', function(model, collection, options){
      added = model.get('label');
      opts = options;
    });
    col.add(e, {amazing: true});
    equals(added, 'e');
    equals(col.length, 5);
    equals(col.last(), e);
    equals(otherCol.length, 1);
    equals(secondAdded, null);
    ok(opts.amazing);

    var f = new Backbone.Model({id: 20, label : 'f'});
    var g = new Backbone.Model({id: 21, label : 'g'});
    var h = new Backbone.Model({id: 22, label : 'h'});
    var atCol = new Backbone.Collection([f, g, h]);
    equals(atCol.length, 3);
    atCol.add(e, {at: 1});
    equals(atCol.length, 4);
    equals(atCol.at(1), e);
    equals(atCol.last(), h);
  });

  test("Collection: add model to collection and verify index updates", function() {
    var f = new Backbone.Model({id: 20, label : 'f'});
    var g = new Backbone.Model({id: 21, label : 'g'});
    var h = new Backbone.Model({id: 22, label : 'h'});
    var col = new Backbone.Collection();
    
    var counts = [];
     
    col.bind('add', function(model, collection, options) {
      counts.push(options.index);  
    });
    col.add(f); 
    col.add(g); 
    col.add(h); 
    ok(_.isEqual(counts, [0,1,2]));
  });

  test("Collection: add model to collection twice", function() {
    try {
      // no id, same cid
      var a2 = new Backbone.Model({label: a.label});
      a2.cid = a.cid;
      col.add(a2);
      ok(false, "duplicate; expected add to fail");
    } catch (e) {
      equals(e.message, "Can't add the same model to a set twice,3");
    }
  });

  test("Collection: add model to multiple collections", function() {
    var counter = 0;
    var e = new Backbone.Model({id: 10, label : 'e'});
    e.bind('add', function(model, collection) {
      counter++;
      equals(e, model);
      if (counter > 1) {
        equals(collection, colF);
      } else {
        equals(collection, colE);
      }
    });
    var colE = new Backbone.Collection([]);
    colE.bind('add', function(model, collection) {
      equals(e, model);
      equals(colE, collection);
    });
    var colF = new Backbone.Collection([]);
    colF.bind('add', function(model, collection) {
      equals(e, model);
      equals(colF, collection);
    });
    colE.add(e);
    equals(e.collection, colE);
    colF.add(e);
    equals(e.collection, colE);
  });

  test("Collection: remove", function() {
    var removed = otherRemoved = null;
    col.bind('remove', function(model){ removed = model.get('label'); });
    otherCol.bind('remove', function(){ otherRemoved = true; });
    col.remove(e);
    equals(removed, 'e');
    equals(col.length, 4);
    equals(col.first(), d);
    equals(otherRemoved, null);
  });

  test("Collection: remove should return correct index events", function() {
    var f = new Backbone.Model({id: 20, label : 'f'});
    var g = new Backbone.Model({id: 21, label : 'g'});
    var h = new Backbone.Model({id: 22, label : 'h'});
    var col = new Backbone.Collection([f,g,h]);
    
    var counts = [];
     
    col.bind('remove', function(model, collection, options) {
      counts.push(options.index);  
    });
    col.remove(h); 
    col.remove(g); 
    col.remove(f); 
    ok(_.isEqual(counts, [2,1,0]));
  });

  test("Collection: events are unbound on remove", function() {
    var counter = 0;
    var dj = new Backbone.Model();
    var emcees = new Backbone.Collection([dj]);
    emcees.bind('change', function(){ counter++; });
    dj.set({name : 'Kool'});
    equals(counter, 1);
    emcees.reset([]);
    equals(dj.collection, undefined);
    dj.set({name : 'Shadow'});
    equals(counter, 1);
  });

  test("Collection: remove in multiple collections", function() {
    var modelData = {
      id : 5,
      title : 'Othello'
    };
    var passed = false;
    var e = new Backbone.Model(modelData);
    var f = new Backbone.Model(modelData);
    f.bind('remove', function() {
      passed = true;
    });
    var colE = new Backbone.Collection([e]);
    var colF = new Backbone.Collection([f]);
    ok(e != f);
    ok(colE.length == 1);
    ok(colF.length == 1);
    colE.remove(e);
    equals(passed, false);
    ok(colE.length == 0);
    colF.remove(e);
    ok(colF.length == 0);
    equals(passed, true);
  });

  test("Collection: remove same model in multiple collection", function() {
    var counter = 0;
    var e = new Backbone.Model({id: 5, title: 'Othello'});
    e.bind('remove', function(model, collection) {
      counter++;
      equals(e, model);
      if (counter > 1) {
        equals(collection, colE);
      } else {
        equals(collection, colF);
      }
    });
    var colE = new Backbone.Collection([e]);
    colE.bind('remove', function(model, collection) {
      equals(e, model);
      equals(colE, collection);
    });
    var colF = new Backbone.Collection([e]);
    colF.bind('remove', function(model, collection) {
      equals(e, model);
      equals(colF, collection);
    });
    equals(colE, e.collection);
    colF.remove(e);
    ok(colF.length == 0);
    ok(colE.length == 1);
    equals(counter, 1);
    equals(colE, e.collection);
    colE.remove(e);
    equals(null, e.collection);
    ok(colE.length == 0);
    equals(counter, 2);
  });

  test("Collection: model destroy removes from all collections", function() {
    var e = new Backbone.Model({id: 5, title: 'Othello'});
    e.sync = function(method, model, options) { options.success({}); };
    var colE = new Backbone.Collection([e]);
    var colF = new Backbone.Collection([e]);
    e.destroy();
    ok(colE.length == 0);
    ok(colF.length == 0);
    equals(null, e.collection);
  });

  test("Colllection: non-persisted model destroy removes from all collections", function() {
    var e = new Backbone.Model({title: 'Othello'});
    e.sync = function(method, model, options) { throw "should not be called"; };
    var colE = new Backbone.Collection([e]);
    var colF = new Backbone.Collection([e]);
    e.destroy();
    ok(colE.length == 0);
    ok(colF.length == 0);
    equals(null, e.collection);
  });

  test("Collection: fetch", function() {
    col.fetch();
    equals(lastRequest[0], 'read');
    equals(lastRequest[1], col);
  });

  test("Collection: create", function() {
    var model = col.create({label: 'f'});
    equals(lastRequest[0], 'create');
    equals(lastRequest[1], model);
    equals(model.get('label'), 'f');
    equals(model.collection, col);
  });

  test("Collection: create enforces validation", function() {
    var ValidatingModel = Backbone.Model.extend({
      validate: function(attrs) {
        return "fail";
      }
    });
    var ValidatingCollection = Backbone.Collection.extend({
      model: ValidatingModel
    });
    var col = new ValidatingCollection();
    equals(col.create({"foo":"bar"}),false);
  });

  test("Collection: a failing create runs the error callback", function() {
    var ValidatingModel = Backbone.Model.extend({
      validate: function(attrs) {
        return "fail";
      }
    });
    var ValidatingCollection = Backbone.Collection.extend({
      model: ValidatingModel
    });
    var flag = false;
    var callback = function(model, error) { flag = true; };
    var col = new ValidatingCollection();
    col.create({"foo":"bar"}, { error: callback });
    equals(flag, true);
  });

  test("collection: initialize", function() {
    var Collection = Backbone.Collection.extend({
      initialize: function() {
        this.one = 1;
      }
    });
    var coll = new Collection;
    equals(coll.one, 1);
  });

  test("Collection: toJSON", function() {
    equals(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
  });

  test("Collection: Underscore methods", function() {
    equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
    equals(col.any(function(model){ return model.id === 100; }), false);
    equals(col.any(function(model){ return model.id === 0; }), true);
    equals(col.indexOf(b), 2);
    equals(col.size(), 4);
    equals(col.rest().length, 3);
    ok(!_.include(col.rest()), a);
    ok(!_.include(col.rest()), d);
    ok(!col.isEmpty());
    ok(!_.include(col.without(d)), d);
    equals(col.max(function(model){ return model.id; }).id, 3);
    equals(col.min(function(model){ return model.id; }).id, 0);
    same(col.chain()
            .filter(function(o){ return o.id % 2 === 0; })
            .map(function(o){ return o.id * 2; })
            .value(),
         [0, 4]);
  });

  test("Collection: reset", function() {
    var resetCount = 0;
    var models = col.models;
    col.bind('reset', function() { resetCount += 1; });
    col.reset([]);
    equals(resetCount, 1);
    equals(col.length, 0);
    equals(col.last(), null);
    col.reset(models);
    equals(resetCount, 2);
    equals(col.length, 4);
    equals(col.last(), a);
    col.reset(_.map(models, function(m){ return m.attributes; }));
    equals(resetCount, 3);
    equals(col.length, 4);
    ok(col.last() !== a);
    ok(_.isEqual(col.last().attributes, a.attributes));
  });

  test("Collection: trigger custom events on models", function() {
    var fired = null;
    a.bind("custom", function() { fired = true; });
    a.trigger("custom");
    equals(fired, true);
  });

});
Back to Top