PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/model.js

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