PageRenderTime 56ms CodeModel.GetById 2ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 0ms

/test/model.js

https://github.com/cdata/backbone-nested
JavaScript | 797 lines | 702 code | 89 blank | 6 comment | 12 complexity | 061bc7dcd0d39806ef23e0c2d65970b8 MD5 | raw file
  1// yanked from Backbone 0.9.2 test suite
  2$(document).ready(function() {
  3
  4  // test Backbone.NestedModel instead of Backbone.Model - reset at end of function
  5  var oldModel = Backbone.Model;
  6  Backbone.Model = Backbone.NestedModel;
  7
  8
  9  // Variable to catch the last request.
 10  var lastRequest = null;
 11  // Variable to catch ajax params.
 12  var ajaxParams = null;
 13  var sync = Backbone.sync;
 14  var ajax = $.ajax;
 15  var urlRoot = null;
 16
 17  var proxy = Backbone.Model.extend();
 18  var klass = Backbone.Collection.extend({
 19    url : function() { return '/collection'; }
 20  });
 21  var doc, collection;
 22
 23  module("Backbone.Model", {
 24
 25    setup: function() {
 26      doc = new proxy({
 27        id     : '1-the-tempest',
 28        title  : "The Tempest",
 29        author : "Bill Shakespeare",
 30        length : 123
 31      });
 32      collection = new klass();
 33      collection.add(doc);
 34
 35      Backbone.sync = function(method, model, options) {
 36        lastRequest = {
 37          method: method,
 38          model: model,
 39          options: options
 40        };
 41        sync.apply(this, arguments);
 42      };
 43      $.ajax = function(params) { ajaxParams = params; };
 44      urlRoot = Backbone.Model.prototype.urlRoot;
 45      Backbone.Model.prototype.urlRoot = '/';
 46    },
 47
 48    teardown: function() {
 49      Backbone.sync = sync;
 50      $.ajax = ajax;
 51      Backbone.Model.prototype.urlRoot = urlRoot;
 52    }
 53
 54  });
 55
 56  test("Model: initialize", function() {
 57    var Model = Backbone.Model.extend({
 58      initialize: function() {
 59        this.one = 1;
 60        equal(this.collection, collection);
 61      }
 62    });
 63    var model = new Model({}, {collection: collection});
 64    equal(model.one, 1);
 65    equal(model.collection, collection);
 66  });
 67
 68  test("Model: initialize with attributes and options", function() {
 69    var Model = Backbone.Model.extend({
 70      initialize: function(attributes, options) {
 71        this.one = options.one;
 72      }
 73    });
 74    var model = new Model({}, {one: 1});
 75    equal(model.one, 1);
 76  });
 77
 78  test("Model: initialize with parsed attributes", function() {
 79    var Model = Backbone.Model.extend({
 80      parse: function(obj) {
 81        obj.value += 1;
 82        return obj;
 83      }
 84    });
 85    var model = new Model({value: 1}, {parse: true});
 86    equal(model.get('value'), 2);
 87  });
 88
 89  test("Model: url", function() {
 90    doc.urlRoot = null;
 91    equal(doc.url(), '/collection/1-the-tempest');
 92    doc.collection.url = '/collection/';
 93    equal(doc.url(), '/collection/1-the-tempest');
 94    doc.collection = null;
 95    raises(function() { doc.url(); });
 96    doc.collection = collection;
 97  });
 98
 99  test("Model: url when using urlRoot, and uri encoding", function() {
100    var Model = Backbone.Model.extend({
101      urlRoot: '/collection'
102    });
103    var model = new Model();
104    equal(model.url(), '/collection');
105    model.set({id: '+1+'});
106    equal(model.url(), '/collection/%2B1%2B');
107  });
108
109  test("Model: url when using urlRoot as a function to determine urlRoot at runtime", function() {
110    var Model = Backbone.Model.extend({
111      urlRoot: function() {
112        return '/nested/' + this.get('parent_id') + '/collection';
113      }
114    });
115
116    var model = new Model({parent_id: 1});
117    equal(model.url(), '/nested/1/collection');
118    model.set({id: 2});
119    equal(model.url(), '/nested/1/collection/2');
120  });
121
122  test("Model: clone", function() {
123    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
124    var b = a.clone();
125    equal(a.get('foo'), 1);
126    equal(a.get('bar'), 2);
127    equal(a.get('baz'), 3);
128    equal(b.get('foo'), a.get('foo'), "Foo should be the same on the clone.");
129    equal(b.get('bar'), a.get('bar'), "Bar should be the same on the clone.");
130    equal(b.get('baz'), a.get('baz'), "Baz should be the same on the clone.");
131    a.set({foo : 100});
132    equal(a.get('foo'), 100);
133    equal(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
134  });
135
136  test("Model: isNew", function() {
137    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
138    ok(a.isNew(), "it should be new");
139    a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 });
140    ok(!a.isNew(), "any defined ID is legal, negative or positive");
141    a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 });
142    ok(!a.isNew(), "any defined ID is legal, including zero");
143    ok( new Backbone.Model({          }).isNew(), "is true when there is no id");
144    ok(!new Backbone.Model({ 'id': 2  }).isNew(), "is false for a positive integer");
145    ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integer");
146  });
147
148  test("Model: get", function() {
149    equal(doc.get('title'), 'The Tempest');
150    equal(doc.get('author'), 'Bill Shakespeare');
151  });
152
153  test("Model: escape", function() {
154    equal(doc.escape('title'), 'The Tempest');
155    doc.set({audience: 'Bill & Bob'});
156    equal(doc.escape('audience'), 'Bill & Bob');
157    doc.set({audience: 'Tim > Joan'});
158    equal(doc.escape('audience'), 'Tim > Joan');
159    doc.set({audience: 10101});
160    equal(doc.escape('audience'), '10101');
161    doc.unset('audience');
162    equal(doc.escape('audience'), '');
163  });
164
165  test("Model: has", function() {
166    var a = new Backbone.Model();
167    equal(a.has("name"), false);
168    _([true, "Truth!", 1, false, '', 0]).each(function(value) {
169      a.set({'name': value});
170      equal(a.has("name"), true);
171    });
172    a.unset('name');
173    equal(a.has('name'), false);
174    _([null, undefined]).each(function(value) {
175      a.set({'name': value});
176      equal(a.has("name"), false);
177    });
178  });
179
180  test("Model: set and unset", function() {
181    expect(8);
182    var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
183    var changeCount = 0;
184    a.on("change:foo", function() { changeCount += 1; });
185    a.set({'foo': 2});
186    ok(a.get('foo') == 2, "Foo should have changed.");
187    ok(changeCount == 1, "Change count should have incremented.");
188    a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
189    ok(a.get('foo') == 2, "Foo should NOT have changed, still 2");
190    ok(changeCount == 1, "Change count should NOT have incremented.");
191
192    a.validate = function(attrs) {
193      equal(attrs.foo, void 0, "don't ignore values when unsetting");
194    };
195    a.unset('foo');
196    equal(a.get('foo'), void 0, "Foo should have changed");
197    delete a.validate;
198    ok(changeCount == 2, "Change count should have incremented for unset.");
199
200    a.unset('id');
201    equal(a.id, undefined, "Unsetting the id should remove the id property.");
202  });
203
204  test("Model: multiple unsets", function() {
205    var i = 0;
206    var counter = function(){ i++; };
207    var model = new Backbone.Model({a: 1});
208    model.on("change:a", counter);
209    model.set({a: 2});
210    model.unset('a');
211    model.unset('a');
212    equal(i, 2, 'Unset does not fire an event for missing attributes.');
213  });
214
215  test("Model: unset and changedAttributes", function() {
216    var model = new Backbone.Model({a: 1});
217    model.unset('a', {silent: true});
218    var changedAttributes = model.changedAttributes();
219    ok('a' in changedAttributes, 'changedAttributes should contain unset properties');
220
221    changedAttributes = model.changedAttributes();
222    ok('a' in changedAttributes, 'changedAttributes should contain unset properties when running changedAttributes again after an unset.');
223  });
224
225  test("Model: using a non-default id attribute.", function() {
226    var MongoModel = Backbone.Model.extend({idAttribute : '_id'});
227    var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
228    equal(model.get('id'), 'eye-dee');
229    equal(model.id, 25);
230    equal(model.isNew(), false);
231    model.unset('_id');
232    equal(model.id, undefined);
233    equal(model.isNew(), true);
234  });
235
236  test("Model: set an empty string", function() {
237    var model = new Backbone.Model({name : "Model"});
238    model.set({name : ''});
239    equal(model.get('name'), '');
240  });
241
242  test("Model: clear", function() {
243    var changed;
244    var model = new Backbone.Model({id: 1, name : "Model"});
245    model.on("change:name", function(){ changed = true; });
246    model.on("change", function() {
247      var changedAttrs = model.changedAttributes();
248      ok('name' in changedAttrs);
249    });
250    model.clear();
251    equal(changed, true);
252    equal(model.get('name'), undefined);
253  });
254
255  test("Model: defaults", function() {
256    var Defaulted = Backbone.Model.extend({
257      defaults: {
258        "one": 1,
259        "two": 2
260      }
261    });
262    var model = new Defaulted({two: null});
263    equal(model.get('one'), 1);
264    equal(model.get('two'), null);
265    Defaulted = Backbone.Model.extend({
266      defaults: function() {
267        return {
268          "one": 3,
269          "two": 4
270        };
271      }
272    });
273    var model = new Defaulted({two: null});
274    equal(model.get('one'), 3);
275    equal(model.get('two'), null);
276  });
277
278  test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
279    var model = new Backbone.Model({name : "Tim", age : 10});
280    equal(model.changedAttributes(), false);
281    model.on('change', function() {
282      ok(model.hasChanged('name'), 'name changed');
283      ok(!model.hasChanged('age'), 'age did not');
284      ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
285      equal(model.previous('name'), 'Tim');
286      ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
287    });
288    model.set({name : 'Rob'}, {silent : true});
289    equal(model.hasChanged(), true);
290    equal(model.hasChanged('name'), true);
291    model.change();
292    equal(model.get('name'), 'Rob');
293  });
294
295  test("Model: changedAttributes", function() {
296    var model = new Backbone.Model({a: 'a', b: 'b'});
297    equal(model.changedAttributes(), false);
298    equal(model.changedAttributes({a: 'a'}), false);
299    equal(model.changedAttributes({a: 'b'}).a, 'b');
300  });
301
302  test("Model: change with options", function() {
303    var value;
304    var model = new Backbone.Model({name: 'Rob'});
305    model.on('change', function(model, options) {
306      value = options.prefix + model.get('name');
307    });
308    model.set({name: 'Bob'}, {silent: true});
309    model.change({prefix: 'Mr. '});
310    equal(value, 'Mr. Bob');
311    model.set({name: 'Sue'}, {prefix: 'Ms. '});
312    equal(value, 'Ms. Sue');
313  });
314
315  test("Model: change after initialize", function () {
316    var changed = 0;
317    var attrs = {id: 1, label: 'c'};
318    var obj = new Backbone.Model(attrs);
319    obj.on('change', function() { changed += 1; });
320    obj.set(attrs);
321    equal(changed, 0);
322  });
323
324  test("Model: save within change event", function () {
325    var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
326    model.on('change', function () {
327      model.save();
328      ok(_.isEqual(lastRequest.model, model));
329    });
330    model.set({lastName: 'Hicks'});
331  });
332
333  test("Model: validate after save", function() {
334    var lastError, model = new Backbone.Model();
335    model.validate = function(attrs) {
336      if (attrs.admin) return "Can't change admin status.";
337    };
338    model.sync = function(method, model, options) {
339      options.success.call(this, {admin: true});
340    };
341    model.save(null, {error: function(model, error) {
342      lastError = error;
343    }});
344
345    equal(lastError, "Can't change admin status.");
346  });
347
348  test("Model: isValid", function() {
349    var model = new Backbone.Model({valid: true});
350    model.validate = function(attrs) {
351      if (!attrs.valid) return "invalid";
352    };
353    equal(model.isValid(), true);
354    equal(model.set({valid: false}), false);
355    equal(model.isValid(), true);
356    ok(model.set('valid', false, {silent: true}));
357    equal(model.isValid(), false);
358  });
359
360  test("Model: save", function() {
361    doc.save({title : "Henry V"});
362    equal(lastRequest.method, 'update');
363    ok(_.isEqual(lastRequest.model, doc));
364  });
365
366  test("Model: save in positional style", function() {
367    var model = new Backbone.Model();
368    model.sync = function(method, model, options) {
369      options.success();
370    };
371    model.save('title', 'Twelfth Night');
372    equal(model.get('title'), 'Twelfth Night');
373  });
374
375
376
377  test("Model: fetch", function() {
378    doc.fetch();
379    equal(lastRequest.method, 'read');
380    ok(_.isEqual(lastRequest.model, doc));
381  });
382
383  test("Model: destroy", function() {
384    doc.destroy();
385    equal(lastRequest.method, 'delete');
386    ok(_.isEqual(lastRequest.model, doc));
387
388    var newModel = new Backbone.Model;
389    equal(newModel.destroy(), false);
390  });
391
392  test("Model: non-persisted destroy", function() {
393    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
394    a.sync = function() { throw "should not be called"; };
395    a.destroy();
396    ok(true, "non-persisted model should not call sync");
397  });
398
399  test("Model: validate", function() {
400    var lastError;
401    var model = new Backbone.Model();
402    model.validate = function(attrs) {
403      if (attrs.admin != this.get('admin')) return "Can't change admin status.";
404    };
405    model.on('error', function(model, error) {
406      lastError = error;
407    });
408    var result = model.set({a: 100});
409    equal(result, model);
410    equal(model.get('a'), 100);
411    equal(lastError, undefined);
412    result = model.set({admin: true}, {silent: true});
413    equal(model.get('admin'), true);
414    result = model.set({a: 200, admin: false});
415    equal(lastError, "Can't change admin status.");
416    equal(result, false);
417    equal(model.get('a'), 100);
418  });
419
420  test("Model: validate on unset and clear", function() {
421    var error;
422    var model = new Backbone.Model({name: "One"});
423    model.validate = function(attrs) {
424      if (!attrs.name) {
425        error = true;
426        return "No thanks.";
427      }
428    };
429    model.set({name: "Two"});
430    equal(model.get('name'), 'Two');
431    equal(error, undefined);
432    model.unset('name');
433    equal(error, true);
434    equal(model.get('name'), 'Two');
435    model.clear();
436    equal(model.get('name'), 'Two');
437    delete model.validate;
438    model.clear();
439    equal(model.get('name'), undefined);
440  });
441
442  test("Model: validate with error callback", function() {
443    var lastError, boundError;
444    var model = new Backbone.Model();
445    model.validate = function(attrs) {
446      if (attrs.admin) return "Can't change admin status.";
447    };
448    var callback = function(model, error) {
449      lastError = error;
450    };
451    model.on('error', function(model, error) {
452      boundError = true;
453    });
454    var result = model.set({a: 100}, {error: callback});
455    equal(result, model);
456    equal(model.get('a'), 100);
457    equal(lastError, undefined);
458    equal(boundError, undefined);
459    result = model.set({a: 200, admin: true}, {error: callback});
460    equal(result, false);
461    equal(model.get('a'), 100);
462    equal(lastError, "Can't change admin status.");
463    equal(boundError, undefined);
464  });
465
466  test("Model: defaults always extend attrs (#459)", function() {
467    var Defaulted = Backbone.Model.extend({
468      defaults: {one: 1},
469      initialize : function(attrs, opts) {
470        equal(this.attributes.one, 1);
471      }
472    });
473    var providedattrs = new Defaulted({});
474    var emptyattrs = new Defaulted();
475  });
476
477  test("Model: Inherit class properties", function() {
478    var Parent = Backbone.Model.extend({
479      instancePropSame: function() {},
480      instancePropDiff: function() {}
481    }, {
482      classProp: function() {}
483    });
484    var Child = Parent.extend({
485      instancePropDiff: function() {}
486    });
487
488    var adult = new Parent;
489    var kid   = new Child;
490
491    equal(Child.classProp, Parent.classProp);
492    notEqual(Child.classProp, undefined);
493
494    equal(kid.instancePropSame, adult.instancePropSame);
495    notEqual(kid.instancePropSame, undefined);
496
497    notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
498    notEqual(Child.prototype.instancePropDiff, undefined);
499  });
500
501  test("Model: Nested change events don't clobber previous attributes", function() {
502    var A = Backbone.Model.extend({
503      initialize: function() {
504        this.on("change:state", function(a, newState) {
505          equal(a.previous('state'), undefined);
506          equal(newState, 'hello');
507          // Fire a nested change event.
508          this.set({ other: "whatever" });
509        });
510      }
511    });
512
513    var B = Backbone.Model.extend({
514      initialize: function() {
515        this.get("a").on("change:state", function(a, newState) {
516          equal(a.previous('state'), undefined);
517          equal(newState, 'hello');
518        });
519      }
520    });
521
522    var a = new A();
523    var b = new B({a: a});
524    a.set({state: 'hello'});
525  });
526
527  test("hasChanged/set should use same comparison", function() {
528    expect(2);
529    var changed = 0, model = new Backbone.Model({a: null});
530    model.on('change', function() {
531      ok(this.hasChanged('a'));
532    })
533    .on('change:a', function() {
534      changed++;
535    })
536    .set({a: undefined});
537    equal(changed, 1);
538  });
539
540  test("#582, #425, change:attribute callbacks should fire after all changes have occurred", 9, function() {
541    var model = new Backbone.Model;
542
543    var assertion = function() {
544      equal(model.get('a'), 'a');
545      equal(model.get('b'), 'b');
546      equal(model.get('c'), 'c');
547    };
548
549    model.on('change:a', assertion);
550    model.on('change:b', assertion);
551    model.on('change:c', assertion);
552
553    model.set({a: 'a', b: 'b', c: 'c'});
554  });
555
556  test("#871, set with attributes property", function() {
557    var model = new Backbone.Model();
558    model.set({attributes: true});
559    ok(model.has('attributes'));
560  });
561
562  test("set value regardless of equality/change", function() {
563    var model = new Backbone.Model({x: []});
564    var a = [];
565    model.set({x: a});
566    ok(model.get('x') === a);
567  });
568
569  test("unset fires change for undefined attributes", 1, function() {
570    var model = new Backbone.Model({x: undefined});
571    model.on('change:x', function(){ ok(true); });
572    model.unset('x');
573  });
574
575  test("set: undefined values", function() {
576    var model = new Backbone.Model({x: undefined});
577    ok('x' in model.attributes);
578  });
579
580  test("change fires change:attr", 1, function() {
581    var model = new Backbone.Model({x: 1});
582    model.set({x: 2}, {silent: true});
583    model.on('change:x', function(){ ok(true); });
584    model.change();
585  });
586
587  test("hasChanged is false after original values are set", function() {
588    var model = new Backbone.Model({x: 1});
589    model.on('change:x', function(){ ok(false); });
590    model.set({x: 2}, {silent: true});
591    ok(model.hasChanged());
592    model.set({x: 1}, {silent: true});
593    ok(!model.hasChanged());
594  });
595
596  test("save with `wait` succeeds without `validate`", function() {
597    var model = new Backbone.Model();
598    model.save({x: 1}, {wait: true});
599    ok(lastRequest.model === model);
600  });
601
602  test("`hasChanged` for falsey keys", function() {
603    var model = new Backbone.Model();
604    model.set({x: true}, {silent: true});
605    ok(!model.hasChanged(0));
606    ok(!model.hasChanged(''));
607  });
608
609  test("`previous` for falsey keys", function() {
610    var model = new Backbone.Model({0: true, '': true});
611    model.set({0: false, '': false}, {silent: true});
612    equal(model.previous(0), true);
613    equal(model.previous(''), true);
614  });
615
616  test("`save` with `wait` sends correct attributes", function() {
617    var changed = 0;
618    var model = new Backbone.Model({x: 1, y: 2});
619    model.on('change:x', function() { changed++; });
620    model.save({x: 3}, {wait: true});
621    deepEqual(JSON.parse(ajaxParams.data), {x: 3, y: 2});
622    equal(model.get('x'), 1);
623    equal(changed, 0);
624    lastRequest.options.success({});
625    equal(model.get('x'), 3);
626    equal(changed, 1);
627  });
628
629  test("`save` with `wait` results in correct attributes if success is called during sync", function() {
630    var changed = 0;
631    var model = new Backbone.Model({x: 1, y: 2});
632    model.sync = function(method, model, options) {
633      options.success();
634    };
635    model.on("change:x", function() { changed++; });
636    model.save({x: 3}, {wait: true});
637    equal(model.get('x'), 3);
638    equal(changed, 1);
639  });
640
641  test("save with wait validates attributes", 1, function() {
642    var model = new Backbone.Model();
643    model.validate = function() { ok(true); };
644    model.save({x: 1}, {wait: true});
645  });
646
647  test("nested `set` during `'change:attr'`", function() {
648    var events = [];
649    var model = new Backbone.Model();
650    model.on('all', function(event) { events.push(event); });
651    model.on('change', function() {
652      model.set({z: true}, {silent:true});
653    });
654    model.on('change:x', function() {
655      model.set({y: true});
656    });
657    model.set({x: true});
658    deepEqual(events, ['change:y', 'change:x', 'change']);
659    events = [];
660    model.change();
661    deepEqual(events, ['change:z', 'change']);
662  });
663
664  test("nested `change` only fires once", 1, function() {
665    var model = new Backbone.Model();
666    model.on('change', function() {
667      ok(true);
668      model.change();
669    });
670    model.set({x: true});
671  });
672
673  test("no `'change'` event if no changes", function() {
674    var model = new Backbone.Model();
675    model.on('change', function() { ok(false); });
676    model.change();
677  });
678
679  test("nested `set` during `'change'`", 6, function() {
680    var count = 0;
681    var model = new Backbone.Model();
682    model.on('change', function() {
683      switch(count++) {
684        case 0:
685          deepEqual(this.changedAttributes(), {x: true});
686          equal(model.previous('x'), undefined);
687          model.set({y: true});
688          break;
689        case 1:
690          deepEqual(this.changedAttributes(), {y: true});
691          equal(model.previous('x'), true);
692          model.set({z: true});
693          break;
694        case 2:
695          deepEqual(this.changedAttributes(), {z: true});
696          equal(model.previous('y'), true);
697          break;
698        default:
699          ok(false);
700      }
701    });
702    model.set({x: true});
703  });
704
705  test("nested `'change'` with silent", 3, function() {
706    var count = 0;
707    var model = new Backbone.Model();
708    model.on('change:y', function() { ok(true); });
709    model.on('change', function() {
710      switch(count++) {
711        case 0:
712          deepEqual(this.changedAttributes(), {x: true});
713          model.set({y: true}, {silent: true});
714          break;
715        case 1:
716          deepEqual(this.changedAttributes(), {y: true, z: true});
717          break;
718        default:
719          ok(false);
720      }
721    });
722    model.set({x: true});
723    model.set({z: true});
724  });
725
726  test("nested `'change:attr'` with silent", 1, function() {
727    var model = new Backbone.Model();
728    model.on('change:y', function(){ ok(true); });
729    model.on('change', function() {
730      model.set({y: true}, {silent: true});
731      model.set({z: true});
732    });
733    model.set({x: true});
734  });
735
736  test("multiple nested changes with silent", 1, function() {
737    var model = new Backbone.Model();
738    model.on('change:x', function() {
739      model.set({y: 1}, {silent: true});
740      model.set({y: 2});
741    });
742    model.on('change:y', function(model, val) {
743      equal(val, 2);
744    });
745    model.set({x: true});
746    model.change();
747  });
748
749  test("multiple nested changes with silent", function() {
750    var changes = [];
751    var model = new Backbone.Model();
752    model.on('change:b', function(model, val) { changes.push(val); });
753    model.on('change', function() {
754      model.set({b: 1});
755      model.set({b: 2}, {silent: true});
756    });
757    model.set({b: 0});
758    deepEqual(changes, [0, 1, 1]);
759    model.change();
760    deepEqual(changes, [0, 1, 1, 2, 1]);
761  });
762
763  test("nested set multiple times", 1, function() {
764    var model = new Backbone.Model();
765    model.on('change:b', function() {
766      ok(true);
767    });
768    model.on('change:a', function() {
769      model.set({b: true});
770      model.set({b: true});
771    });
772    model.set({a: true});
773  });
774
775  test("Backbone.wrapError triggers `'error'`", 12, function() {
776    var resp = {};
777    var options = {};
778    var model = new Backbone.Model();
779    model.on('error', error);
780    var callback = Backbone.wrapError(null, model, options);
781    callback(model, resp);
782    callback(resp);
783    callback = Backbone.wrapError(error, model, options);
784    callback(model, resp);
785    callback(resp);
786    function error(_model, _resp, _options) {
787      ok(model === _model);
788      ok(resp === _resp);
789      ok(options === _options);
790    }
791  });
792
793
794  // reset the Model
795  Backbone.Model = oldModel;
796  
797});