PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 84ms RepoModel.GetById 1ms app.codeStats 1ms

/test/model.js

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