PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/test/model.js

https://github.com/ustas-v/backbone
JavaScript | 534 lines | 471 code | 60 blank | 3 comment | 12 complexity | 5937a6e145a7e8ad9a3994a2e0c988fd MD5 | raw file
  1. $(document).ready(function() {
  2. module("Backbone.Model");
  3. // Variable to catch the last request.
  4. window.lastRequest = null;
  5. window.originalSync = Backbone.sync;
  6. // Stub out Backbone.request...
  7. Backbone.sync = function() {
  8. lastRequest = _.toArray(arguments);
  9. };
  10. var attrs = {
  11. id : '1-the-tempest',
  12. title : "The Tempest",
  13. author : "Bill Shakespeare",
  14. length : 123
  15. };
  16. var proxy = Backbone.Model.extend();
  17. var doc = new proxy(attrs);
  18. var klass = Backbone.Collection.extend({
  19. url : function() { return '/collection'; }
  20. });
  21. var collection = new klass();
  22. collection.add(doc);
  23. test("Model: initialize", function() {
  24. var Model = Backbone.Model.extend({
  25. initialize: function() {
  26. this.one = 1;
  27. equal(this.collection, collection);
  28. }
  29. });
  30. var model = new Model({}, {collection: collection});
  31. equal(model.one, 1);
  32. equal(model.collection, collection);
  33. });
  34. test("Model: initialize with attributes and options", function() {
  35. var Model = Backbone.Model.extend({
  36. initialize: function(attributes, options) {
  37. this.one = options.one;
  38. }
  39. });
  40. var model = new Model({}, {one: 1});
  41. equal(model.one, 1);
  42. });
  43. test("Model: initialize with parsed attributes", function() {
  44. var Model = Backbone.Model.extend({
  45. parse: function(obj) {
  46. obj.value += 1;
  47. return obj;
  48. }
  49. });
  50. var model = new Model({value: 1}, {parse: true});
  51. equal(model.get('value'), 2);
  52. });
  53. test("Model: url", function() {
  54. equal(doc.url(), '/collection/1-the-tempest');
  55. doc.collection.url = '/collection/';
  56. equal(doc.url(), '/collection/1-the-tempest');
  57. doc.collection = null;
  58. var failed = false;
  59. try {
  60. doc.url();
  61. } catch (e) {
  62. failed = true;
  63. }
  64. equal(failed, true);
  65. doc.collection = collection;
  66. });
  67. test("Model: url when using urlRoot, and uri encoding", function() {
  68. var Model = Backbone.Model.extend({
  69. urlRoot: '/collection'
  70. });
  71. var model = new Model();
  72. equal(model.url(), '/collection');
  73. model.set({id: '+1+'});
  74. equal(model.url(), '/collection/%2B1%2B');
  75. });
  76. test("Model: url when using urlRoot as a function to determine urlRoot at runtime", function() {
  77. var Model = Backbone.Model.extend({
  78. urlRoot: function() {
  79. return '/nested/' + this.get('parent_id') + '/collection';
  80. }
  81. });
  82. var model = new Model({parent_id: 1});
  83. equal(model.url(), '/nested/1/collection');
  84. model.set({id: 2});
  85. equal(model.url(), '/nested/1/collection/2');
  86. });
  87. test("Model: clone", function() {
  88. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  89. a = new Backbone.Model(attrs);
  90. b = a.clone();
  91. equal(a.get('foo'), 1);
  92. equal(a.get('bar'), 2);
  93. equal(a.get('baz'), 3);
  94. equal(b.get('foo'), a.get('foo'), "Foo should be the same on the clone.");
  95. equal(b.get('bar'), a.get('bar'), "Bar should be the same on the clone.");
  96. equal(b.get('baz'), a.get('baz'), "Baz should be the same on the clone.");
  97. a.set({foo : 100});
  98. equal(a.get('foo'), 100);
  99. equal(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
  100. });
  101. test("Model: isNew", function() {
  102. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  103. a = new Backbone.Model(attrs);
  104. ok(a.isNew(), "it should be new");
  105. attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
  106. a = new Backbone.Model(attrs);
  107. ok(!a.isNew(), "any defined ID is legal, negative or positive");
  108. attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 };
  109. a = new Backbone.Model(attrs);
  110. ok(!a.isNew(), "any defined ID is legal, including zero");
  111. ok( new Backbone.Model({ }).isNew(), "is true when there is no id");
  112. ok(!new Backbone.Model({ 'id': 2 }).isNew(), "is false for a positive integer");
  113. ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integer");
  114. });
  115. test("Model: get", function() {
  116. equal(doc.get('title'), 'The Tempest');
  117. equal(doc.get('author'), 'Bill Shakespeare');
  118. });
  119. test("Model: escape", function() {
  120. equal(doc.escape('title'), 'The Tempest');
  121. doc.set({audience: 'Bill & Bob'});
  122. equal(doc.escape('audience'), 'Bill & Bob');
  123. doc.set({audience: 'Tim > Joan'});
  124. equal(doc.escape('audience'), 'Tim > Joan');
  125. doc.set({audience: 10101});
  126. equal(doc.escape('audience'), '10101');
  127. doc.unset('audience');
  128. equal(doc.escape('audience'), '');
  129. });
  130. test("Model: has", function() {
  131. attrs = {};
  132. a = new Backbone.Model(attrs);
  133. equal(a.has("name"), false);
  134. _([true, "Truth!", 1, false, '', 0]).each(function(value) {
  135. a.set({'name': value});
  136. equal(a.has("name"), true);
  137. });
  138. a.unset('name');
  139. equal(a.has('name'), false);
  140. _([null, undefined]).each(function(value) {
  141. a.set({'name': value});
  142. equal(a.has("name"), false);
  143. });
  144. });
  145. test("Model: set and unset", function() {
  146. expect(8);
  147. attrs = {id: 'id', foo: 1, bar: 2, baz: 3};
  148. a = new Backbone.Model(attrs);
  149. var changeCount = 0;
  150. a.on("change:foo", function() { changeCount += 1; });
  151. a.set({'foo': 2});
  152. ok(a.get('foo') == 2, "Foo should have changed.");
  153. ok(changeCount == 1, "Change count should have incremented.");
  154. a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
  155. ok(a.get('foo') == 2, "Foo should NOT have changed, still 2");
  156. ok(changeCount == 1, "Change count should NOT have incremented.");
  157. a.validate = function(attrs) {
  158. equal(attrs.foo, void 0, 'ignore values when unsetting');
  159. };
  160. a.unset('foo');
  161. ok(a.get('foo') == null, "Foo should have changed");
  162. delete a.validate;
  163. ok(changeCount == 2, "Change count should have incremented for unset.");
  164. a.unset('id');
  165. equal(a.id, undefined, "Unsetting the id should remove the id property.");
  166. });
  167. test("Model: multiple unsets", function() {
  168. var i = 0;
  169. var counter = function(){ i++; };
  170. var model = new Backbone.Model({a: 1});
  171. model.on("change:a", counter);
  172. model.set({a: 2});
  173. model.unset('a');
  174. model.unset('a');
  175. equal(i, 2, 'Unset does not fire an event for missing attributes.');
  176. });
  177. test("Model: unset and changedAttributes", function() {
  178. var model = new Backbone.Model({a: 1});
  179. model.unset('a', {silent: true});
  180. var changedAttributes = model.changedAttributes();
  181. ok('a' in changedAttributes, 'changedAttributes should contain unset properties');
  182. changedAttributes = model.changedAttributes();
  183. ok('a' in changedAttributes, 'changedAttributes should contain unset properties when running changedAttributes again after an unset.');
  184. });
  185. test("Model: using a non-default id attribute.", function() {
  186. var MongoModel = Backbone.Model.extend({idAttribute : '_id'});
  187. var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
  188. equal(model.get('id'), 'eye-dee');
  189. equal(model.id, 25);
  190. equal(model.isNew(), false);
  191. model.unset('_id');
  192. equal(model.id, undefined);
  193. equal(model.isNew(), true);
  194. });
  195. test("Model: set an empty string", function() {
  196. var model = new Backbone.Model({name : "Model"});
  197. model.set({name : ''});
  198. equal(model.get('name'), '');
  199. });
  200. test("Model: clear", function() {
  201. var changed;
  202. var model = new Backbone.Model({id: 1, name : "Model"});
  203. model.on("change:name", function(){ changed = true; });
  204. model.on("change", function() {
  205. var changedAttrs = model.changedAttributes();
  206. ok('name' in changedAttrs);
  207. });
  208. model.clear();
  209. equal(changed, true);
  210. equal(model.get('name'), undefined);
  211. });
  212. test("Model: defaults", function() {
  213. var Defaulted = Backbone.Model.extend({
  214. defaults: {
  215. "one": 1,
  216. "two": 2
  217. }
  218. });
  219. var model = new Defaulted({two: null});
  220. equal(model.get('one'), 1);
  221. equal(model.get('two'), null);
  222. Defaulted = Backbone.Model.extend({
  223. defaults: function() {
  224. return {
  225. "one": 3,
  226. "two": 4
  227. };
  228. }
  229. });
  230. var model = new Defaulted({two: null});
  231. equal(model.get('one'), 3);
  232. equal(model.get('two'), null);
  233. });
  234. test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
  235. var model = new Backbone.Model({name : "Tim", age : 10});
  236. equal(model.changedAttributes(), false);
  237. model.on('change', function() {
  238. ok(model.hasChanged('name'), 'name changed');
  239. ok(!model.hasChanged('age'), 'age did not');
  240. ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
  241. equal(model.previous('name'), 'Tim');
  242. ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
  243. });
  244. model.set({name : 'Rob'}, {silent : true});
  245. equal(model.hasChanged(), true);
  246. equal(model.hasChanged('name'), true);
  247. model.change();
  248. equal(model.get('name'), 'Rob');
  249. });
  250. test("Model: change with options", function() {
  251. var value;
  252. var model = new Backbone.Model({name: 'Rob'});
  253. model.on('change', function(model, options) {
  254. value = options.prefix + model.get('name');
  255. });
  256. model.set({name: 'Bob'}, {silent: true});
  257. model.change({prefix: 'Mr. '});
  258. equal(value, 'Mr. Bob');
  259. model.set({name: 'Sue'}, {prefix: 'Ms. '});
  260. equal(value, 'Ms. Sue');
  261. });
  262. test("Model: change after initialize", function () {
  263. var changed = 0;
  264. var attrs = {id: 1, label: 'c'};
  265. var obj = new Backbone.Model(attrs);
  266. obj.on('change', function() { changed += 1; });
  267. obj.set(attrs);
  268. equal(changed, 0);
  269. });
  270. test("Model: save within change event", function () {
  271. var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
  272. model.on('change', function () {
  273. model.save();
  274. ok(_.isEqual(lastRequest[1], model));
  275. });
  276. model.set({lastName: 'Hicks'});
  277. });
  278. test("Model: validate after save", function() {
  279. var lastError, model = new Backbone.Model();
  280. model.validate = function(attrs) {
  281. if (attrs.admin) return "Can't change admin status.";
  282. };
  283. model.sync = function(method, model, options) {
  284. options.success.call(this, {admin: true});
  285. };
  286. model.save(null, {error: function(model, error) {
  287. lastError = error;
  288. }});
  289. equal(lastError, "Can't change admin status.");
  290. });
  291. test("Model: save", function() {
  292. doc.save({title : "Henry V"});
  293. equal(lastRequest[0], 'update');
  294. ok(_.isEqual(lastRequest[1], doc));
  295. });
  296. test("Model: save in positional style", function() {
  297. var model = new Backbone.Model();
  298. model.sync = function(method, model, options) {
  299. options.success();
  300. };
  301. model.save('title', 'Twelfth Night');
  302. equal(model.get('title'), 'Twelfth Night');
  303. });
  304. test("Model: fetch", function() {
  305. doc.fetch();
  306. ok(lastRequest[0], 'read');
  307. ok(_.isEqual(lastRequest[1], doc));
  308. });
  309. test("Model: destroy", function() {
  310. doc.destroy();
  311. equal(lastRequest[0], 'delete');
  312. ok(_.isEqual(lastRequest[1], doc));
  313. });
  314. test("Model: non-persisted destroy", function() {
  315. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  316. a = new Backbone.Model(attrs);
  317. a.sync = function() { throw "should not be called"; };
  318. a.destroy();
  319. ok(true, "non-persisted model should not call sync");
  320. });
  321. test("Model: validate", function() {
  322. var lastError;
  323. var model = new Backbone.Model();
  324. model.validate = function(attrs) {
  325. if (attrs.admin) return "Can't change admin status.";
  326. };
  327. model.on('error', function(model, error) {
  328. lastError = error;
  329. });
  330. var result = model.set({a: 100});
  331. equal(result, model);
  332. equal(model.get('a'), 100);
  333. equal(lastError, undefined);
  334. result = model.set({admin: true}, {silent: true});
  335. equal(lastError, undefined);
  336. equal(model.get('admin'), true);
  337. result = model.set({a: 200, admin: true});
  338. equal(result, false);
  339. equal(model.get('a'), 100);
  340. equal(lastError, "Can't change admin status.");
  341. });
  342. test("Model: validate on unset and clear", function() {
  343. var error;
  344. var model = new Backbone.Model({name: "One"});
  345. model.validate = function(attrs) {
  346. if ("name" in attrs) {
  347. if (!attrs.name) {
  348. error = true;
  349. return "No thanks.";
  350. }
  351. }
  352. };
  353. model.set({name: "Two"});
  354. equal(model.get('name'), 'Two');
  355. equal(error, undefined);
  356. model.unset('name');
  357. equal(error, true);
  358. equal(model.get('name'), 'Two');
  359. model.clear();
  360. equal(model.get('name'), 'Two');
  361. delete model.validate;
  362. model.clear();
  363. equal(model.get('name'), undefined);
  364. });
  365. test("Model: validate with error callback", function() {
  366. var lastError, boundError;
  367. var model = new Backbone.Model();
  368. model.validate = function(attrs) {
  369. if (attrs.admin) return "Can't change admin status.";
  370. };
  371. var callback = function(model, error) {
  372. lastError = error;
  373. };
  374. model.on('error', function(model, error) {
  375. boundError = true;
  376. });
  377. var result = model.set({a: 100}, {error: callback});
  378. equal(result, model);
  379. equal(model.get('a'), 100);
  380. equal(lastError, undefined);
  381. equal(boundError, undefined);
  382. result = model.set({a: 200, admin: true}, {error: callback});
  383. equal(result, false);
  384. equal(model.get('a'), 100);
  385. equal(lastError, "Can't change admin status.");
  386. equal(boundError, undefined);
  387. });
  388. test("Model: defaults always extend attrs (#459)", function() {
  389. var Defaulted = Backbone.Model.extend({
  390. defaults: {one: 1},
  391. initialize : function(attrs, opts) {
  392. equal(this.attributes.one, 1);
  393. }
  394. });
  395. var providedattrs = new Defaulted({});
  396. var emptyattrs = new Defaulted();
  397. });
  398. test("Model: Inherit class properties", function() {
  399. var Parent = Backbone.Model.extend({
  400. instancePropSame: function() {},
  401. instancePropDiff: function() {}
  402. }, {
  403. classProp: function() {}
  404. });
  405. var Child = Parent.extend({
  406. instancePropDiff: function() {}
  407. });
  408. var adult = new Parent;
  409. var kid = new Child;
  410. equal(Child.classProp, Parent.classProp);
  411. notEqual(Child.classProp, undefined);
  412. equal(kid.instancePropSame, adult.instancePropSame);
  413. notEqual(kid.instancePropSame, undefined);
  414. notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
  415. notEqual(Child.prototype.instancePropDiff, undefined);
  416. });
  417. test("Model: Nested change events don't clobber previous attributes", function() {
  418. var A = Backbone.Model.extend({
  419. initialize: function() {
  420. this.on("change:state", function(a, newState) {
  421. equal(a.previous('state'), undefined);
  422. equal(newState, 'hello');
  423. // Fire a nested change event.
  424. this.set({ other: "whatever" });
  425. });
  426. }
  427. });
  428. var B = Backbone.Model.extend({
  429. initialize: function() {
  430. this.get("a").on("change:state", function(a, newState) {
  431. equal(a.previous('state'), undefined);
  432. equal(newState, 'hello');
  433. });
  434. }
  435. });
  436. a = new A();
  437. b = new B({a: a});
  438. a.set({state: 'hello'});
  439. });
  440. test("Model: Multiple nested calls to set", function() {
  441. var counter = 0, model = new Backbone.Model({});
  442. model.on('change', function() {
  443. counter++;
  444. model.set({b: 1});
  445. model.set({a: 1});
  446. })
  447. .set({a: 1});
  448. equal(counter, 1, 'change is only triggered once');
  449. });
  450. test("hasChanged/set should use same comparison", function() {
  451. expect(2);
  452. var changed = 0, model = new Backbone.Model({a: null});
  453. model.on('change', function() {
  454. ok(this.hasChanged('a'));
  455. })
  456. .on('change:a', function() {
  457. changed++;
  458. })
  459. .set({a: undefined});
  460. equal(changed, 1);
  461. });
  462. test("#582, #425, change:attribute callbacks should fire after all changes have occurred", 9, function() {
  463. var model = new Backbone.Model;
  464. var assertion = function() {
  465. equal(model.get('a'), 'a');
  466. equal(model.get('b'), 'b');
  467. equal(model.get('c'), 'c');
  468. };
  469. model.on('change:a', assertion);
  470. model.on('change:b', assertion);
  471. model.on('change:c', assertion);
  472. model.set({a: 'a', b: 'b', c: 'c'});
  473. });
  474. });