PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/test/model.js

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