PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/model.js

https://github.com/djmitche/backbone
JavaScript | 350 lines | 310 code | 38 blank | 2 comment | 7 complexity | 4a820c4ae3c7c572744515a2709d51a2 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: url", function() {
  44. equals(doc.url(), '/collection/1-the-tempest');
  45. doc.collection.url = '/collection/';
  46. equals(doc.url(), '/collection/1-the-tempest');
  47. doc.collection = null;
  48. var failed = false;
  49. try {
  50. doc.url();
  51. } catch (e) {
  52. failed = true;
  53. }
  54. equals(failed, true);
  55. doc.collection = collection;
  56. });
  57. test("Model: url when using urlRoot", function() {
  58. var Model = Backbone.Model.extend({
  59. urlRoot: '/collection'
  60. });
  61. var model = new Model();
  62. equals(model.url(), '/collection');
  63. model.set({id: '1'});
  64. equals(model.url(), '/collection/1');
  65. });
  66. test("Model: clone", function() {
  67. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  68. a = new Backbone.Model(attrs);
  69. b = a.clone();
  70. equals(a.get('foo'), 1);
  71. equals(a.get('bar'), 2);
  72. equals(a.get('baz'), 3);
  73. equals(b.get('foo'), a.get('foo'), "Foo should be the same on the clone.");
  74. equals(b.get('bar'), a.get('bar'), "Bar should be the same on the clone.");
  75. equals(b.get('baz'), a.get('baz'), "Baz should be the same on the clone.");
  76. a.set({foo : 100});
  77. equals(a.get('foo'), 100);
  78. equals(b.get('foo'), 1, "Changing a parent attribute does not change the clone.");
  79. });
  80. test("Model: isNew", function() {
  81. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  82. a = new Backbone.Model(attrs);
  83. ok(a.isNew(), "it should be new");
  84. attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': -5 };
  85. ok(a.isNew(), "any defined ID is legal, negative or positive");
  86. });
  87. test("Model: get", function() {
  88. equals(doc.get('title'), 'The Tempest');
  89. equals(doc.get('author'), 'Bill Shakespeare');
  90. });
  91. test("Model: escape", function() {
  92. equals(doc.escape('title'), 'The Tempest');
  93. doc.set({audience: 'Bill & Bob'});
  94. equals(doc.escape('audience'), 'Bill & Bob');
  95. doc.set({audience: 'Tim > Joan'});
  96. equals(doc.escape('audience'), 'Tim > Joan');
  97. doc.set({audience: 10101});
  98. equals(doc.escape('audience'), '10101');
  99. doc.unset('audience');
  100. equals(doc.escape('audience'), '');
  101. });
  102. test("Model: has", function() {
  103. attrs = {};
  104. a = new Backbone.Model(attrs);
  105. equals(a.has("name"), false);
  106. _([true, "Truth!", 1, false, '', 0]).each(function(value) {
  107. a.set({'name': value});
  108. equals(a.has("name"), true);
  109. });
  110. a.unset('name');
  111. equals(a.has('name'), false);
  112. _([null, undefined]).each(function(value) {
  113. a.set({'name': value});
  114. equals(a.has("name"), false);
  115. });
  116. });
  117. test("Model: set and unset", function() {
  118. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  119. a = new Backbone.Model(attrs);
  120. var changeCount = 0;
  121. a.bind("change:foo", function() { changeCount += 1; });
  122. a.set({'foo': 2});
  123. ok(a.get('foo')== 2, "Foo should have changed.");
  124. ok(changeCount == 1, "Change count should have incremented.");
  125. a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
  126. ok(a.get('foo')== 2, "Foo should NOT have changed, still 2");
  127. ok(changeCount == 1, "Change count should NOT have incremented.");
  128. a.unset('foo');
  129. ok(a.get('foo')== null, "Foo should have changed");
  130. ok(changeCount == 2, "Change count should have incremented for unset.");
  131. });
  132. test("Model: set an empty string", function() {
  133. var model = new Backbone.Model({name : "Model"});
  134. model.set({name : ''});
  135. equals(model.get('name'), '');
  136. });
  137. test("Model: clear", function() {
  138. var changed;
  139. var model = new Backbone.Model({name : "Model"});
  140. model.bind("change:name", function(){ changed = true; });
  141. model.clear();
  142. equals(changed, true);
  143. equals(model.get('name'), undefined);
  144. });
  145. test("Model: defaults", function() {
  146. var Defaulted = Backbone.Model.extend({
  147. defaults: {
  148. "one": 1,
  149. "two": 2
  150. }
  151. });
  152. var model = new Defaulted({two: null});
  153. equals(model.get('one'), 1);
  154. equals(model.get('two'), null);
  155. Defaulted = Backbone.Model.extend({
  156. defaults: function() {
  157. return {
  158. "one": 3,
  159. "two": 4
  160. };
  161. }
  162. });
  163. var model = new Defaulted({two: null});
  164. equals(model.get('one'), 3);
  165. equals(model.get('two'), null);
  166. });
  167. test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
  168. var model = new Backbone.Model({name : "Tim", age : 10});
  169. equals(model.changedAttributes(), false);
  170. model.bind('change', function() {
  171. ok(model.hasChanged('name'), 'name changed');
  172. ok(!model.hasChanged('age'), 'age did not');
  173. ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
  174. equals(model.previous('name'), 'Tim');
  175. ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
  176. });
  177. model.set({name : 'Rob'}, {silent : true});
  178. equals(model.hasChanged(), true);
  179. equals(model.hasChanged('name'), true);
  180. model.change();
  181. equals(model.get('name'), 'Rob');
  182. });
  183. test("Model: change with options", function() {
  184. var value;
  185. var model = new Backbone.Model({name: 'Rob'});
  186. model.bind('change', function(model, options) {
  187. value = options.prefix + model.get('name');
  188. });
  189. model.set({name: 'Bob'}, {silent: true});
  190. model.change({prefix: 'Mr. '});
  191. equals(value, 'Mr. Bob');
  192. model.set({name: 'Sue'}, {prefix: 'Ms. '});
  193. equals(value, 'Ms. Sue');
  194. });
  195. test("Model: change after initialize", function () {
  196. var changed = 0;
  197. var attrs = {id: 1, label: 'c'};
  198. var obj = new Backbone.Model(attrs);
  199. obj.bind('change', function() { changed += 1; });
  200. obj.set(attrs);
  201. equals(changed, 0);
  202. });
  203. test("Model: save within change event", function () {
  204. var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
  205. model.bind('change', function () {
  206. model.save();
  207. ok(_.isEqual(lastRequest[1], model));
  208. });
  209. model.set({lastName: 'Hicks'});
  210. });
  211. test("Model: save", function() {
  212. doc.save({title : "Henry V"});
  213. equals(lastRequest[0], 'update');
  214. ok(_.isEqual(lastRequest[1], doc));
  215. });
  216. test("Model: fetch", function() {
  217. doc.fetch();
  218. ok(lastRequest[0], 'read');
  219. ok(_.isEqual(lastRequest[1], doc));
  220. });
  221. test("Model: destroy", function() {
  222. doc.destroy();
  223. equals(lastRequest[0], 'delete');
  224. ok(_.isEqual(lastRequest[1], doc));
  225. });
  226. test("Model: validate", function() {
  227. var lastError;
  228. var model = new Backbone.Model();
  229. model.validate = function(attrs) {
  230. if (attrs.admin) return "Can't change admin status.";
  231. };
  232. model.bind('error', function(model, error) {
  233. lastError = error;
  234. });
  235. var result = model.set({a: 100});
  236. equals(result, model);
  237. equals(model.get('a'), 100);
  238. equals(lastError, undefined);
  239. result = model.set({admin: true}, {silent: true});
  240. equals(lastError, undefined);
  241. equals(model.get('admin'), true);
  242. result = model.set({a: 200, admin: true});
  243. equals(result, false);
  244. equals(model.get('a'), 100);
  245. equals(lastError, "Can't change admin status.");
  246. });
  247. test("Model: validate on unset and clear", function() {
  248. var error;
  249. var model = new Backbone.Model({name: "One"});
  250. model.validate = function(attrs) {
  251. if ("name" in attrs) {
  252. if (!attrs.name) {
  253. error = true;
  254. return "No thanks.";
  255. }
  256. }
  257. };
  258. model.set({name: "Two"});
  259. equals(model.get('name'), 'Two');
  260. equals(error, undefined);
  261. model.unset('name');
  262. equals(error, true);
  263. equals(model.get('name'), 'Two');
  264. model.clear();
  265. equals(model.get('name'), 'Two');
  266. delete model.validate;
  267. model.clear();
  268. equals(model.get('name'), undefined);
  269. });
  270. test("Model: validate with error callback", function() {
  271. var lastError, boundError;
  272. var model = new Backbone.Model();
  273. model.validate = function(attrs) {
  274. if (attrs.admin) return "Can't change admin status.";
  275. };
  276. var callback = function(model, error) {
  277. lastError = error;
  278. };
  279. model.bind('error', function(model, error) {
  280. boundError = true;
  281. });
  282. var result = model.set({a: 100}, {error: callback});
  283. equals(result, model);
  284. equals(model.get('a'), 100);
  285. equals(lastError, undefined);
  286. equals(boundError, undefined);
  287. result = model.set({a: 200, admin: true}, {error: callback});
  288. equals(result, false);
  289. equals(model.get('a'), 100);
  290. equals(lastError, "Can't change admin status.");
  291. equals(boundError, undefined);
  292. });
  293. test("Model: Inherit class properties", function() {
  294. var Parent = Backbone.Model.extend({
  295. instancePropSame: function() {},
  296. instancePropDiff: function() {}
  297. }, {
  298. classProp: function() {}
  299. });
  300. var Child = Parent.extend({
  301. instancePropDiff: function() {}
  302. });
  303. var adult = new Parent;
  304. var kid = new Child;
  305. equals(Child.classProp, Parent.classProp);
  306. notEqual(Child.classProp, undefined);
  307. equals(kid.instancePropSame, adult.instancePropSame);
  308. notEqual(kid.instancePropSame, undefined);
  309. notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
  310. notEqual(Child.prototype.instancePropDiff, undefined);
  311. });
  312. });