PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 61ms RepoModel.GetById 1ms app.codeStats 0ms

/test/backbone/test/model.js

https://github.com/auggernaut/backbone-query-parameters
JavaScript | 1066 lines | 953 code | 112 blank | 1 comment | 14 complexity | 44c23069d080149ac22e5852ab36fafa 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, this, {admin: true}, options);
 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 with PATCH", function() {
 422    doc.clear().set({id: 1, a: 1, b: 2, c: 3, d: 4});
 423    doc.save();
 424    equal(this.syncArgs.method, 'update');
 425    equal(this.syncArgs.options.attrs, undefined);
 426
 427    doc.save({b: 2, d: 4}, {patch: true});
 428    equal(this.syncArgs.method, 'patch');
 429    equal(_.size(this.syncArgs.options.attrs), 2);
 430    equal(this.syncArgs.options.attrs.d, 4);
 431    equal(this.syncArgs.options.attrs.a, undefined);
 432    equal(this.ajaxSettings.data, "{\"b\":2,\"d\":4}");
 433  });
 434
 435  test("save in positional style", 1, function() {
 436    var model = new Backbone.Model();
 437    model.sync = function(method, model, options) {
 438      options.success(model, {}, options);
 439    };
 440    model.save('title', 'Twelfth Night');
 441    equal(model.get('title'), 'Twelfth Night');
 442  });
 443
 444  test("save with non-object success response", 2, function () {
 445    var model = new Backbone.Model();
 446    model.sync = function(method, model, options) {
 447      options.success(model, '', options);
 448      options.success(model, null, options);
 449    };
 450    model.save({testing:'empty'}, {
 451      success: function (model) {
 452        deepEqual(model.attributes, {testing:'empty'});
 453      }
 454    });
 455  });
 456
 457  test("fetch", 2, function() {
 458    doc.fetch();
 459    equal(this.syncArgs.method, 'read');
 460    ok(_.isEqual(this.syncArgs.model, doc));
 461  });
 462
 463  test("destroy", 3, function() {
 464    doc.destroy();
 465    equal(this.syncArgs.method, 'delete');
 466    ok(_.isEqual(this.syncArgs.model, doc));
 467
 468    var newModel = new Backbone.Model;
 469    equal(newModel.destroy(), false);
 470  });
 471
 472  test("non-persisted destroy", 1, function() {
 473    var a = new Backbone.Model({ 'foo': 1, 'bar': 2, 'baz': 3});
 474    a.sync = function() { throw "should not be called"; };
 475    a.destroy();
 476    ok(true, "non-persisted model should not call sync");
 477  });
 478
 479  test("validate", function() {
 480    var lastError;
 481    var model = new Backbone.Model();
 482    model.validate = function(attrs) {
 483      if (attrs.admin != this.get('admin')) return "Can't change admin status.";
 484    };
 485    model.on('invalid', function(model, error) {
 486      lastError = error;
 487    });
 488    var result = model.set({a: 100});
 489    equal(result, model);
 490    equal(model.get('a'), 100);
 491    equal(lastError, undefined);
 492    result = model.set({admin: true});
 493    equal(model.get('admin'), true);
 494    result = model.set({a: 200, admin: false}, {validate:true});
 495    equal(lastError, "Can't change admin status.");
 496    equal(result, false);
 497    equal(model.get('a'), 100);
 498  });
 499
 500  test("validate on unset and clear", 6, function() {
 501    var error;
 502    var model = new Backbone.Model({name: "One"});
 503    model.validate = function(attrs) {
 504      if (!attrs.name) {
 505        error = true;
 506        return "No thanks.";
 507      }
 508    };
 509    model.set({name: "Two"});
 510    equal(model.get('name'), 'Two');
 511    equal(error, undefined);
 512    model.unset('name', {validate: true});
 513    equal(error, true);
 514    equal(model.get('name'), 'Two');
 515    model.clear({validate:true});
 516    equal(model.get('name'), 'Two');
 517    delete model.validate;
 518    model.clear();
 519    equal(model.get('name'), undefined);
 520  });
 521
 522  test("validate with error callback", 8, function() {
 523    var lastError, boundError;
 524    var model = new Backbone.Model();
 525    model.validate = function(attrs) {
 526      if (attrs.admin) return "Can't change admin status.";
 527    };
 528    model.on('invalid', function(model, error) {
 529      boundError = true;
 530    });
 531    var result = model.set({a: 100}, {validate:true});
 532    equal(result, model);
 533    equal(model.get('a'), 100);
 534    equal(model.validationError, null);
 535    equal(boundError, undefined);
 536    result = model.set({a: 200, admin: true}, {validate:true});
 537    equal(result, false);
 538    equal(model.get('a'), 100);
 539    equal(model.validationError, "Can't change admin status.");
 540    equal(boundError, true);
 541  });
 542
 543  test("defaults always extend attrs (#459)", 2, function() {
 544    var Defaulted = Backbone.Model.extend({
 545      defaults: {one: 1},
 546      initialize : function(attrs, opts) {
 547        equal(this.attributes.one, 1);
 548      }
 549    });
 550    var providedattrs = new Defaulted({});
 551    var emptyattrs = new Defaulted();
 552  });
 553
 554  test("Inherit class properties", 6, function() {
 555    var Parent = Backbone.Model.extend({
 556      instancePropSame: function() {},
 557      instancePropDiff: function() {}
 558    }, {
 559      classProp: function() {}
 560    });
 561    var Child = Parent.extend({
 562      instancePropDiff: function() {}
 563    });
 564
 565    var adult = new Parent;
 566    var kid   = new Child;
 567
 568    equal(Child.classProp, Parent.classProp);
 569    notEqual(Child.classProp, undefined);
 570
 571    equal(kid.instancePropSame, adult.instancePropSame);
 572    notEqual(kid.instancePropSame, undefined);
 573
 574    notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
 575    notEqual(Child.prototype.instancePropDiff, undefined);
 576  });
 577
 578  test("Nested change events don't clobber previous attributes", 4, function() {
 579    new Backbone.Model()
 580    .on('change:state', function(model, newState) {
 581      equal(model.previous('state'), undefined);
 582      equal(newState, 'hello');
 583      // Fire a nested change event.
 584      model.set({other: 'whatever'});
 585    })
 586    .on('change:state', function(model, newState) {
 587      equal(model.previous('state'), undefined);
 588      equal(newState, 'hello');
 589    })
 590    .set({state: 'hello'});
 591  });
 592
 593  test("hasChanged/set should use same comparison", 2, function() {
 594    var changed = 0, model = new Backbone.Model({a: null});
 595    model.on('change', function() {
 596      ok(this.hasChanged('a'));
 597    })
 598    .on('change:a', function() {
 599      changed++;
 600    })
 601    .set({a: undefined});
 602    equal(changed, 1);
 603  });
 604
 605  test("#582, #425, change:attribute callbacks should fire after all changes have occurred", 9, function() {
 606    var model = new Backbone.Model;
 607
 608    var assertion = function() {
 609      equal(model.get('a'), 'a');
 610      equal(model.get('b'), 'b');
 611      equal(model.get('c'), 'c');
 612    };
 613
 614    model.on('change:a', assertion);
 615    model.on('change:b', assertion);
 616    model.on('change:c', assertion);
 617
 618    model.set({a: 'a', b: 'b', c: 'c'});
 619  });
 620
 621  test("#871, set with attributes property", 1, function() {
 622    var model = new Backbone.Model();
 623    model.set({attributes: true});
 624    ok(model.has('attributes'));
 625  });
 626
 627  test("set value regardless of equality/change", 1, function() {
 628    var model = new Backbone.Model({x: []});
 629    var a = [];
 630    model.set({x: a});
 631    ok(model.get('x') === a);
 632  });
 633
 634  test("set same value does not trigger change", 0, function() {
 635    var model = new Backbone.Model({x: 1});
 636    model.on('change change:x', function() { ok(false); });
 637    model.set({x: 1});
 638    model.set({x: 1});
 639  });
 640
 641  test("unset does not fire a change for undefined attributes", 0, function() {
 642    var model = new Backbone.Model({x: undefined});
 643    model.on('change:x', function(){ ok(false); });
 644    model.unset('x');
 645  });
 646
 647  test("set: undefined values", 1, function() {
 648    var model = new Backbone.Model({x: undefined});
 649    ok('x' in model.attributes);
 650  });
 651
 652  test("hasChanged works outside of change events, and true within", 6, function() {
 653    var model = new Backbone.Model({x: 1});
 654    model.on('change:x', function() {
 655      ok(model.hasChanged('x'));
 656      equal(model.get('x'), 1);
 657    });
 658    model.set({x: 2}, {silent: true});
 659    ok(model.hasChanged());
 660    equal(model.hasChanged('x'), true);
 661    model.set({x: 1});
 662    ok(model.hasChanged());
 663    equal(model.hasChanged('x'), true);
 664  });
 665
 666  test("hasChanged gets cleared on the following set", 4, function() {
 667    var model = new Backbone.Model;
 668    model.set({x: 1});
 669    ok(model.hasChanged());
 670    model.set({x: 1});
 671    ok(!model.hasChanged());
 672    model.set({x: 2});
 673    ok(model.hasChanged());
 674    model.set({});
 675    ok(!model.hasChanged());
 676  });
 677
 678  test("save with `wait` succeeds without `validate`", 1, function() {
 679    var model = new Backbone.Model();
 680    model.url = '/test';
 681    model.save({x: 1}, {wait: true});
 682    ok(this.syncArgs.model === model);
 683  });
 684
 685  test("`hasChanged` for falsey keys", 2, function() {
 686    var model = new Backbone.Model();
 687    model.set({x: true}, {silent: true});
 688    ok(!model.hasChanged(0));
 689    ok(!model.hasChanged(''));
 690  });
 691
 692  test("`previous` for falsey keys", 2, function() {
 693    var model = new Backbone.Model({0: true, '': true});
 694    model.set({0: false, '': false}, {silent: true});
 695    equal(model.previous(0), true);
 696    equal(model.previous(''), true);
 697  });
 698
 699  test("`save` with `wait` sends correct attributes", 5, function() {
 700    var changed = 0;
 701    var model = new Backbone.Model({x: 1, y: 2});
 702    model.url = '/test';
 703    model.on('change:x', function() { changed++; });
 704    model.save({x: 3}, {wait: true});
 705    deepEqual(JSON.parse(this.ajaxSettings.data), {x: 3, y: 2});
 706    equal(model.get('x'), 1);
 707    equal(changed, 0);
 708    this.syncArgs.options.success({});
 709    equal(model.get('x'), 3);
 710    equal(changed, 1);
 711  });
 712
 713  test("a failed `save` with `wait` doesn't leave attributes behind", 1, function() {
 714    var model = new Backbone.Model;
 715    model.url = '/test';
 716    model.save({x: 1}, {wait: true});
 717    equal(model.get('x'), void 0);
 718  });
 719
 720  test("#1030 - `save` with `wait` results in correct attributes if success is called during sync", 2, function() {
 721    var model = new Backbone.Model({x: 1, y: 2});
 722    model.sync = function(method, model, options) {
 723      options.success(model, {}, options);
 724    };
 725    model.on("change:x", function() { ok(true); });
 726    model.save({x: 3}, {wait: true});
 727    equal(model.get('x'), 3);
 728  });
 729
 730  test("save with wait validates attributes", function() {
 731    var model = new Backbone.Model();
 732    model.url = '/test';
 733    model.validate = function() { ok(true); };
 734    model.save({x: 1}, {wait: true});
 735  });
 736
 737  test("nested `set` during `'change:attr'`", 2, function() {
 738    var events = [];
 739    var model = new Backbone.Model();
 740    model.on('all', function(event) { events.push(event); });
 741    model.on('change', function() {
 742      model.set({z: true}, {silent:true});
 743    });
 744    model.on('change:x', function() {
 745      model.set({y: true});
 746    });
 747    model.set({x: true});
 748    deepEqual(events, ['change:y', 'change:x', 'change']);
 749    events = [];
 750    model.set({z: true});
 751    deepEqual(events, []);
 752  });
 753
 754  test("nested `change` only fires once", 1, function() {
 755    var model = new Backbone.Model();
 756    model.on('change', function() {
 757      ok(true);
 758      model.set({x: true});
 759    });
 760    model.set({x: true});
 761  });
 762
 763  test("nested `set` during `'change'`", 6, function() {
 764    var count = 0;
 765    var model = new Backbone.Model();
 766    model.on('change', function() {
 767      switch(count++) {
 768        case 0:
 769          deepEqual(this.changedAttributes(), {x: true});
 770          equal(model.previous('x'), undefined);
 771          model.set({y: true});
 772          break;
 773        case 1:
 774          deepEqual(this.changedAttributes(), {x: true, y: true});
 775          equal(model.previous('x'), undefined);
 776          model.set({z: true});
 777          break;
 778        case 2:
 779          deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
 780          equal(model.previous('y'), undefined);
 781          break;
 782        default:
 783          ok(false);
 784      }
 785    });
 786    model.set({x: true});
 787  });
 788
 789  test("nested `change` with silent", 3, function() {
 790    var count = 0;
 791    var model = new Backbone.Model();
 792    model.on('change:y', function() { ok(false); });
 793    model.on('change', function() {
 794      switch(count++) {
 795        case 0:
 796          deepEqual(this.changedAttributes(), {x: true});
 797          model.set({y: true}, {silent: true});
 798          model.set({z: true});
 799          break;
 800        case 1:
 801          deepEqual(this.changedAttributes(), {x: true, y: true, z: true});
 802          break;
 803        case 2:
 804          deepEqual(this.changedAttributes(), {z: false});
 805          break;
 806        default:
 807          ok(false);
 808      }
 809    });
 810    model.set({x: true});
 811    model.set({z: false});
 812  });
 813
 814  test("nested `change:attr` with silent", 0, function() {
 815    var model = new Backbone.Model();
 816    model.on('change:y', function(){ ok(false); });
 817    model.on('change', function() {
 818      model.set({y: true}, {silent: true});
 819      model.set({z: true});
 820    });
 821    model.set({x: true});
 822  });
 823
 824  test("multiple nested changes with silent", 1, function() {
 825    var model = new Backbone.Model();
 826    model.on('change:x', function() {
 827      model.set({y: 1}, {silent: true});
 828      model.set({y: 2});
 829    });
 830    model.on('change:y', function(model, val) {
 831      equal(val, 2);
 832    });
 833    model.set({x: true});
 834  });
 835
 836  test("multiple nested changes with silent", 1, function() {
 837    var changes = [];
 838    var model = new Backbone.Model();
 839    model.on('change:b', function(model, val) { changes.push(val); });
 840    model.on('change', function() {
 841      model.set({b: 1});
 842    });
 843    model.set({b: 0});
 844    deepEqual(changes, [0, 1]);
 845  });
 846
 847  test("basic silent change semantics", 1, function() {
 848    var model = new Backbone.Model;
 849    model.set({x: 1});
 850    model.on('change', function(){ ok(true); });
 851    model.set({x: 2}, {silent: true});
 852    model.set({x: 1});
 853  });
 854
 855  test("nested set multiple times", 1, function() {
 856    var model = new Backbone.Model();
 857    model.on('change:b', function() {
 858      ok(true);
 859    });
 860    model.on('change:a', function() {
 861      model.set({b: true});
 862      model.set({b: true});
 863    });
 864    model.set({a: true});
 865  });
 866
 867  test("#1122 - clear does not alter options.", 1, function() {
 868    var model = new Backbone.Model();
 869    var options = {};
 870    model.clear(options);
 871    ok(!options.unset);
 872  });
 873
 874  test("#1122 - unset does not alter options.", 1, function() {
 875    var model = new Backbone.Model();
 876    var options = {};
 877    model.unset('x', options);
 878    ok(!options.unset);
 879  });
 880
 881  test("#1355 - `options` is passed to success callbacks", 3, function() {
 882    var model = new Backbone.Model();
 883    var opts = {
 884      success: function( model, resp, options ) {
 885        ok(options);
 886      }
 887    };
 888    model.sync = function(method, model, options) {
 889      options.success(model, {}, options);
 890    };
 891    model.save({id: 1}, opts);
 892    model.fetch(opts);
 893    model.destroy(opts);
 894  });
 895
 896  test("#1412 - Trigger 'sync' event.", 3, function() {
 897    var model = new Backbone.Model({id: 1});
 898    model.url = '/test';
 899    model.on('sync', function(){ ok(true); });
 900    Backbone.ajax = function(settings){ settings.success(); };
 901    model.fetch();
 902    model.save();
 903    model.destroy();
 904  });
 905
 906  test("#1365 - Destroy: New models execute success callback.", 2, function() {
 907    new Backbone.Model()
 908    .on('sync', function() { ok(false); })
 909    .on('destroy', function(){ ok(true); })
 910    .destroy({ success: function(){ ok(true); }});
 911  });
 912
 913  test("#1433 - Save: An invalid model cannot be persisted.", 1, function() {
 914    var model = new Backbone.Model;
 915    model.validate = function(){ return 'invalid'; };
 916    model.sync = function(){ ok(false); };
 917    strictEqual(model.save(), false);
 918  });
 919
 920  test("#1377 - Save without attrs triggers 'error'.", 1, function() {
 921    var Model = Backbone.Model.extend({
 922      url: '/test/',
 923      sync: function(method, model, options){ options.success(); },
 924      validate: function(){ return 'invalid'; }
 925    });
 926    var model = new Model({id: 1});
 927    model.on('invalid', function(){ ok(true); });
 928    model.save();
 929  });
 930
 931  test("#1545 - `undefined` can be passed to a model constructor without coersion", function() {
 932    var Model = Backbone.Model.extend({
 933      defaults: { one: 1 },
 934      initialize : function(attrs, opts) {
 935        equal(attrs, undefined);
 936      }
 937    });
 938    var emptyattrs = new Model();
 939    var undefinedattrs = new Model(undefined);
 940  });
 941
 942  asyncTest("#1478 - Model `save` does not trigger change on unchanged attributes", 0, function() {
 943    var Model = Backbone.Model.extend({
 944      sync: function(method, model, options) {
 945        setTimeout(function(){
 946          options.success(model, {}, options);
 947          start();
 948        }, 0);
 949      }
 950    });
 951    new Model({x: true})
 952    .on('change:x', function(){ ok(false); })
 953    .save(null, {wait: true});
 954  });
 955
 956  test("#1664 - Changing from one value, silently to another, back to original triggers a change.", 1, function() {
 957    var model = new Backbone.Model({x:1});
 958    model.on('change:x', function() { ok(true); });
 959    model.set({x:2},{silent:true});
 960    model.set({x:3},{silent:true});
 961    model.set({x:1});
 962  });
 963
 964  test("#1664 - multiple silent changes nested inside a change event", 2, function() {
 965    var changes = [];
 966    var model = new Backbone.Model();
 967    model.on('change', function() {
 968      model.set({a:'c'}, {silent:true});
 969      model.set({b:2}, {silent:true});
 970      model.unset('c', {silent:true});
 971    });
 972    model.on('change:a change:b change:c', function(model, val) { changes.push(val); });
 973    model.set({a:'a', b:1, c:'item'});
 974    deepEqual(changes, ['a',1,'item']);
 975    deepEqual(model.attributes, {a: 'c', b: 2});
 976  });
 977
 978  test("#1791 - `attributes` is available for `parse`", function() {
 979    var Model = Backbone.Model.extend({
 980      parse: function() { this.has('a'); } // shouldn't throw an error
 981    });
 982    var model = new Model(null, {parse: true});
 983    expect(0);
 984  });
 985
 986  test("silent changes in last `change` event back to original triggers change", 2, function() {
 987    var changes = [];
 988    var model = new Backbone.Model();
 989    model.on('change:a change:b change:c', function(model, val) { changes.push(val); });
 990    model.on('change', function() {
 991      model.set({a:'c'}, {silent:true});
 992    });
 993    model.set({a:'a'});
 994    deepEqual(changes, ['a']);
 995    model.set({a:'a'});
 996    deepEqual(changes, ['a', 'a']);
 997  });
 998
 999  test("#1943 change calculations should use _.isEqual", function() {
1000    var model = new Backbone.Model({a: {key: 'value'}});
1001    model.set('a', {key:'value'}, {silent:true});
1002    equal(model.changedAttributes(), false);
1003  });
1004
1005  test("#1964 - final `change` event is always fired, regardless of interim changes", 1, function () {
1006    var model = new Backbone.Model();
1007    model.on('change:property', function() {
1008      model.set('property', 'bar');
1009    });
1010    model.on('change', function() {
1011      ok(true);
1012    });
1013    model.set('property', 'foo');
1014  });
1015
1016  test("isValid", function() {
1017    var model = new Backbone.Model({valid: true});
1018    model.validate = function(attrs) {
1019      if (!attrs.valid) return "invalid";
1020    };
1021    equal(model.isValid(), true);
1022    equal(model.set({valid: false}, {validate:true}), false);
1023    equal(model.isValid(), true);
1024    model.set({valid:false});
1025    equal(model.isValid(), false);
1026    ok(!model.set('valid', false, {validate: true}));
1027  });
1028
1029  test("#1179 - isValid returns true in the absence of validate.", 1, function() {
1030    var model = new Backbone.Model();
1031    model.validate = null;
1032    ok(model.isValid());
1033  });
1034
1035  test("#1961 - Creating a model with {validate:true} will call validate and use the error callback", function () {
1036    var Model = Backbone.Model.extend({
1037      validate: function (attrs) {
1038        if (attrs.id === 1) return "This shouldn't happen";
1039      }
1040    });
1041    var model = new Model({id: 1}, {validate: true});
1042    equal(model.validationError, "This shouldn't happen");
1043  });
1044
1045  test("toJSON receives attrs during save(..., {wait: true})", 1, function() {
1046    var Model = Backbone.Model.extend({
1047      url: '/test',
1048      toJSON: function() {
1049        strictEqual(this.attributes.x, 1);
1050        return _.clone(this.attributes);
1051      }
1052    });
1053    var model = new Model;
1054    model.save({x: 1}, {wait: true});
1055  });
1056
1057  test("#2034 - nested set with silent only triggers one change", 1, function() {
1058    var model = new Backbone.Model();
1059    model.on('change', function() {
1060      model.set({b: true}, {silent: true});
1061      ok(true);
1062    });
1063    model.set({a: true});
1064  });
1065
1066});