PageRenderTime 102ms CodeModel.GetById 3ms app.highlight 91ms RepoModel.GetById 1ms app.codeStats 0ms

/test/model.js

https://github.com/jessepinuelas/backbone
JavaScript | 1006 lines | 898 code | 107 blank | 1 comment | 14 complexity | a1988d6cce583b169e3558e196b1f427 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(attrs) {
  50        attrs.value += 1;
  51        return attrs;
  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(attrs) {
  73        attrs.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: false});
 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});
 251  });
 252
 253  test("multiple unsets", 1, function() {
 254    var i = 0;
 255    var counter = function(){ i++; };
 256    var model = new Backbone.Model({a: 1});
 257    model.on("change:a", counter);
 258    model.set({a: 2});
 259    model.unset('a');
 260    model.unset('a');
 261    equal(i, 2, 'Unset does not fire an event for missing attributes.');
 262  });
 263
 264  test("unset and changedAttributes", 1, function() {
 265    var model = new Backbone.Model({a: 1});
 266    model.on('change', function() {
 267      ok('a' in model.changedAttributes(), 'changedAttributes should contain unset properties');
 268    });
 269    model.unset('a');
 270  });
 271
 272  test("using a non-default id attribute.", 5, function() {
 273    var MongoModel = Backbone.Model.extend({idAttribute : '_id'});
 274    var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
 275    equal(model.get('id'), 'eye-dee');
 276    equal(model.id, 25);
 277    equal(model.isNew(), false);
 278    model.unset('_id');
 279    equal(model.id, undefined);
 280    equal(model.isNew(), true);
 281  });
 282
 283  test("set an empty string", 1, function() {
 284    var model = new Backbone.Model({name : "Model"});
 285    model.set({name : ''});
 286    equal(model.get('name'), '');
 287  });
 288
 289  test("setting an object", 1, function() {
 290    var model = new Backbone.Model({
 291      custom: { foo: 1 }
 292    });
 293    model.on('change', function() {
 294      ok(1);
 295    });
 296    model.set({
 297      custom: { foo: 1 } // no change should be fired
 298    });
 299    model.set({
 300      custom: { foo: 2 } // change event should be fired
 301    });
 302  });
 303
 304  test("clear", 3, function() {
 305    var changed;
 306    var model = new Backbone.Model({id: 1, name : "Model"});
 307    model.on("change:name", function(){ changed = true; });
 308    model.on("change", function() {
 309      var changedAttrs = model.changedAttributes();
 310      ok('name' in changedAttrs);
 311    });
 312    model.clear();
 313    equal(changed, true);
 314    equal(model.get('name'), undefined);
 315  });
 316
 317  test("defaults", 4, function() {
 318    var Defaulted = Backbone.Model.extend({
 319      defaults: {
 320        "one": 1,
 321        "two": 2
 322      }
 323    });
 324    var model = new Defaulted({two: undefined});
 325    equal(model.get('one'), 1);
 326    equal(model.get('two'), 2);
 327    Defaulted = Backbone.Model.extend({
 328      defaults: function() {
 329        return {
 330          "one": 3,
 331          "two": 4
 332        };
 333      }
 334    });
 335    model = new Defaulted({two: undefined});
 336    equal(model.get('one'), 3);
 337    equal(model.get('two'), 4);
 338  });
 339
 340  test("change, hasChanged, changedAttributes, previous, previousAttributes", 9, function() {
 341    var model = new Backbone.Model({name: "Tim", age: 10});
 342    deepEqual(model.changedAttributes(), false);
 343    model.on('change', function() {
 344      ok(model.hasChanged('name'), 'name changed');
 345      ok(!model.hasChanged('age'), 'age did not');
 346      ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
 347      equal(model.previous('name'), 'Tim');
 348      ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
 349    });
 350    equal(model.hasChanged(), false);
 351    equal(model.hasChanged(undefined), false);
 352    model.set({name : 'Rob'});
 353    equal(model.get('name'), 'Rob');
 354  });
 355
 356  test("changedAttributes", 3, function() {
 357    var model = new Backbone.Model({a: 'a', b: 'b'});
 358    deepEqual(model.changedAttributes(), false);
 359    equal(model.changedAttributes({a: 'a'}), false);
 360    equal(model.changedAttributes({a: 'b'}).a, 'b');
 361  });
 362
 363  test("change with options", 2, function() {
 364    var value;
 365    var model = new Backbone.Model({name: 'Rob'});
 366    model.on('change', function(model, options) {
 367      value = options.prefix + model.get('name');
 368    });
 369    model.set({name: 'Bob'}, {prefix: 'Mr. '});
 370    equal(value, 'Mr. Bob');
 371    model.set({name: 'Sue'}, {prefix: 'Ms. '});
 372    equal(value, 'Ms. Sue');
 373  });
 374
 375  test("change after initialize", 1, function () {
 376    var changed = 0;
 377    var attrs = {id: 1, label: 'c'};
 378    var obj = new Backbone.Model(attrs);
 379    obj.on('change', function() { changed += 1; });
 380    obj.set(attrs);
 381    equal(changed, 0);
 382  });
 383
 384  test("save within change event", 1, function () {
 385    var env = this;
 386    var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
 387    model.url = '/test';
 388    model.on('change', function () {
 389      model.save();
 390      ok(_.isEqual(env.syncArgs.model, model));
 391    });
 392    model.set({lastName: 'Hicks'});
 393  });
 394
 395  test("validate after save", 2, function() {
 396    var lastError, model = new Backbone.Model();
 397    model.validate = function(attrs) {
 398      if (attrs.admin) return "Can't change admin status.";
 399    };
 400    model.sync = function(method, model, options) {
 401      options.success.call(this, {admin: true});
 402    };
 403    model.on('invalid', function(model, error) {
 404      lastError = error;
 405    });
 406    model.save(null);
 407
 408    equal(lastError, "Can't change admin status.");
 409    equal(model.validationError, "Can't change admin status.");
 410  });
 411
 412  test("save", 2, function() {
 413    doc.save({title : "Henry V"});
 414    equal(this.syncArgs.method, 'update');
 415    ok(_.isEqual(this.syncArgs.model, doc));
 416  });
 417
 418  test("save, fetch, destroy triggers error event when an error occurs", 3, function () {
 419    var model = new Backbone.Model();
 420    model.on('error', function () {
 421      ok(true);
 422    });
 423    model.sync = function (method, model, options) {
 424      options.error();
 425    };
 426    model.save({data: 2, id: 1});
 427    model.fetch();
 428    model.destroy();
 429  });
 430
 431  test("save with PATCH", function() {
 432    doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
 433    doc.save();
 434    equal(this.syncArgs.method, 'update');
 435    equal(this.syncArgs.options.attrs, undefined);
 436
 437    doc.save({b: 2, d: 4}, {patch: true});
 438    equal(this.syncArgs.method, 'patch');
 439    equal(_.size(this.syncArgs.options.attrs), 2);
 440    equal(this.syncArgs.options.attrs.d, 4);
 441    equal(this.syncArgs.options.attrs.a, undefined);
 442    equal(this.ajaxSettings.data, "{\"b\":2,\"d\":4}");
 443  });
 444
 445  test("save in positional style", 1, function() {
 446    var model = new Backbone.Model();
 447    model.sync = function(method, model, options) {
 448      options.success();
 449    };
 450    model.save('title', 'Twelfth Night');
 451    equal(model.get('title'), 'Twelfth Night');
 452  });
 453
 454  test("save with non-object success response", 2, function () {
 455    var model = new Backbone.Model();
 456    model.sync = function(method, model, options) {
 457      options.success('', options);
 458      options.success(null, options);
 459    };
 460    model.save({testing:'empty'}, {
 461      success: function (model) {
 462        deepEqual(model.attributes, {testing:'empty'});
 463      }
 464    });
 465  });
 466
 467  test("fetch", 2, function() {
 468    doc.fetch();
 469    equal(this.syncArgs.method, 'read');
 470    ok(_.isEqual(this.syncArgs.model, doc));
 471  });
 472
 473  test("destroy", 3, function() {
 474    doc.destroy();
 475    equal(this.syncArgs.method, 'delete');
 476    ok(_.isEqual(this.syncArgs.model, doc));
 477
 478    var newModel = new Backbone.Model;
 479    equal(newModel.destroy(), false);
 480  });
 481
 482  test("non-persisted destroy", 1, function() {
 483    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
 484    a.sync = function() { throw "should not be called"; };
 485    a.destroy();
 486    ok(true, "non-persisted model should not call sync");
 487  });
 488
 489  test("validate", function() {
 490    var lastError;
 491    var model = new Backbone.Model();
 492    model.validate = function(attrs) {
 493      if (attrs.admin != this.get('admin')) return "Can't change admin status.";
 494    };
 495    model.on('invalid', function(model, error) {
 496      lastError = error;
 497    });
 498    var result = model.set({a: 100});
 499    equal(result, model);
 500    equal(model.get('a'), 100);
 501    equal(lastError, undefined);
 502    result = model.set({admin: true});
 503    equal(model.get('admin'), true);
 504    result = model.set({a: 200, admin: false}, {validate:true});
 505    equal(lastError, "Can't change admin status.");
 506    equal(result, false);
 507    equal(model.get('a'), 100);
 508  });
 509
 510  test("validate on unset and clear", 6, function() {
 511    var error;
 512    var model = new Backbone.Model({name: "One"});
 513    model.validate = function(attrs) {
 514      if (!attrs.name) {
 515        error = true;
 516        return "No thanks.";
 517      }
 518    };
 519    model.set({name: "Two"});
 520    equal(model.get('name'), 'Two');
 521    equal(error, undefined);
 522    model.unset('name', {validate: true});
 523    equal(error, true);
 524    equal(model.get('name'), 'Two');
 525    model.clear({validate:true});
 526    equal(model.get('name'), 'Two');
 527    delete model.validate;
 528    model.clear();
 529    equal(model.get('name'), undefined);
 530  });
 531
 532  test("validate with error callback", 8, function() {
 533    var lastError, boundError;
 534    var model = new Backbone.Model();
 535    model.validate = function(attrs) {
 536      if (attrs.admin) return "Can't change admin status.";
 537    };
 538    model.on('invalid', function(model, error) {
 539      boundError = true;
 540    });
 541    var result = model.set({a: 100}, {validate:true});
 542    equal(result, model);
 543    equal(model.get('a'), 100);
 544    equal(model.validationError, null);
 545    equal(boundError, undefined);
 546    result = model.set({a: 200, admin: true}, {validate:true});
 547    equal(result, false);
 548    equal(model.get('a'), 100);
 549    equal(model.validationError, "Can't change admin status.");
 550    equal(boundError, true);
 551  });
 552
 553  test("defaults always extend attrs (#459)", 2, function() {
 554    var Defaulted = Backbone.Model.extend({
 555      defaults: {one: 1},
 556      initialize : function(attrs, opts) {
 557        equal(this.attributes.one, 1);
 558      }
 559    });
 560    var providedattrs = new Defaulted({});
 561    var emptyattrs = new Defaulted();
 562  });
 563
 564  test("Inherit class properties", 6, function() {
 565    var Parent = Backbone.Model.extend({
 566      instancePropSame: function() {},
 567      instancePropDiff: function() {}
 568    }, {
 569      classProp: function() {}
 570    });
 571    var Child = Parent.extend({
 572      instancePropDiff: function() {}
 573    });
 574
 575    var adult = new Parent;
 576    var kid   = new Child;
 577
 578    equal(Child.classProp, Parent.classProp);
 579    notEqual(Child.classProp, undefined);
 580
 581    equal(kid.instancePropSame, adult.instancePropSame);
 582    notEqual(kid.instancePropSame, undefined);
 583
 584    notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
 585    notEqual(Child.prototype.instancePropDiff, undefined);
 586  });
 587
 588  test("Nested change events don't clobber previous attributes", 4, function() {
 589    new Backbone.Model()
 590    .on('change:state', function(model, newState) {
 591      equal(model.previous('state'), undefined);
 592      equal(newState, 'hello');
 593      // Fire a nested change event.
 594      model.set({other: 'whatever'});
 595    })
 596    .on('change:state', function(model, newState) {
 597      equal(model.previous('state'), undefined);
 598      equal(newState, 'hello');
 599    })
 600    .set({state: 'hello'});
 601  });
 602
 603  test("hasChanged/set should use same comparison", 2, function() {
 604    var changed = 0, model = new Backbone.Model({a: null});
 605    model.on('change', function() {
 606      ok(this.hasChanged('a'));
 607    })
 608    .on('change:a', function() {
 609      changed++;
 610    })
 611    .set({a: undefined});
 612    equal(changed, 1);
 613  });
 614
 615  test("#582, #425, change:attribute callbacks should fire after all changes have occurred", 9, function() {
 616    var model = new Backbone.Model;
 617
 618    var assertion = function() {
 619      equal(model.get('a'), 'a');
 620      equal(model.get('b'), 'b');
 621      equal(model.get('c'), 'c');
 622    };
 623
 624    model.on('change:a', assertion);
 625    model.on('change:b', assertion);
 626    model.on('change:c', assertion);
 627
 628    model.set({a: 'a', b: 'b', c: 'c'});
 629  });
 630
 631  test("#871, set with attributes property", 1, function() {
 632    var model = new Backbone.Model();
 633    model.set({attributes: true});
 634    ok(model.has('attributes'));
 635  });
 636
 637  test("set value regardless of equality/change", 1, function() {
 638    var model = new Backbone.Model({x: []});
 639    var a = [];
 640    model.set({x: a});
 641    ok(model.get('x') === a);
 642  });
 643
 644  test("set same value does not trigger change", 0, function() {
 645    var model = new Backbone.Model({x: 1});
 646    model.on('change change:x', function() { ok(false); });
 647    model.set({x: 1});
 648    model.set({x: 1});
 649  });
 650
 651  test("unset does not fire a change for undefined attributes", 0, function() {
 652    var model = new Backbone.Model({x: undefined});
 653    model.on('change:x', function(){ ok(false); });
 654    model.unset('x');
 655  });
 656
 657  test("set: undefined values", 1, function() {
 658    var model = new Backbone.Model({x: undefined});
 659    ok('x' in model.attributes);
 660  });
 661
 662  test("hasChanged works outside of change events, and true within", 4, function() {
 663    var model = new Backbone.Model;
 664    model.on('change:x', function() {
 665      ok(model.hasChanged('x'));
 666      equal(model.get('x'), 1);
 667    });
 668    model.set({x: 1});
 669    ok(model.hasChanged());
 670    equal(model.hasChanged('x'), true);
 671  });
 672
 673  test("hasChanged gets cleared on the following set", 4, function() {
 674    var model = new Backbone.Model;
 675    model.set({x: 1});
 676    ok(model.hasChanged());
 677    model.set({x: 1});
 678    ok(!model.hasChanged());
 679    model.set({x: 2});
 680    ok(model.hasChanged());
 681    model.set({});
 682    ok(!model.hasChanged());
 683  });
 684
 685  test("save with `wait` succeeds without `validate`", 1, function() {
 686    var model = new Backbone.Model();
 687    model.url = '/test';
 688    model.save({x: 1}, {wait: true});
 689    ok(this.syncArgs.model === model);
 690  });
 691
 692  test("`hasChanged` for falsey keys", 2, function() {
 693    var model = new Backbone.Model();
 694    model.set({x: true});
 695    ok(!model.hasChanged(0));
 696    ok(!model.hasChanged(''));
 697  });
 698
 699  test("`previous` for falsey keys", 2, function() {
 700    var model = new Backbone.Model({0: true, '': true});
 701    model.set({0: false, '': false});
 702    equal(model.previous(0), true);
 703    equal(model.previous(''), true);
 704  });
 705
 706  test("`save` with `wait` sends correct attributes", 5, function() {
 707    var changed = 0;
 708    var model = new Backbone.Model({x: 1, y: 2});
 709    model.url = '/test';
 710    model.on('change:x', function() { changed++; });
 711    model.save({x: 3}, {wait: true});
 712    deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
 713    equal(model.get('x'), 1);
 714    equal(changed, 0);
 715    this.syncArgs.options.success({});
 716    equal(model.get('x'), 3);
 717    equal(changed, 1);
 718  });
 719
 720  test("a failed `save` with `wait` doesn't leave attributes behind", 1, function() {
 721    var model = new Backbone.Model;
 722    model.url = '/test';
 723    model.save({x: 1}, {wait: true});
 724    equal(model.get('x'), void 0);
 725  });
 726
 727  test("#1030 - `save` with `wait` results in correct attributes if success is called during sync", 2, function() {
 728    var model = new Backbone.Model({x: 1, y: 2});
 729    model.sync = function(method, model, options) {
 730      options.success();
 731    };
 732    model.on("change:x", function() { ok(true); });
 733    model.save({x: 3}, {wait: true});
 734    equal(model.get('x'), 3);
 735  });
 736
 737  test("save with wait validates attributes", function() {
 738    var model = new Backbone.Model();
 739    model.url = '/test';
 740    model.validate = function() { ok(true); };
 741    model.save({x: 1}, {wait: true});
 742  });
 743
 744  test("save turns on parse flag", function () {
 745    var Model = Backbone.Model.extend({
 746      sync: function(method, model, options) { ok(options.parse); }
 747    });
 748    new Model().save();
 749  });
 750
 751  test("nested `set` during `'change:attr'`", 1, function() {
 752    var events = [];
 753    var model = new Backbone.Model;
 754    model.on('all', function(event) { events.push(event); });
 755    model.on('change', function() {
 756      model.set({z: true});
 757    });
 758    model.on('change:x', function() {
 759      model.set({y: true});
 760    });
 761    model.set({x: true});
 762    deepEqual(events, [
 763      'change:y',
 764      'change:x',
 765      'change:z',
 766      'change',
 767      'change'
 768    ]);
 769  });
 770
 771  test("nested `change` only fires once", 1, function() {
 772    var model = new Backbone.Model();
 773    model.on('change', function() {
 774      ok(true);
 775      model.set({x: true});
 776    });
 777    model.set({x: true});
 778  });
 779
 780  test("nested `set` during `'change'`", 6, function() {
 781    var count = 0;
 782    var model = new Backbone.Model();
 783    model.on('change', function() {
 784      switch(count++) {
 785        case 0:
 786          deepEqual(this.changedAttributes(), {x: true});
 787          equal(model.previous('x'), undefined);
 788          model.set({y: true});
 789          break;
 790        case 1:
 791          deepEqual(this.changedAttributes(), {x: true, y: true});
 792          equal(model.previous('x'), undefined);
 793          model.set({z: true});
 794          break;
 795        case 2:
 796          deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
 797          equal(model.previous('y'), undefined);
 798          break;
 799        default:
 800          ok(false);
 801      }
 802    });
 803    model.set({x: true});
 804  });
 805
 806  test("nested change", 3, function() {
 807    var count = 0;
 808    var model = new Backbone.Model();
 809    model.on('change', function() {
 810      switch(count++) {
 811        case 0:
 812          deepEqual(this.changedAttributes(), {x: true});
 813          model.set({y: true, z: true});
 814          break;
 815        case 1:
 816          deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
 817          break;
 818        case 2:
 819          deepEqual(this.changedAttributes(), {z: false});
 820          break;
 821        default:
 822          ok(false);
 823      }
 824    });
 825    model.set({x: true});
 826    model.set({z: false});
 827  });
 828
 829  test("multiple nested changes", 1, function() {
 830    var changes = [];
 831    var model = new Backbone.Model();
 832    model.on('change:b', function(model, val) { changes.push(val); });
 833    model.on('change', function() {
 834      model.set({b: 1});
 835    });
 836    model.set({b: 0});
 837    deepEqual(changes, [0, 1]);
 838  });
 839
 840  test("nested set multiple times", 1, function() {
 841    var model = new Backbone.Model();
 842    model.on('change:b', function() {
 843      ok(true);
 844    });
 845    model.on('change:a', function() {
 846      model.set({b: true});
 847      model.set({b: true});
 848    });
 849    model.set({a: true});
 850  });
 851
 852  test("#1122 - clear does not alter options.", 1, function() {
 853    var model = new Backbone.Model();
 854    var options = {};
 855    model.clear(options);
 856    ok(!options.unset);
 857  });
 858
 859  test("#1122 - unset does not alter options.", 1, function() {
 860    var model = new Backbone.Model();
 861    var options = {};
 862    model.unset('x', options);
 863    ok(!options.unset);
 864  });
 865
 866  test("#1355 - `options` is passed to success callbacks", 3, function() {
 867    var model = new Backbone.Model();
 868    var opts = {
 869      success: function( model, resp, options ) {
 870        ok(options);
 871      }
 872    };
 873    model.sync = function(method, model, options) {
 874      options.success();
 875    };
 876    model.save({id: 1}, opts);
 877    model.fetch(opts);
 878    model.destroy(opts);
 879  });
 880
 881  test("#1412 - Trigger 'sync' event.", 3, function() {
 882    var model = new Backbone.Model({id: 1});
 883    model.sync = function (method, model, options) { options.success(); };
 884    model.on('sync', function(){ ok(true); });
 885    model.fetch();
 886    model.save();
 887    model.destroy();
 888  });
 889
 890  test("#1365 - Destroy: New models execute success callback.", 2, function() {
 891    new Backbone.Model()
 892    .on('sync', function() { ok(false); })
 893    .on('destroy', function(){ ok(true); })
 894    .destroy({ success: function(){ ok(true); }});
 895  });
 896
 897  test("#1433 - Save: An invalid model cannot be persisted.", 1, function() {
 898    var model = new Backbone.Model;
 899    model.validate = function(){ return 'invalid'; };
 900    model.sync = function(){ ok(false); };
 901    strictEqual(model.save(), false);
 902  });
 903
 904  test("#1377 - Save without attrs triggers 'error'.", 1, function() {
 905    var Model = Backbone.Model.extend({
 906      url: '/test/',
 907      sync: function(method, model, options){ options.success(); },
 908      validate: function(){ return 'invalid'; }
 909    });
 910    var model = new Model({id: 1});
 911    model.on('invalid', function(){ ok(true); });
 912    model.save();
 913  });
 914
 915  test("#1545 - `undefined` can be passed to a model constructor without coersion", function() {
 916    var Model = Backbone.Model.extend({
 917      defaults: { one: 1 },
 918      initialize : function(attrs, opts) {
 919        equal(attrs, undefined);
 920      }
 921    });
 922    var emptyattrs = new Model();
 923    var undefinedattrs = new Model(undefined);
 924  });
 925
 926  asyncTest("#1478 - Model `save` does not trigger change on unchanged attributes", 0, function() {
 927    var Model = Backbone.Model.extend({
 928      sync: function(method, model, options) {
 929        setTimeout(function(){
 930          options.success();
 931          start();
 932        }, 0);
 933      }
 934    });
 935    new Model({x: true})
 936    .on('change:x', function(){ ok(false); })
 937    .save(null, {wait: true});
 938  });
 939
 940  test("#1791 - `attributes` is available for `parse`", function() {
 941    var Model = Backbone.Model.extend({
 942      parse: function() { this.has('a'); } // shouldn't throw an error
 943    });
 944    var model = new Model(null, {parse: true});
 945    expect(0);
 946  });
 947
 948  test("#1943 change calculations should use _.isEqual", function() {
 949    var model = new Backbone.Model({a: {key: 'value'}});
 950    model.set('a', {key:'value'});
 951    equal(model.changedAttributes(), false);
 952  });
 953
 954  test("#1964 - final `change` event is always fired, regardless of interim changes", 1, function () {
 955    var model = new Backbone.Model();
 956    model.on('change:property', function() {
 957      model.set('property', 'bar');
 958    });
 959    model.on('change', function() {
 960      ok(true);
 961    });
 962    model.set('property', 'foo');
 963  });
 964
 965  test("isValid", function() {
 966    var model = new Backbone.Model({valid: true});
 967    model.validate = function(attrs) {
 968      if (!attrs.valid) return "invalid";
 969    };
 970    equal(model.isValid(), true);
 971    equal(model.set({valid: false}, {validate:true}), false);
 972    equal(model.isValid(), true);
 973    model.set({valid:false});
 974    equal(model.isValid(), false);
 975    ok(!model.set('valid', false, {validate: true}));
 976  });
 977
 978  test("#1179 - isValid returns true in the absence of validate.", 1, function() {
 979    var model = new Backbone.Model();
 980    model.validate = null;
 981    ok(model.isValid());
 982  });
 983
 984  test("#1961 - Creating a model with {validate:true} will call validate and use the error callback", function () {
 985    var Model = Backbone.Model.extend({
 986      validate: function (attrs) {
 987        if (attrs.id === 1) return "This shouldn't happen";
 988      }
 989    });
 990    var model = new Model({id: 1}, {validate: true});
 991    equal(model.validationError, "This shouldn't happen");
 992  });
 993
 994  test("toJSON receives attrs during save(..., {wait: true})", 1, function() {
 995    var Model = Backbone.Model.extend({
 996      url: '/test',
 997      toJSON: function() {
 998        strictEqual(this.attributes.x, 1);
 999        return _.clone(this.attributes);
1000      }
1001    });
1002    var model = new Model;
1003    model.save({x: 1}, {wait: true});
1004  });
1005
1006});