PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/backbone/test/model.js

https://github.com/jzarka/sgbusmap
JavaScript | 426 lines | 377 code | 46 blank | 3 comment | 8 complexity | d4706e23d73505bd5f2e766074970dcf 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, and uri encoding", 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/%2B1%2B');
  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. a = new Backbone.Model(attrs);
  86. ok(!a.isNew(), "any defined ID is legal, negative or positive");
  87. attrs = { 'foo': 1, 'bar': 2, 'baz': 3, 'id': 0 };
  88. a = new Backbone.Model(attrs);
  89. ok(!a.isNew(), "any defined ID is legal, including zero");
  90. ok( new Backbone.Model({ }).isNew(), "is true when there is no id");
  91. ok(!new Backbone.Model({ 'id': 2 }).isNew(), "is false for a positive integer");
  92. ok(!new Backbone.Model({ 'id': -5 }).isNew(), "is false for a negative integer");
  93. });
  94. test("Model: get", function() {
  95. equals(doc.get('title'), 'The Tempest');
  96. equals(doc.get('author'), 'Bill Shakespeare');
  97. });
  98. test("Model: escape", function() {
  99. equals(doc.escape('title'), 'The Tempest');
  100. doc.set({audience: 'Bill & Bob'});
  101. equals(doc.escape('audience'), 'Bill & Bob');
  102. doc.set({audience: 'Tim > Joan'});
  103. equals(doc.escape('audience'), 'Tim > Joan');
  104. doc.set({audience: 10101});
  105. equals(doc.escape('audience'), '10101');
  106. doc.unset('audience');
  107. equals(doc.escape('audience'), '');
  108. });
  109. test("Model: has", function() {
  110. attrs = {};
  111. a = new Backbone.Model(attrs);
  112. equals(a.has("name"), false);
  113. _([true, "Truth!", 1, false, '', 0]).each(function(value) {
  114. a.set({'name': value});
  115. equals(a.has("name"), true);
  116. });
  117. a.unset('name');
  118. equals(a.has('name'), false);
  119. _([null, undefined]).each(function(value) {
  120. a.set({'name': value});
  121. equals(a.has("name"), false);
  122. });
  123. });
  124. test("Model: set and unset", function() {
  125. attrs = {id: 'id', foo: 1, bar: 2, baz: 3};
  126. a = new Backbone.Model(attrs);
  127. var changeCount = 0;
  128. a.bind("change:foo", function() { changeCount += 1; });
  129. a.set({'foo': 2});
  130. ok(a.get('foo')== 2, "Foo should have changed.");
  131. ok(changeCount == 1, "Change count should have incremented.");
  132. a.set({'foo': 2}); // set with value that is not new shouldn't fire change event
  133. ok(a.get('foo')== 2, "Foo should NOT have changed, still 2");
  134. ok(changeCount == 1, "Change count should NOT have incremented.");
  135. a.unset('foo');
  136. ok(a.get('foo')== null, "Foo should have changed");
  137. ok(changeCount == 2, "Change count should have incremented for unset.");
  138. a.unset('id');
  139. equals(a.id, undefined, "Unsetting the id should remove the id property.");
  140. });
  141. test("Model: multiple unsets", function() {
  142. var i = 0;
  143. var counter = function(){ i++; };
  144. var model = new Backbone.Model({a: 1});
  145. model.bind("change:a", counter);
  146. model.set({a: 2});
  147. model.unset('a');
  148. model.unset('a');
  149. equals(i, 2, 'Unset does not fire an event for missing attributes.');
  150. });
  151. test("Model: using a non-default id attribute.", function() {
  152. var MongoModel = Backbone.Model.extend({idAttribute : '_id'});
  153. var model = new MongoModel({id: 'eye-dee', _id: 25, title: 'Model'});
  154. equals(model.get('id'), 'eye-dee');
  155. equals(model.id, 25);
  156. equals(model.isNew(), false);
  157. model.unset('_id');
  158. equals(model.id, undefined);
  159. equals(model.isNew(), true);
  160. });
  161. test("Model: set an empty string", function() {
  162. var model = new Backbone.Model({name : "Model"});
  163. model.set({name : ''});
  164. equals(model.get('name'), '');
  165. });
  166. test("Model: clear", function() {
  167. var changed;
  168. var model = new Backbone.Model({name : "Model"});
  169. model.bind("change:name", function(){ changed = true; });
  170. model.clear();
  171. equals(changed, true);
  172. equals(model.get('name'), undefined);
  173. });
  174. test("Model: defaults", function() {
  175. var Defaulted = Backbone.Model.extend({
  176. defaults: {
  177. "one": 1,
  178. "two": 2
  179. }
  180. });
  181. var model = new Defaulted({two: null});
  182. equals(model.get('one'), 1);
  183. equals(model.get('two'), null);
  184. Defaulted = Backbone.Model.extend({
  185. defaults: function() {
  186. return {
  187. "one": 3,
  188. "two": 4
  189. };
  190. }
  191. });
  192. var model = new Defaulted({two: null});
  193. equals(model.get('one'), 3);
  194. equals(model.get('two'), null);
  195. });
  196. test("Model: change, hasChanged, changedAttributes, previous, previousAttributes", function() {
  197. var model = new Backbone.Model({name : "Tim", age : 10});
  198. equals(model.changedAttributes(), false);
  199. model.bind('change', function() {
  200. ok(model.hasChanged('name'), 'name changed');
  201. ok(!model.hasChanged('age'), 'age did not');
  202. ok(_.isEqual(model.changedAttributes(), {name : 'Rob'}), 'changedAttributes returns the changed attrs');
  203. equals(model.previous('name'), 'Tim');
  204. ok(_.isEqual(model.previousAttributes(), {name : "Tim", age : 10}), 'previousAttributes is correct');
  205. });
  206. model.set({name : 'Rob'}, {silent : true});
  207. equals(model.hasChanged(), true);
  208. equals(model.hasChanged('name'), true);
  209. model.change();
  210. equals(model.get('name'), 'Rob');
  211. });
  212. test("Model: change with options", function() {
  213. var value;
  214. var model = new Backbone.Model({name: 'Rob'});
  215. model.bind('change', function(model, options) {
  216. value = options.prefix + model.get('name');
  217. });
  218. model.set({name: 'Bob'}, {silent: true});
  219. model.change({prefix: 'Mr. '});
  220. equals(value, 'Mr. Bob');
  221. model.set({name: 'Sue'}, {prefix: 'Ms. '});
  222. equals(value, 'Ms. Sue');
  223. });
  224. test("Model: change after initialize", function () {
  225. var changed = 0;
  226. var attrs = {id: 1, label: 'c'};
  227. var obj = new Backbone.Model(attrs);
  228. obj.bind('change', function() { changed += 1; });
  229. obj.set(attrs);
  230. equals(changed, 0);
  231. });
  232. test("Model: save within change event", function () {
  233. var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
  234. model.bind('change', function () {
  235. model.save();
  236. ok(_.isEqual(lastRequest[1], model));
  237. });
  238. model.set({lastName: 'Hicks'});
  239. });
  240. test("Model: save", function() {
  241. doc.save({title : "Henry V"});
  242. equals(lastRequest[0], 'update');
  243. ok(_.isEqual(lastRequest[1], doc));
  244. });
  245. test("Model: fetch", function() {
  246. doc.fetch();
  247. ok(lastRequest[0], 'read');
  248. ok(_.isEqual(lastRequest[1], doc));
  249. });
  250. test("Model: destroy", function() {
  251. doc.destroy();
  252. equals(lastRequest[0], 'delete');
  253. ok(_.isEqual(lastRequest[1], doc));
  254. });
  255. test("Model: non-persisted destroy", function() {
  256. attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
  257. a = new Backbone.Model(attrs);
  258. a.sync = function() { throw "should not be called"; };
  259. ok(a.destroy(), "non-persisted model should not call sync");
  260. });
  261. test("Model: validate", function() {
  262. var lastError;
  263. var model = new Backbone.Model();
  264. model.validate = function(attrs) {
  265. if (attrs.admin) return "Can't change admin status.";
  266. };
  267. model.bind('error', function(model, error) {
  268. lastError = error;
  269. });
  270. var result = model.set({a: 100});
  271. equals(result, model);
  272. equals(model.get('a'), 100);
  273. equals(lastError, undefined);
  274. result = model.set({admin: true}, {silent: true});
  275. equals(lastError, undefined);
  276. equals(model.get('admin'), true);
  277. result = model.set({a: 200, admin: true});
  278. equals(result, false);
  279. equals(model.get('a'), 100);
  280. equals(lastError, "Can't change admin status.");
  281. });
  282. test("Model: validate on unset and clear", function() {
  283. var error;
  284. var model = new Backbone.Model({name: "One"});
  285. model.validate = function(attrs) {
  286. if ("name" in attrs) {
  287. if (!attrs.name) {
  288. error = true;
  289. return "No thanks.";
  290. }
  291. }
  292. };
  293. model.set({name: "Two"});
  294. equals(model.get('name'), 'Two');
  295. equals(error, undefined);
  296. model.unset('name');
  297. equals(error, true);
  298. equals(model.get('name'), 'Two');
  299. model.clear();
  300. equals(model.get('name'), 'Two');
  301. delete model.validate;
  302. model.clear();
  303. equals(model.get('name'), undefined);
  304. });
  305. test("Model: validate with error callback", function() {
  306. var lastError, boundError;
  307. var model = new Backbone.Model();
  308. model.validate = function(attrs) {
  309. if (attrs.admin) return "Can't change admin status.";
  310. };
  311. var callback = function(model, error) {
  312. lastError = error;
  313. };
  314. model.bind('error', function(model, error) {
  315. boundError = true;
  316. });
  317. var result = model.set({a: 100}, {error: callback});
  318. equals(result, model);
  319. equals(model.get('a'), 100);
  320. equals(lastError, undefined);
  321. equals(boundError, undefined);
  322. result = model.set({a: 200, admin: true}, {error: callback});
  323. equals(result, false);
  324. equals(model.get('a'), 100);
  325. equals(lastError, "Can't change admin status.");
  326. equals(boundError, undefined);
  327. });
  328. test("Model: defaults always extend attrs (#459)", function() {
  329. var Defaulted = Backbone.Model.extend({
  330. defaults: {one: 1},
  331. initialize : function(attrs, opts) {
  332. equals(attrs.one, 1);
  333. }
  334. });
  335. var providedattrs = new Defaulted({});
  336. var emptyattrs = new Defaulted();
  337. });
  338. test("Model: Inherit class properties", function() {
  339. var Parent = Backbone.Model.extend({
  340. instancePropSame: function() {},
  341. instancePropDiff: function() {}
  342. }, {
  343. classProp: function() {}
  344. });
  345. var Child = Parent.extend({
  346. instancePropDiff: function() {}
  347. });
  348. var adult = new Parent;
  349. var kid = new Child;
  350. equals(Child.classProp, Parent.classProp);
  351. notEqual(Child.classProp, undefined);
  352. equals(kid.instancePropSame, adult.instancePropSame);
  353. notEqual(kid.instancePropSame, undefined);
  354. notEqual(Child.prototype.instancePropDiff, Parent.prototype.instancePropDiff);
  355. notEqual(Child.prototype.instancePropDiff, undefined);
  356. });
  357. test("Model: Nested change events don't clobber previous attributes", function() {
  358. var A = Backbone.Model.extend({
  359. initialize: function() {
  360. this.bind("change:state", function(a, newState) {
  361. equals(a.previous('state'), undefined);
  362. equals(newState, 'hello');
  363. // Fire a nested change event.
  364. this.set({ other: "whatever" });
  365. });
  366. }
  367. });
  368. var B = Backbone.Model.extend({
  369. initialize: function() {
  370. this.get("a").bind("change:state", function(a, newState) {
  371. equals(a.previous('state'), undefined);
  372. equals(newState, 'hello');
  373. });
  374. }
  375. });
  376. a = new A();
  377. b = new B({a: a});
  378. a.set({state: 'hello'});
  379. });
  380. });