PageRenderTime 88ms CodeModel.GetById 2ms app.highlight 76ms RepoModel.GetById 1ms app.codeStats 1ms

/test/model.js

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