PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/test/model.js

https://github.com/chandlerkent/backbone
JavaScript | 313 lines | 278 code | 33 blank | 2 comment | 7 complexity | ad2f46de97ec78117f0e895dc071e7a0 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.unset('audience');
  98. equals(doc.escape('audience'), '');
  99. });
  100. test("Model: has", function() {
  101. attrs = {};
  102. a = new Backbone.Model(attrs);
  103. equals(a.has("name"), false);
  104. _([true, "Truth!", 1, false, '', 0]).each(function(value) {
  105. a.set({'name': value});
  106. equals(a.has("name"), true);
  107. });
  108. a.unset('name');
  109. equals(a.has('name'), false);
  110. _([null, undefined]).each(function(value) {
  111. a.set({'name': value});
  112. equals(a.has("name"), false);
  113. });
  114. });
  115. test("Model: set and unset", function() {
  116. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  117. a = new Backbone.Model(attrs);
  118. var changeCount = 0;
  119. a.bind("change:foo", function() { changeCount += 1; });
  120. a.set({'foo': 2});
  121. ok(a.get('foo')== 2, "Foo should have changed.");
  122. ok(changeCount == 1, "Change count should have incremented.");
  123. a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
  124. ok(a.get('foo')== 2, "Foo should NOT have changed, still 2");
  125. ok(changeCount == 1, "Change count should NOT have incremented.");
  126. a.unset('foo');
  127. ok(a.get('foo')== null, "Foo should have changed");
  128. ok(changeCount == 2, "Change count should have incremented for unset.");
  129. });
  130. test("Model: set an empty string", function() {
  131. var model = new Backbone.Model({name : "Model"});
  132. model.set({name : ''});
  133. equals(model.get('name'), '');
  134. });
  135. test("Model: clear", function() {
  136. var changed;
  137. var model = new Backbone.Model({name : "Model"});
  138. model.bind("change:name", function(){ changed = true; });
  139. model.clear();
  140. equals(changed, true);
  141. equals(model.get('name'), undefined);
  142. });
  143. test("Model: defaults", function() {
  144. var Defaulted = Backbone.Model.extend({
  145. defaults: {
  146. "one": 1,
  147. "two": 2
  148. }
  149. });
  150. var model = new Defaulted({two: null});
  151. equals(model.get('one'), 1);
  152. equals(model.get('two'), null);
  153. });
  154. test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
  155. var model = new Backbone.Model({name : "Tim", age : 10});
  156. equals(model.changedAttributes(), false);
  157. model.bind('change', function() {
  158. ok(model.hasChanged('name'), 'name changed');
  159. ok(!model.hasChanged('age'), 'age did not');
  160. ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
  161. equals(model.previous('name'), 'Tim');
  162. ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
  163. });
  164. model.set({name : 'Rob'}, {silent : true});
  165. equals(model.hasChanged(), true);
  166. equals(model.hasChanged('name'), true);
  167. model.change();
  168. equals(model.get('name'), 'Rob');
  169. });
  170. test("Model: change with options", function() {
  171. var value;
  172. var model = new Backbone.Model({name: 'Rob'});
  173. model.bind('change', function(model, options) {
  174. value = options.prefix + model.get('name');
  175. });
  176. model.set({name: 'Bob'}, {silent: true});
  177. model.change({prefix: 'Mr. '});
  178. equals(value, 'Mr. Bob');
  179. model.set({name: 'Sue'}, {prefix: 'Ms. '});
  180. equals(value, 'Ms. Sue');
  181. });
  182. test("Model: change after initialize", function () {
  183. var changed = 0;
  184. var attrs = {id: 1, label: 'c'};
  185. var obj = new Backbone.Model(attrs);
  186. obj.bind('change', function() { changed += 1; });
  187. obj.set(attrs);
  188. equals(changed, 0);
  189. });
  190. test("Model: save within change event", function () {
  191. var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
  192. model.bind('change', function () {
  193. model.save();
  194. ok(_.isEqual(lastRequest[1], model));
  195. });
  196. model.set({lastName: 'Hicks'});
  197. });
  198. test("Model: save", function() {
  199. doc.save({title : "Henry V"});
  200. equals(lastRequest[0], 'update');
  201. ok(_.isEqual(lastRequest[1], doc));
  202. });
  203. test("Model: fetch", function() {
  204. doc.fetch();
  205. ok(lastRequest[0], 'read');
  206. ok(_.isEqual(lastRequest[1], doc));
  207. });
  208. test("Model: destroy", function() {
  209. doc.destroy();
  210. equals(lastRequest[0], 'delete');
  211. ok(_.isEqual(lastRequest[1], doc));
  212. });
  213. test("Model: validate", function() {
  214. var lastError;
  215. var model = new Backbone.Model();
  216. model.validate = function(attrs) {
  217. if (attrs.admin) return "Can't change admin status.";
  218. };
  219. model.bind('error', function(model, error) {
  220. lastError = error;
  221. });
  222. var result = model.set({a: 100});
  223. equals(result, model);
  224. equals(model.get('a'), 100);
  225. equals(lastError, undefined);
  226. result = model.set({admin: true}, {silent: true});
  227. equals(lastError, undefined);
  228. equals(model.get('admin'), true);
  229. result = model.set({a: 200, admin: true});
  230. equals(result, false);
  231. equals(model.get('a'), 100);
  232. equals(lastError, "Can't change admin status.");
  233. });
  234. test("Model: validate on unset and clear", function() {
  235. var error;
  236. var model = new Backbone.Model({name: "One"});
  237. model.validate = function(attrs) {
  238. if ("name" in attrs) {
  239. if (!attrs.name) {
  240. error = true;
  241. return "No thanks.";
  242. }
  243. }
  244. };
  245. model.set({name: "Two"});
  246. equals(model.get('name'), 'Two');
  247. equals(error, undefined);
  248. model.unset('name');
  249. equals(error, true);
  250. equals(model.get('name'), 'Two');
  251. model.clear();
  252. equals(model.get('name'), 'Two');
  253. delete model.validate;
  254. model.clear();
  255. equals(model.get('name'), undefined);
  256. });
  257. test("Model: validate with error callback", function() {
  258. var lastError, boundError;
  259. var model = new Backbone.Model();
  260. model.validate = function(attrs) {
  261. if (attrs.admin) return "Can't change admin status.";
  262. };
  263. var callback = function(model, error) {
  264. lastError = error;
  265. };
  266. model.bind('error', function(model, error) {
  267. boundError = true;
  268. });
  269. var result = model.set({a: 100}, {error: callback});
  270. equals(result, model);
  271. equals(model.get('a'), 100);
  272. equals(lastError, undefined);
  273. equals(boundError, undefined);
  274. result = model.set({a: 200, admin: true}, {error: callback});
  275. equals(result, false);
  276. equals(model.get('a'), 100);
  277. equals(lastError, "Can't change admin status.");
  278. equals(boundError, undefined);
  279. });
  280. });