PageRenderTime 85ms CodeModel.GetById 2ms app.highlight 73ms RepoModel.GetById 1ms app.codeStats 1ms

/test/model.js

https://github.com/notatestuser/backbone-deep-model
JavaScript | 1081 lines | 962 code | 115 blank | 4 comment | 14 complexity | 76813825665b7be7b21d16b14da40354 MD5 | raw file
   1// yanked from Backbone 0.9.10 test suite
   2$(document).ready(function() {
   3
   4  // test Backbone.DeepModel instead of Backbone.Model - reset at end of function
   5  var oldModel = Backbone.Model;
   6  Backbone.Model = Backbone.DeepModel;
   7
   8  var proxy = Backbone.Model.extend();
   9  var klass = Backbone.Collection.extend({
  10    url : function() { return '/collection'; }
  11  });
  12  var doc, collection;
  13
  14  module("Backbone.Model", _.extend(new Environment, {
  15
  16    setup: function() {
  17      Environment.prototype.setup.apply(this, arguments);
  18      doc = new proxy({
  19        id     : '1-the-tempest',
  20        title  : "The Tempest",
  21        author : "Bill Shakespeare",
  22        length : 123
  23      });
  24      collection = new klass();
  25      collection.add(doc);
  26    }
  27
  28  }));
  29
  30  test("initialize", 3, function() {
  31    var Model = Backbone.Model.extend({
  32      initialize: function() {
  33        this.one = 1;
  34        equal(this.collection, collection);
  35      }
  36    });
  37    var model = new Model({}, {collection: collection});
  38    equal(model.one, 1);
  39    equal(model.collection, collection);
  40  });
  41
  42  test("initialize with attributes and options", 1, function() {
  43    var Model = Backbone.Model.extend({
  44      initialize: function(attributes, options) {
  45        this.one = options.one;
  46      }
  47    });
  48    var model = new Model({}, {one: 1});
  49    equal(model.one, 1);
  50  });
  51
  52  test("initialize with parsed attributes", 1, function() {
  53    var Model = Backbone.Model.extend({
  54      parse: function(obj) {
  55        obj.value += 1;
  56        return obj;
  57      }
  58    });
  59    var model = new Model({value: 1}, {parse: true});
  60    equal(model.get('value'), 2);
  61  });
  62
  63  test("initialize with defaults", 2, function() {
  64    var Model = Backbone.Model.extend({
  65      defaults: {
  66        first_name: 'Unknown',
  67        last_name: 'Unknown'
  68      }
  69    });
  70    var model = new Model({'first_name': 'John'});
  71    equal(model.get('first_name'), 'John');
  72    equal(model.get('last_name'), 'Unknown');
  73  });
  74
  75  test("parse can return null", 1, function() {
  76    var Model = Backbone.Model.extend({
  77      parse: function(obj) {
  78        obj.value += 1;
  79        return null;
  80      }
  81    });
  82    var model = new Model({value: 1}, {parse: true});
  83    equal(JSON.stringify(model.toJSON()), "{}");
  84  });
  85
  86  test("url", 3, function() {
  87    doc.urlRoot = null;
  88    equal(doc.url(), '/collection/1-the-tempest');
  89    doc.collection.url = '/collection/';
  90    equal(doc.url(), '/collection/1-the-tempest');
  91    doc.collection = null;
  92    raises(function() { doc.url(); });
  93    doc.collection = collection;
  94  });
  95
  96  test("url when using urlRoot, and uri encoding", 2, function() {
  97    var Model = Backbone.Model.extend({
  98      urlRoot: '/collection'
  99    });
 100    var model = new Model();
 101    equal(model.url(), '/collection');
 102    model.set({id: '+1+'});
 103    equal(model.url(), '/collection/%2B1%2B');
 104  });
 105
 106  test("url when using urlRoot as a function to determine urlRoot at runtime", 2, function() {
 107    var Model = Backbone.Model.extend({
 108      urlRoot: function() {
 109        return '/nested/' + this.get('parent_id') + '/collection';
 110      }
 111    });
 112
 113    var model = new Model({parent_id: 1});
 114    equal(model.url(), '/nested/1/collection');
 115    model.set({id: 2});
 116    equal(model.url(), '/nested/1/collection/2');
 117  });
 118
 119  test("clone", 10, function() {
 120    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
 121    var b = a.clone();
 122    equal(a.get('foo'), 1);
 123    equal(a.get('bar'), 2);
 124    equal(a.get('baz'), 3);
 125    equal(b.get('foo'), a.get('foo'), "Foo should be the same on the clone.");
 126    equal(b.get('bar'), a.get('bar'), "Bar should be the same on the clone.");
 127    equal(b.get('baz'), a.get('baz'), "Baz should be the same on the clone.");
 128    a.set({foo : 100});
 129    equal(a.get('foo'), 100);
 130    equal(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
 131
 132    var foo = new Backbone.Model({p: 1});
 133    var bar = new Backbone.Model({p: 2});
 134    bar.set(foo.clone().attributes, {unset: true});
 135    equal(foo.get('p'), 1);
 136    equal(bar.get('p'), undefined);
 137  });
 138
 139  test("isNew", 6, function() {
 140    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
 141    ok(a.isNew(), "it should be new");
 142    a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 });
 143    ok(!a.isNew(), "any defined ID is legal, negative or positive");
 144    a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 });
 145    ok(!a.isNew(), "any defined ID is legal, including zero");
 146    ok( new Backbone.Model({          }).isNew(), "is true when there is no id");
 147    ok(!new Backbone.Model({ 'id': 2  }).isNew(), "is false for a positive integer");
 148    ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integer");
 149  });
 150
 151  test("get", 2, function() {
 152    equal(doc.get('title'), 'The Tempest');
 153    equal(doc.get('author'), 'Bill Shakespeare');
 154  });
 155
 156  test("escape", 5, function() {
 157    equal(doc.escape('title'), 'The Tempest');
 158    doc.set({audience: 'Bill & Bob'});
 159    equal(doc.escape('audience'), 'Bill & Bob');
 160    doc.set({audience: 'Tim > Joan'});
 161    equal(doc.escape('audience'), 'Tim > Joan');
 162    doc.set({audience: 10101});
 163    equal(doc.escape('audience'), '10101');
 164    doc.unset('audience');
 165    equal(doc.escape('audience'), '');
 166  });
 167
 168  test("has", 10, function() {
 169    var model = new Backbone.Model();
 170
 171    strictEqual(model.has('name'), false);
 172
 173    model.set({
 174      '0': 0,
 175      '1': 1,
 176      'true': true,
 177      'false': false,
 178      'empty': '',
 179      'name': 'name',
 180      'null': null,
 181      'undefined': undefined
 182    });
 183
 184    strictEqual(model.has('0'), true);
 185    strictEqual(model.has('1'), true);
 186    strictEqual(model.has('true'), true);
 187    strictEqual(model.has('false'), true);
 188    strictEqual(model.has('empty'), true);
 189    strictEqual(model.has('name'), true);
 190
 191    model.unset('name');
 192
 193    strictEqual(model.has('name'), false);
 194    strictEqual(model.has('null'), false);
 195    strictEqual(model.has('undefined'), false);
 196  });
 197
 198  test("set and unset", 8, function() {
 199    var a = new Backbone.Model({id: 'id', foo: 1, bar: 2, baz: 3});
 200    var changeCount = 0;
 201    a.on("change:foo", function() { changeCount += 1; });
 202    a.set({'foo': 2});
 203    ok(a.get('foo') == 2, "Foo should have changed.");
 204    ok(changeCount == 1, "Change count should have incremented.");
 205    a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
 206    ok(a.get('foo') == 2, "Foo should NOT have changed, still 2");
 207    ok(changeCount == 1, "Change count should NOT have incremented.");
 208
 209    a.validate = function(attrs) {
 210      equal(attrs.foo, void 0, "validate:true passed while unsetting");
 211    };
 212    a.unset('foo', {validate: true});
 213    equal(a.get('foo'), void 0, "Foo should have changed");
 214    delete a.validate;
 215    ok(changeCount == 2, "Change count should have incremented for unset.");
 216
 217    a.unset('id');
 218    equal(a.id, undefined, "Unsetting the id should remove the id property.");
 219  });
 220
 221  test("#2030 - set with failed validate, followed by another set triggers change", function () {
 222    var attr = 0, main = 0, error = 0;
 223    var Model = Backbone.Model.extend({
 224      validate: function (attr) {
 225        if (attr.x > 1) {
 226          error++;
 227          return "this is an error";
 228        }
 229      }
 230    });
 231    var model = new Model({x:0});
 232      model.on('change:x', function () { attr++; });
 233      model.on('change', function () { main++; });
 234      model.set({x:2}, {validate:true});
 235      model.set({x:1}, {validate:true});
 236      deepEqual([attr, main, error], [1, 1, 1]);
 237  });
 238
 239  test("set triggers changes in the correct order", function() {
 240    var value = null;
 241    var model = new Backbone.Model;
 242    model.on('last', function(){ value = 'last'; });
 243    model.on('first', function(){ value = 'first'; });
 244    model.trigger('first');
 245    model.trigger('last');
 246    equal(value, 'last');
 247  });
 248
 249  test("set falsy values in the correct order", 2, function() {
 250    var model = new Backbone.Model({result: 'result'});
 251    model.on('change', function() {
 252      equal(model.changed.result, void 0);
 253      equal(model.previous('result'), false);
 254    });
 255    model.set({result: void 0}, {silent: true});
 256    model.set({result: null}, {silent: true});
 257    model.set({result: false}, {silent: true});
 258    model.set({result: void 0});
 259  });
 260
 261  test("multiple unsets", 1, function() {
 262    var i = 0;
 263    var counter = function(){ i++; };
 264    var model = new Backbone.Model({a: 1});
 265    model.on("change:a", counter);
 266    model.set({a: 2});
 267    model.unset('a');
 268    model.unset('a');
 269    equal(i, 2, 'Unset does not fire an event for missing attributes.');
 270  });
 271
 272  test("unset and changedAttributes", 1, function() {
 273    var model = new Backbone.Model({a: 1});
 274    model.on('change', function() {
 275      ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties');
 276    });
 277    model.unset('a');
 278  });
 279
 280  test("using a non-default id attribute.", 5, function() {
 281    var MongoModel = Backbone.Model.extend({idAttribute : '_id'});
 282    var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
 283    equal(model.get('id'), 'eye-dee');
 284    equal(model.id, 25);
 285    equal(model.isNew(), false);
 286    model.unset('_id');
 287    equal(model.id, undefined);
 288    equal(model.isNew(), true);
 289  });
 290
 291  test("set an empty string", 1, function() {
 292    var model = new Backbone.Model({name : "Model"});
 293    model.set({name : ''});
 294    equal(model.get('name'), '');
 295  });
 296
 297  test("setting an object", 1, function() {
 298    var model = new Backbone.Model({
 299      custom: { foo: 1 }
 300    });
 301    model.on('change', function() {
 302      ok(1);
 303    });
 304    model.set({
 305      custom: { foo: 1 } // no change should be fired
 306    });
 307    model.set({
 308      custom: { foo: 2 } // change event should be fired
 309    });
 310  });
 311
 312  test("clear", 3, function() {
 313    var changed;
 314    var model = new Backbone.Model({id: 1, name : "Model"});
 315    model.on("change:name", function(){ changed = true; });
 316    model.on("change", function() {
 317      var changedAttrs = model.changedAttributes();
 318      ok('name' in changedAttrs);
 319    });
 320    model.clear();
 321    equal(changed, true);
 322    equal(model.get('name'), undefined);
 323  });
 324
 325  test("defaults", 4, function() {
 326    var Defaulted = Backbone.Model.extend({
 327      defaults: {
 328        "one": 1,
 329        "two": 2
 330      }
 331    });
 332    var model = new Defaulted({two: null});
 333    equal(model.get('one'), 1);
 334    equal(model.get('two'), 2);
 335    Defaulted = Backbone.Model.extend({
 336      defaults: function() {
 337        return {
 338          "one": 3,
 339          "two": 4
 340        };
 341      }
 342    });
 343    model = new Defaulted({two: null});
 344    equal(model.get('one'), 3);
 345    equal(model.get('two'), 4);
 346  });
 347
 348  test("change, hasChanged, changedAttributes, previous, previousAttributes", 9, function() {
 349    var model = new Backbone.Model({name: "Tim", age: 10});
 350    deepEqual(model.changedAttributes(), false);
 351    model.on('change', function() {
 352      ok(model.hasChanged('name'), 'name changed');
 353      ok(!model.hasChanged('age'), 'age did not');
 354      ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
 355      equal(model.previous('name'), 'Tim');
 356      ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
 357    });
 358    equal(model.hasChanged(), false);
 359    equal(model.hasChanged(undefined), false);
 360    model.set({name : 'Rob'});
 361    equal(model.get('name'), 'Rob');
 362  });
 363
 364  test("changedAttributes", 3, function() {
 365    var model = new Backbone.Model({a: 'a', b: 'b'});
 366    deepEqual(model.changedAttributes(), false);
 367    equal(model.changedAttributes({a: 'a'}), false);
 368    equal(model.changedAttributes({a: 'b'}).a, 'b');
 369  });
 370
 371  test("change with options", 2, function() {
 372    var value;
 373    var model = new Backbone.Model({name: 'Rob'});
 374    model.on('change', function(model, options) {
 375      value = options.prefix + model.get('name');
 376    });
 377    model.set({name: 'Bob'}, {prefix: 'Mr. '});
 378    equal(value, 'Mr. Bob');
 379    model.set({name: 'Sue'}, {prefix: 'Ms. '});
 380    equal(value, 'Ms. Sue');
 381  });
 382
 383  test("change after initialize", 1, function () {
 384    var changed = 0;
 385    var attrs = {id: 1, label: 'c'};
 386    var obj = new Backbone.Model(attrs);
 387    obj.on('change', function() { changed += 1; });
 388    obj.set(attrs);
 389    equal(changed, 0);
 390  });
 391
 392  test("save within change event", 1, function () {
 393    var env = this;
 394    var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
 395    model.url = '/test';
 396    model.on('change', function () {
 397      model.save();
 398      ok(_.isEqual(env.syncArgs.model, model));
 399    });
 400    model.set({lastName: 'Hicks'});
 401  });
 402
 403  test("validate after save", 2, function() {
 404    var lastError, model = new Backbone.Model();
 405    model.validate = function(attrs) {
 406      if (attrs.admin) return "Can't change admin status.";
 407    };
 408    model.sync = function(method, model, options) {
 409      options.success.call(this, this, {admin: true}, options);
 410    };
 411    model.on('invalid', function(model, error) {
 412      lastError = error;
 413    });
 414    model.save(null);
 415
 416    equal(lastError, "Can't change admin status.");
 417    equal(model.validationError, "Can't change admin status.");
 418  });
 419
 420  test("save", 2, function() {
 421    doc.save({title : "Henry V"});
 422    equal(this.syncArgs.method, 'update');
 423    ok(_.isEqual(this.syncArgs.model, doc));
 424  });
 425
 426  test("save with PATCH", function() {
 427    doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
 428    doc.save();
 429    equal(this.syncArgs.method, 'update');
 430    equal(this.syncArgs.options.attrs, undefined);
 431
 432    doc.save({b: 2, d: 4}, {patch: true});
 433    equal(this.syncArgs.method, 'patch');
 434    equal(_.size(this.syncArgs.options.attrs), 2);
 435    equal(this.syncArgs.options.attrs.d, 4);
 436    equal(this.syncArgs.options.attrs.a, undefined);
 437    equal(this.ajaxSettings.data, "{\"b\":2,\"d\":4}");
 438  });
 439
 440  test("save in positional style", 1, function() {
 441    var model = new Backbone.Model();
 442    model.sync = function(method, model, options) {
 443      options.success(model, {}, options);
 444    };
 445    model.save('title', 'Twelfth Night');
 446    equal(model.get('title'), 'Twelfth Night');
 447  });
 448
 449  test("save with non-object success response", 2, function () {
 450    var model = new Backbone.Model();
 451    model.sync = function(method, model, options) {
 452      options.success(model, '', options);
 453      options.success(model, null, options);
 454    };
 455    model.save({testing:'empty'}, {
 456      success: function (model) {
 457        deepEqual(model.attributes, {testing:'empty'});
 458      }
 459    });
 460  });
 461
 462  test("fetch", 2, function() {
 463    doc.fetch();
 464    equal(this.syncArgs.method, 'read');
 465    ok(_.isEqual(this.syncArgs.model, doc));
 466  });
 467
 468  test("destroy", 3, function() {
 469    doc.destroy();
 470    equal(this.syncArgs.method, 'delete');
 471    ok(_.isEqual(this.syncArgs.model, doc));
 472
 473    var newModel = new Backbone.Model;
 474    equal(newModel.destroy(), false);
 475  });
 476
 477  test("non-persisted destroy", 1, function() {
 478    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
 479    a.sync = function() { throw "should not be called"; };
 480    a.destroy();
 481    ok(true, "non-persisted model should not call sync");
 482  });
 483
 484  test("validate", function() {
 485    var lastError;
 486    var model = new Backbone.Model();
 487    model.validate = function(attrs) {
 488      if (attrs.admin != this.get('admin')) return "Can't change admin status.";
 489    };
 490    model.on('invalid', function(model, error) {
 491      lastError = error;
 492    });
 493    var result = model.set({a: 100});
 494    equal(result, model);
 495    equal(model.get('a'), 100);
 496    equal(lastError, undefined);
 497    result = model.set({admin: true});
 498    equal(model.get('admin'), true);
 499    result = model.set({a: 200, admin: false}, {validate:true});
 500    equal(lastError, "Can't change admin status.");
 501    equal(result, false);
 502    equal(model.get('a'), 100);
 503  });
 504
 505  test("validate on unset and clear", 6, function() {
 506    var error;
 507    var model = new Backbone.Model({name: "One"});
 508    model.validate = function(attrs) {
 509      if (!attrs.name) {
 510        error = true;
 511        return "No thanks.";
 512      }
 513    };
 514    model.set({name: "Two"});
 515    equal(model.get('name'), 'Two');
 516    equal(error, undefined);
 517    model.unset('name', {validate: true});
 518    equal(error, true);
 519    equal(model.get('name'), 'Two');
 520    model.clear({validate:true});
 521    equal(model.get('name'), 'Two');
 522    delete model.validate;
 523    model.clear();
 524    equal(model.get('name'), undefined);
 525  });
 526
 527  test("validate with error callback", 8, function() {
 528    var lastError, boundError;
 529    var model = new Backbone.Model();
 530    model.validate = function(attrs) {
 531      if (attrs.admin) return "Can't change admin status.";
 532    };
 533    model.on('invalid', function(model, error) {
 534      boundError = true;
 535    });
 536    var result = model.set({a: 100}, {validate:true});
 537    equal(result, model);
 538    equal(model.get('a'), 100);
 539    equal(model.validationError, null);
 540    equal(boundError, undefined);
 541    result = model.set({a: 200, admin: true}, {validate:true});
 542    equal(result, false);
 543    equal(model.get('a'), 100);
 544    equal(model.validationError, "Can't change admin status.");
 545    equal(boundError, true);
 546  });
 547
 548  test("defaults always extend attrs (#459)", 2, function() {
 549    var Defaulted = Backbone.Model.extend({
 550      defaults: {one: 1},
 551      initialize : function(attrs, opts) {
 552        equal(this.attributes.one, 1);
 553      }
 554    });
 555    var providedattrs = new Defaulted({});
 556    var emptyattrs = new Defaulted();
 557  });
 558
 559  test("Inherit class properties", 6, function() {
 560    var Parent = Backbone.Model.extend({
 561      instancePropSame: function() {},
 562      instancePropDiff: function() {}
 563    }, {
 564      classProp: function() {}
 565    });
 566    var Child = Parent.extend({
 567      instancePropDiff: function() {}
 568    });
 569
 570    var adult = new Parent;
 571    var kid   = new Child;
 572
 573    equal(Child.classProp, Parent.classProp);
 574    notEqual(Child.classProp, undefined);
 575
 576    equal(kid.instancePropSame, adult.instancePropSame);
 577    notEqual(kid.instancePropSame, undefined);
 578
 579    notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
 580    notEqual(Child.prototype.instancePropDiff, undefined);
 581  });
 582
 583  test("Nested change events don't clobber previous attributes", 4, function() {
 584    new Backbone.Model()
 585    .on('change:state', function(model, newState) {
 586      equal(model.previous('state'), undefined);
 587      equal(newState, 'hello');
 588      // Fire a nested change event.
 589      model.set({other: 'whatever'});
 590    })
 591    .on('change:state', function(model, newState) {
 592      equal(model.previous('state'), undefined);
 593      equal(newState, 'hello');
 594    })
 595    .set({state: 'hello'});
 596  });
 597
 598  test("hasChanged/set should use same comparison", 2, function() {
 599    var changed = 0, model = new Backbone.Model({a: null});
 600    model.on('change', function() {
 601      ok(this.hasChanged('a'));
 602    })
 603    .on('change:a', function() {
 604      changed++;
 605    })
 606    .set({a: undefined});
 607    equal(changed, 1);
 608  });
 609
 610  test("#582, #425, change:attribute callbacks should fire after all changes have occurred", 9, function() {
 611    var model = new Backbone.Model;
 612
 613    var assertion = function() {
 614      equal(model.get('a'), 'a');
 615      equal(model.get('b'), 'b');
 616      equal(model.get('c'), 'c');
 617    };
 618
 619    model.on('change:a', assertion);
 620    model.on('change:b', assertion);
 621    model.on('change:c', assertion);
 622
 623    model.set({a: 'a', b: 'b', c: 'c'});
 624  });
 625
 626  test("#871, set with attributes property", 1, function() {
 627    var model = new Backbone.Model();
 628    model.set({attributes: true});
 629    ok(model.has('attributes'));
 630  });
 631
 632  test("set value regardless of equality/change", 1, function() {
 633    var model = new Backbone.Model({x: []});
 634    var a = [];
 635    model.set({x: a});
 636    ok(model.get('x') === a);
 637  });
 638
 639  test("set same value does not trigger change", 0, function() {
 640    var model = new Backbone.Model({x: 1});
 641    model.on('change change:x', function() { ok(false); });
 642    model.set({x: 1});
 643    model.set({x: 1});
 644  });
 645
 646  test("unset does not fire a change for undefined attributes", 0, function() {
 647    var model = new Backbone.Model({x: undefined});
 648    model.on('change:x', function(){ ok(false); });
 649    model.unset('x');
 650  });
 651
 652  test("set: undefined values", 1, function() {
 653    var model = new Backbone.Model({x: undefined});
 654    ok('x' in model.attributes);
 655  });
 656
 657  test("hasChanged works outside of change events, and true within", 6, function() {
 658    var model = new Backbone.Model({x: 1});
 659    model.on('change:x', function() {
 660      ok(model.hasChanged('x'));
 661      equal(model.get('x'), 1);
 662    });
 663    model.set({x: 2}, {silent: true});
 664    ok(model.hasChanged());
 665    equal(model.hasChanged('x'), true);
 666    model.set({x: 1});
 667    ok(model.hasChanged());
 668    equal(model.hasChanged('x'), true);
 669  });
 670
 671  test("hasChanged gets cleared on the following set", 4, function() {
 672    var model = new Backbone.Model;
 673    model.set({x: 1});
 674    ok(model.hasChanged());
 675    model.set({x: 1});
 676    ok(!model.hasChanged());
 677    model.set({x: 2});
 678    ok(model.hasChanged());
 679    model.set({});
 680    ok(!model.hasChanged());
 681  });
 682
 683  test("save with `wait` succeeds without `validate`", 1, function() {
 684    var model = new Backbone.Model();
 685    model.url = '/test';
 686    model.save({x: 1}, {wait: true});
 687    ok(this.syncArgs.model === model);
 688  });
 689
 690  test("`hasChanged` for falsey keys", 2, function() {
 691    var model = new Backbone.Model();
 692    model.set({x: true}, {silent: true});
 693    ok(!model.hasChanged(0));
 694    ok(!model.hasChanged(''));
 695  });
 696
 697  test("`previous` for falsey keys", 2, function() {
 698    var model = new Backbone.Model({0: true, '': true});
 699    model.set({0: false, '': false}, {silent: true});
 700    equal(model.previous(0), true);
 701    equal(model.previous(''), true);
 702  });
 703
 704  test("`save` with `wait` sends correct attributes", 5, function() {
 705    var changed = 0;
 706    var model = new Backbone.Model({x: 1, y: 2});
 707    model.url = '/test';
 708    model.on('change:x', function() { changed++; });
 709    model.save({x: 3}, {wait: true});
 710    deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
 711    equal(model.get('x'), 1);
 712    equal(changed, 0);
 713    this.syncArgs.options.success({});
 714    equal(model.get('x'), 3);
 715    equal(changed, 1);
 716  });
 717
 718  test("a failed `save` with `wait` doesn't leave attributes behind", 1, function() {
 719    var model = new Backbone.Model;
 720    model.url = '/test';
 721    model.save({x: 1}, {wait: true});
 722    equal(model.get('x'), void 0);
 723  });
 724
 725  test("#1030 - `save` with `wait` results in correct attributes if success is called during sync", 2, function() {
 726    var model = new Backbone.Model({x: 1, y: 2});
 727    model.sync = function(method, model, options) {
 728      options.success(model, {}, options);
 729    };
 730    model.on("change:x", function() { ok(true); });
 731    model.save({x: 3}, {wait: true});
 732    equal(model.get('x'), 3);
 733  });
 734
 735  test("save with wait validates attributes", function() {
 736    var model = new Backbone.Model();
 737    model.url = '/test';
 738    model.validate = function() { ok(true); };
 739    model.save({x: 1}, {wait: true});
 740  });
 741
 742  test("save turns on parse flag", function () {
 743    var Model = Backbone.Model.extend({
 744      sync: function(method, model, options) { ok(options.parse); }
 745    });
 746    new Model().save();
 747  });
 748
 749  test("nested `set` during `'change:attr'`", 2, function() {
 750    var events = [];
 751    var model = new Backbone.Model();
 752    model.on('all', function(event) { events.push(event); });
 753    model.on('change', function() {
 754      model.set({z: true}, {silent:true});
 755    });
 756    model.on('change:x', function() {
 757      model.set({y: true});
 758    });
 759    model.set({x: true});
 760    deepEqual(events, ['change:y', 'change:x', 'change']);
 761    events = [];
 762    model.set({z: true});
 763    deepEqual(events, []);
 764  });
 765
 766  test("nested `change` only fires once", 1, function() {
 767    var model = new Backbone.Model();
 768    model.on('change', function() {
 769      ok(true);
 770      model.set({x: true});
 771    });
 772    model.set({x: true});
 773  });
 774
 775  test("nested `set` during `'change'`", 6, function() {
 776    var count = 0;
 777    var model = new Backbone.Model();
 778    model.on('change', function() {
 779      switch(count++) {
 780        case 0:
 781          deepEqual(this.changedAttributes(), {x: true});
 782          equal(model.previous('x'), undefined);
 783          model.set({y: true});
 784          break;
 785        case 1:
 786          deepEqual(this.changedAttributes(), {x: true, y: true});
 787          equal(model.previous('x'), undefined);
 788          model.set({z: true});
 789          break;
 790        case 2:
 791          deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
 792          equal(model.previous('y'), undefined);
 793          break;
 794        default:
 795          ok(false);
 796      }
 797    });
 798    model.set({x: true});
 799  });
 800
 801  test("nested `change` with silent", 3, function() {
 802    var count = 0;
 803    var model = new Backbone.Model();
 804    model.on('change:y', function() { ok(false); });
 805    model.on('change', function() {
 806      switch(count++) {
 807        case 0:
 808          deepEqual(this.changedAttributes(), {x: true});
 809          model.set({y: true}, {silent: true});
 810          model.set({z: true});
 811          break;
 812        case 1:
 813          deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
 814          break;
 815        case 2:
 816          deepEqual(this.changedAttributes(), {z: false});
 817          break;
 818        default:
 819          ok(false);
 820      }
 821    });
 822    model.set({x: true});
 823    model.set({z: false});
 824  });
 825
 826  test("nested `change:attr` with silent", 0, function() {
 827    var model = new Backbone.Model();
 828    model.on('change:y', function(){ ok(false); });
 829    model.on('change', function() {
 830      model.set({y: true}, {silent: true});
 831      model.set({z: true});
 832    });
 833    model.set({x: true});
 834  });
 835
 836  test("multiple nested changes with silent", 1, function() {
 837    var model = new Backbone.Model();
 838    model.on('change:x', function() {
 839      model.set({y: 1}, {silent: true});
 840      model.set({y: 2});
 841    });
 842    model.on('change:y', function(model, val) {
 843      equal(val, 2);
 844    });
 845    model.set({x: true});
 846  });
 847
 848  test("multiple nested changes with silent", 1, function() {
 849    var changes = [];
 850    var model = new Backbone.Model();
 851    model.on('change:b', function(model, val) { changes.push(val); });
 852    model.on('change', function() {
 853      model.set({b: 1});
 854    });
 855    model.set({b: 0});
 856    deepEqual(changes, [0, 1]);
 857  });
 858
 859  test("basic silent change semantics", 1, function() {
 860    var model = new Backbone.Model;
 861    model.set({x: 1});
 862    model.on('change', function(){ ok(true); });
 863    model.set({x: 2}, {silent: true});
 864    model.set({x: 1});
 865  });
 866
 867  test("nested set multiple times", 1, function() {
 868    var model = new Backbone.Model();
 869    model.on('change:b', function() {
 870      ok(true);
 871    });
 872    model.on('change:a', function() {
 873      model.set({b: true});
 874      model.set({b: true});
 875    });
 876    model.set({a: true});
 877  });
 878
 879  test("#1122 - clear does not alter options.", 1, function() {
 880    var model = new Backbone.Model();
 881    var options = {};
 882    model.clear(options);
 883    ok(!options.unset);
 884  });
 885
 886  test("#1122 - unset does not alter options.", 1, function() {
 887    var model = new Backbone.Model();
 888    var options = {};
 889    model.unset('x', options);
 890    ok(!options.unset);
 891  });
 892
 893  test("#1355 - `options` is passed to success callbacks", 3, function() {
 894    var model = new Backbone.Model();
 895    var opts = {
 896      success: function( model, resp, options ) {
 897        ok(options);
 898      }
 899    };
 900    model.sync = function(method, model, options) {
 901      options.success(model, {}, options);
 902    };
 903    model.save({id: 1}, opts);
 904    model.fetch(opts);
 905    model.destroy(opts);
 906  });
 907
 908  test("#1412 - Trigger 'sync' event.", 3, function() {
 909    var model = new Backbone.Model({id: 1});
 910    model.url = '/test';
 911    model.on('sync', function(){ ok(true); });
 912    Backbone.ajax = function(settings){ settings.success(); };
 913    model.fetch();
 914    model.save();
 915    model.destroy();
 916  });
 917
 918  test("#1365 - Destroy: New models execute success callback.", 2, function() {
 919    new Backbone.Model()
 920    .on('sync', function() { ok(false); })
 921    .on('destroy', function(){ ok(true); })
 922    .destroy({ success: function(){ ok(true); }});
 923  });
 924
 925  test("#1433 - Save: An invalid model cannot be persisted.", 1, function() {
 926    var model = new Backbone.Model;
 927    model.validate = function(){ return 'invalid'; };
 928    model.sync = function(){ ok(false); };
 929    strictEqual(model.save(), false);
 930  });
 931
 932  test("#1377 - Save without attrs triggers 'error'.", 1, function() {
 933    var Model = Backbone.Model.extend({
 934      url: '/test/',
 935      sync: function(method, model, options){ options.success(); },
 936      validate: function(){ return 'invalid'; }
 937    });
 938    var model = new Model({id: 1});
 939    model.on('invalid', function(){ ok(true); });
 940    model.save();
 941  });
 942
 943  test("#1545 - `undefined` can be passed to a model constructor without coersion", function() {
 944    var Model = Backbone.Model.extend({
 945      defaults: { one: 1 },
 946      initialize : function(attrs, opts) {
 947        equal(attrs, undefined);
 948      }
 949    });
 950    var emptyattrs = new Model();
 951    var undefinedattrs = new Model(undefined);
 952  });
 953
 954  asyncTest("#1478 - Model `save` does not trigger change on unchanged attributes", 0, function() {
 955    var Model = Backbone.Model.extend({
 956      sync: function(method, model, options) {
 957        setTimeout(function(){
 958          options.success(model, {}, options);
 959          start();
 960        }, 0);
 961      }
 962    });
 963    new Model({x: true})
 964    .on('change:x', function(){ ok(false); })
 965    .save(null, {wait: true});
 966  });
 967
 968  test("#1664 - Changing from one value, silently to another, back to original triggers a change.", 1, function() {
 969    var model = new Backbone.Model({x:1});
 970    model.on('change:x', function() { ok(true); });
 971    model.set({x:2},{silent:true});
 972    model.set({x:3},{silent:true});
 973    model.set({x:1});
 974  });
 975
 976  test("#1664 - multiple silent changes nested inside a change event", 2, function() {
 977    var changes = [];
 978    var model = new Backbone.Model();
 979    model.on('change', function() {
 980      model.set({a:'c'}, {silent:true});
 981      model.set({b:2}, {silent:true});
 982      model.unset('c', {silent:true});
 983    });
 984    model.on('change:a change:b change:c', function(model, val) { changes.push(val); });
 985    model.set({a:'a', b:1, c:'item'});
 986    deepEqual(changes, ['a',1,'item']);
 987    deepEqual(model.attributes, {a: 'c', b: 2});
 988  });
 989
 990  test("#1791 - `attributes` is available for `parse`", function() {
 991    var Model = Backbone.Model.extend({
 992      parse: function() { this.has('a'); } // shouldn't throw an error
 993    });
 994    var model = new Model(null, {parse: true});
 995    expect(0);
 996  });
 997
 998  test("silent changes in last `change` event back to original triggers change", 2, function() {
 999    var changes = [];
1000    var model = new Backbone.Model();
1001    model.on('change:a change:b change:c', function(model, val) { changes.push(val); });
1002    model.on('change', function() {
1003      model.set({a:'c'}, {silent:true});
1004    });
1005    model.set({a:'a'});
1006    deepEqual(changes, ['a']);
1007    model.set({a:'a'});
1008    deepEqual(changes, ['a', 'a']);
1009  });
1010
1011  test("#1943 change calculations should use _.isEqual", function() {
1012    var model = new Backbone.Model({a: {key: 'value'}});
1013    model.set('a', {key:'value'}, {silent:true});
1014    equal(model.changedAttributes(), false);
1015  });
1016
1017  test("#1964 - final `change` event is always fired, regardless of interim changes", 1, function () {
1018    var model = new Backbone.Model();
1019    model.on('change:property', function() {
1020      model.set('property', 'bar');
1021    });
1022    model.on('change', function() {
1023      ok(true);
1024    });
1025    model.set('property', 'foo');
1026  });
1027
1028  test("isValid", function() {
1029    var model = new Backbone.Model({valid: true});
1030    model.validate = function(attrs) {
1031      if (!attrs.valid) return "invalid";
1032    };
1033    equal(model.isValid(), true);
1034    equal(model.set({valid: false}, {validate:true}), false);
1035    equal(model.isValid(), true);
1036    model.set({valid:false});
1037    equal(model.isValid(), false);
1038    ok(!model.set('valid', false, {validate: true}));
1039  });
1040
1041  test("#1179 - isValid returns true in the absence of validate.", 1, function() {
1042    var model = new Backbone.Model();
1043    model.validate = null;
1044    ok(model.isValid());
1045  });
1046
1047  test("#1961 - Creating a model with {validate:true} will call validate and use the error callback", function () {
1048    var Model = Backbone.Model.extend({
1049      validate: function (attrs) {
1050        if (attrs.id === 1) return "This shouldn't happen";
1051      }
1052    });
1053    var model = new Model({id: 1}, {validate: true});
1054    equal(model.validationError, "This shouldn't happen");
1055  });
1056
1057  test("toJSON receives attrs during save(..., {wait: true})", 1, function() {
1058    var Model = Backbone.Model.extend({
1059      url: '/test',
1060      toJSON: function() {
1061        strictEqual(this.attributes.x, 1);
1062        return _.clone(this.attributes);
1063      }
1064    });
1065    var model = new Model;
1066    model.save({x: 1}, {wait: true});
1067  });
1068
1069  test("#2034 - nested set with silent only triggers one change", 1, function() {
1070    var model = new Backbone.Model();
1071    model.on('change', function() {
1072      model.set({b: true}, {silent: true});
1073      ok(true);
1074    });
1075    model.set({a: true});
1076  });
1077
1078  // reset the Model
1079  Backbone.Model = oldModel;
1080
1081});