PageRenderTime 4ms CodeModel.GetById 56ms app.highlight 67ms RepoModel.GetById 1ms app.codeStats 1ms

/test/model.js

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