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

/test/model.js

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