PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/static/backbone/test/model.js

https://github.com/weblancer23/Final-Project
JavaScript | 495 lines | 437 code | 54 blank | 4 comment | 12 complexity | 2509dffc15ea68d1ac2551a367bbbefd 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. console.log('erroring!');
  287. lastError = error;
  288. }});
  289. equals(lastError, "Can't change admin status.");
  290. });
  291. test("Model: save", function() {
  292. doc.save({title : "Henry V"});
  293. equals(lastRequest[0], 'update');
  294. ok(_.isEqual(lastRequest[1], doc));
  295. });
  296. test("Model: fetch", function() {
  297. doc.fetch();
  298. ok(lastRequest[0], 'read');
  299. ok(_.isEqual(lastRequest[1], doc));
  300. });
  301. test("Model: destroy", function() {
  302. doc.destroy();
  303. equals(lastRequest[0], 'delete');
  304. ok(_.isEqual(lastRequest[1], doc));
  305. });
  306. test("Model: non-persisted destroy", function() {
  307. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  308. a = new Backbone.Model(attrs);
  309. a.sync = function() { throw "should not be called"; };
  310. ok(a.destroy(), "non-persisted model should not call sync");
  311. });
  312. test("Model: validate", function() {
  313. var lastError;
  314. var model = new Backbone.Model();
  315. model.validate = function(attrs) {
  316. if (attrs.admin) return "Can't change admin status.";
  317. };
  318. model.bind('error', function(model, error) {
  319. lastError = error;
  320. });
  321. var result = model.set({a: 100});
  322. equals(result, model);
  323. equals(model.get('a'), 100);
  324. equals(lastError, undefined);
  325. result = model.set({admin: true}, {silent: true});
  326. equals(lastError, undefined);
  327. equals(model.get('admin'), true);
  328. result = model.set({a: 200, admin: true});
  329. equals(result, false);
  330. equals(model.get('a'), 100);
  331. equals(lastError, "Can't change admin status.");
  332. });
  333. test("Model: validate on unset and clear", function() {
  334. var error;
  335. var model = new Backbone.Model({name: "One"});
  336. model.validate = function(attrs) {
  337. if ("name" in attrs) {
  338. if (!attrs.name) {
  339. error = true;
  340. return "No thanks.";
  341. }
  342. }
  343. };
  344. model.set({name: "Two"});
  345. equals(model.get('name'), 'Two');
  346. equals(error, undefined);
  347. model.unset('name');
  348. equals(error, true);
  349. equals(model.get('name'), 'Two');
  350. model.clear();
  351. equals(model.get('name'), 'Two');
  352. delete model.validate;
  353. model.clear();
  354. equals(model.get('name'), undefined);
  355. });
  356. test("Model: validate with error callback", function() {
  357. var lastError, boundError;
  358. var model = new Backbone.Model();
  359. model.validate = function(attrs) {
  360. if (attrs.admin) return "Can't change admin status.";
  361. };
  362. var callback = function(model, error) {
  363. lastError = error;
  364. };
  365. model.bind('error', function(model, error) {
  366. boundError = true;
  367. });
  368. var result = model.set({a: 100}, {error: callback});
  369. equals(result, model);
  370. equals(model.get('a'), 100);
  371. equals(lastError, undefined);
  372. equals(boundError, undefined);
  373. result = model.set({a: 200, admin: true}, {error: callback});
  374. equals(result, false);
  375. equals(model.get('a'), 100);
  376. equals(lastError, "Can't change admin status.");
  377. equals(boundError, undefined);
  378. });
  379. test("Model: defaults always extend attrs (#459)", function() {
  380. var Defaulted = Backbone.Model.extend({
  381. defaults: {one: 1},
  382. initialize : function(attrs, opts) {
  383. equals(attrs.one, 1);
  384. }
  385. });
  386. var providedattrs = new Defaulted({});
  387. var emptyattrs = new Defaulted();
  388. });
  389. test("Model: Inherit class properties", function() {
  390. var Parent = Backbone.Model.extend({
  391. instancePropSame: function() {},
  392. instancePropDiff: function() {}
  393. }, {
  394. classProp: function() {}
  395. });
  396. var Child = Parent.extend({
  397. instancePropDiff: function() {}
  398. });
  399. var adult = new Parent;
  400. var kid = new Child;
  401. equals(Child.classProp, Parent.classProp);
  402. notEqual(Child.classProp, undefined);
  403. equals(kid.instancePropSame, adult.instancePropSame);
  404. notEqual(kid.instancePropSame, undefined);
  405. notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
  406. notEqual(Child.prototype.instancePropDiff, undefined);
  407. });
  408. test("Model: Nested change events don't clobber previous attributes", function() {
  409. var A = Backbone.Model.extend({
  410. initialize: function() {
  411. this.bind("change:state", function(a, newState) {
  412. equals(a.previous('state'), undefined);
  413. equals(newState, 'hello');
  414. // Fire a nested change event.
  415. this.set({ other: "whatever" });
  416. });
  417. }
  418. });
  419. var B = Backbone.Model.extend({
  420. initialize: function() {
  421. this.get("a").bind("change:state", function(a, newState) {
  422. equals(a.previous('state'), undefined);
  423. equals(newState, 'hello');
  424. });
  425. }
  426. });
  427. a = new A();
  428. b = new B({a: a});
  429. a.set({state: 'hello'});
  430. });
  431. test("Model: Multiple nested calls to set", function() {
  432. var counter = 0, model = new Backbone.Model({});
  433. model.bind('change', function() {
  434. counter++;
  435. model.set({b: 1});
  436. model.set({a: 1});
  437. })
  438. .set({a: 1});
  439. equal(counter, 1, 'change is only triggered once');
  440. });
  441. });