PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/collection.js

https://github.com/parkerproject/backbone
JavaScript | 406 lines | 365 code | 40 blank | 1 comment | 18 complexity | 7458c805096e5a310d5dfb67ffaaa07a MD5 | raw file
  1. $(document).ready(function() {
  2. module("Backbone.Collection");
  3. window.lastRequest = null;
  4. Backbone.sync = function() {
  5. lastRequest = _.toArray(arguments);
  6. };
  7. var a = new Backbone.Model({id: 3, label: 'a'});
  8. var b = new Backbone.Model({id: 2, label: 'b'});
  9. var c = new Backbone.Model({id: 1, label: 'c'});
  10. var d = new Backbone.Model({id: 0, label: 'd'});
  11. var e = null;
  12. var col = new Backbone.Collection([a,b,c,d]);
  13. var otherCol = new Backbone.Collection();
  14. test("Collection: new and sort", function() {
  15. equals(col.first(), a, "a should be first");
  16. equals(col.last(), d, "d should be last");
  17. col.comparator = function(model) { return model.id; };
  18. col.sort();
  19. equals(col.first(), d, "d should be first");
  20. equals(col.last(), a, "a should be last");
  21. equals(col.length, 4);
  22. });
  23. test("Collection: get, getByCid", function() {
  24. equals(col.get(0), d);
  25. equals(col.get(2), b);
  26. equals(col.getByCid(col.first().cid), col.first());
  27. });
  28. test("Collection: get with non-default ids", function() {
  29. var col = new Backbone.Collection();
  30. var MongoModel = Backbone.Model.extend({
  31. idAttribute: '_id'
  32. });
  33. var model = new MongoModel({_id: 100});
  34. col.add(model);
  35. equals(col.get(100), model);
  36. model.set({_id: 101});
  37. equals(col.get(101), model);
  38. });
  39. test("Collection: add model with attributes modified by set", function() {
  40. var CustomSetModel = Backbone.Model.extend({
  41. defaults: {
  42. number_as_string: null //presence of defaults forces extend
  43. },
  44. validate: function (attributes) {
  45. if (!_.isString(attributes.num_as_string)) {
  46. return 'fail';
  47. }
  48. },
  49. set: function (attributes, options) {
  50. if (attributes.num_as_string) {
  51. attributes.num_as_string = attributes.num_as_string.toString();
  52. }
  53. Backbone.Model.prototype.set.call(this, attributes, options);
  54. }
  55. });
  56. var CustomSetCollection = Backbone.Collection.extend({
  57. model: CustomSetModel
  58. });
  59. var col = new CustomSetCollection([{ num_as_string: 2 }]);
  60. equals(col.length, 1);
  61. });
  62. test("Collection: update index when id changes", function() {
  63. var col = new Backbone.Collection();
  64. col.add([
  65. {id : 0, name : 'one'},
  66. {id : 1, name : 'two'}
  67. ]);
  68. var one = col.get(0);
  69. equals(one.get('name'), 'one');
  70. one.set({id : 101});
  71. equals(col.get(0), null);
  72. equals(col.get(101).get('name'), 'one');
  73. });
  74. test("Collection: at", function() {
  75. equals(col.at(2), b);
  76. });
  77. test("Collection: pluck", function() {
  78. equals(col.pluck('label').join(' '), 'd c b a');
  79. });
  80. test("Collection: add", function() {
  81. var added = opts = secondAdded = null;
  82. e = new Backbone.Model({id: 10, label : 'e'});
  83. otherCol.add(e);
  84. otherCol.bind('add', function() {
  85. secondAdded = true;
  86. });
  87. col.bind('add', function(model, collection, options){
  88. added = model.get('label');
  89. opts = options;
  90. });
  91. col.add(e, {amazing: true});
  92. equals(added, 'e');
  93. equals(col.length, 5);
  94. equals(col.last(), e);
  95. equals(otherCol.length, 1);
  96. equals(secondAdded, null);
  97. ok(opts.amazing);
  98. var f = new Backbone.Model({id: 20, label : 'f'});
  99. var g = new Backbone.Model({id: 21, label : 'g'});
  100. var h = new Backbone.Model({id: 22, label : 'h'});
  101. var atCol = new Backbone.Collection([f, g, h]);
  102. equals(atCol.length, 3);
  103. atCol.add(e, {at: 1});
  104. equals(atCol.length, 4);
  105. equals(atCol.at(1), e);
  106. equals(atCol.last(), h);
  107. });
  108. test("Collection: add model to collection and verify index updates", function() {
  109. var f = new Backbone.Model({id: 20, label : 'f'});
  110. var g = new Backbone.Model({id: 21, label : 'g'});
  111. var h = new Backbone.Model({id: 22, label : 'h'});
  112. var col = new Backbone.Collection();
  113. var counts = [];
  114. col.bind('add', function(model, collection, options) {
  115. counts.push(options.index);
  116. });
  117. col.add(f);
  118. col.add(g);
  119. col.add(h);
  120. ok(_.isEqual(counts, [0,1,2]));
  121. });
  122. test("Collection: add model to collection twice", function() {
  123. try {
  124. // no id, same cid
  125. var a2 = new Backbone.Model({label: a.label});
  126. a2.cid = a.cid;
  127. col.add(a2);
  128. ok(false, "duplicate; expected add to fail");
  129. } catch (e) {
  130. equals(e.message, "Can't add the same model to a set twice,3");
  131. }
  132. });
  133. test("Collection: add model to multiple collections", function() {
  134. var counter = 0;
  135. var e = new Backbone.Model({id: 10, label : 'e'});
  136. e.bind('add', function(model, collection) {
  137. counter++;
  138. equals(e, model);
  139. if (counter > 1) {
  140. equals(collection, colF);
  141. } else {
  142. equals(collection, colE);
  143. }
  144. });
  145. var colE = new Backbone.Collection([]);
  146. colE.bind('add', function(model, collection) {
  147. equals(e, model);
  148. equals(colE, collection);
  149. });
  150. var colF = new Backbone.Collection([]);
  151. colF.bind('add', function(model, collection) {
  152. equals(e, model);
  153. equals(colF, collection);
  154. });
  155. colE.add(e);
  156. equals(e.collection, colE);
  157. colF.add(e);
  158. equals(e.collection, colE);
  159. });
  160. test("Collection: remove", function() {
  161. var removed = otherRemoved = null;
  162. col.bind('remove', function(model){ removed = model.get('label'); });
  163. otherCol.bind('remove', function(){ otherRemoved = true; });
  164. col.remove(e);
  165. equals(removed, 'e');
  166. equals(col.length, 4);
  167. equals(col.first(), d);
  168. equals(otherRemoved, null);
  169. });
  170. test("Collection: remove should return correct index events", function() {
  171. var f = new Backbone.Model({id: 20, label : 'f'});
  172. var g = new Backbone.Model({id: 21, label : 'g'});
  173. var h = new Backbone.Model({id: 22, label : 'h'});
  174. var col = new Backbone.Collection([f,g,h]);
  175. var counts = [];
  176. col.bind('remove', function(model, collection, options) {
  177. counts.push(options.index);
  178. });
  179. col.remove(h);
  180. col.remove(g);
  181. col.remove(f);
  182. ok(_.isEqual(counts, [2,1,0]));
  183. });
  184. test("Collection: events are unbound on remove", function() {
  185. var counter = 0;
  186. var dj = new Backbone.Model();
  187. var emcees = new Backbone.Collection([dj]);
  188. emcees.bind('change', function(){ counter++; });
  189. dj.set({name : 'Kool'});
  190. equals(counter, 1);
  191. emcees.reset([]);
  192. equals(dj.collection, undefined);
  193. dj.set({name : 'Shadow'});
  194. equals(counter, 1);
  195. });
  196. test("Collection: remove in multiple collections", function() {
  197. var modelData = {
  198. id : 5,
  199. title : 'Othello'
  200. };
  201. var passed = false;
  202. var e = new Backbone.Model(modelData);
  203. var f = new Backbone.Model(modelData);
  204. f.bind('remove', function() {
  205. passed = true;
  206. });
  207. var colE = new Backbone.Collection([e]);
  208. var colF = new Backbone.Collection([f]);
  209. ok(e != f);
  210. ok(colE.length == 1);
  211. ok(colF.length == 1);
  212. colE.remove(e);
  213. equals(passed, false);
  214. ok(colE.length == 0);
  215. colF.remove(e);
  216. ok(colF.length == 0);
  217. equals(passed, true);
  218. });
  219. test("Collection: remove same model in multiple collection", function() {
  220. var counter = 0;
  221. var e = new Backbone.Model({id: 5, title: 'Othello'});
  222. e.bind('remove', function(model, collection) {
  223. counter++;
  224. equals(e, model);
  225. if (counter > 1) {
  226. equals(collection, colE);
  227. } else {
  228. equals(collection, colF);
  229. }
  230. });
  231. var colE = new Backbone.Collection([e]);
  232. colE.bind('remove', function(model, collection) {
  233. equals(e, model);
  234. equals(colE, collection);
  235. });
  236. var colF = new Backbone.Collection([e]);
  237. colF.bind('remove', function(model, collection) {
  238. equals(e, model);
  239. equals(colF, collection);
  240. });
  241. equals(colE, e.collection);
  242. colF.remove(e);
  243. ok(colF.length == 0);
  244. ok(colE.length == 1);
  245. equals(counter, 1);
  246. equals(colE, e.collection);
  247. colE.remove(e);
  248. equals(null, e.collection);
  249. ok(colE.length == 0);
  250. equals(counter, 2);
  251. });
  252. test("Collection: model destroy removes from all collections", function() {
  253. var e = new Backbone.Model({id: 5, title: 'Othello'});
  254. e.sync = function(method, model, options) { options.success({}); };
  255. var colE = new Backbone.Collection([e]);
  256. var colF = new Backbone.Collection([e]);
  257. e.destroy();
  258. ok(colE.length == 0);
  259. ok(colF.length == 0);
  260. equals(null, e.collection);
  261. });
  262. test("Colllection: non-persisted model destroy removes from all collections", function() {
  263. var e = new Backbone.Model({title: 'Othello'});
  264. e.sync = function(method, model, options) { throw "should not be called"; };
  265. var colE = new Backbone.Collection([e]);
  266. var colF = new Backbone.Collection([e]);
  267. e.destroy();
  268. ok(colE.length == 0);
  269. ok(colF.length == 0);
  270. equals(null, e.collection);
  271. });
  272. test("Collection: fetch", function() {
  273. col.fetch();
  274. equals(lastRequest[0], 'read');
  275. equals(lastRequest[1], col);
  276. });
  277. test("Collection: create", function() {
  278. var model = col.create({label: 'f'});
  279. equals(lastRequest[0], 'create');
  280. equals(lastRequest[1], model);
  281. equals(model.get('label'), 'f');
  282. equals(model.collection, col);
  283. });
  284. test("Collection: create enforces validation", function() {
  285. var ValidatingModel = Backbone.Model.extend({
  286. validate: function(attrs) {
  287. return "fail";
  288. }
  289. });
  290. var ValidatingCollection = Backbone.Collection.extend({
  291. model: ValidatingModel
  292. });
  293. var col = new ValidatingCollection();
  294. equals(col.create({"foo":"bar"}),false);
  295. });
  296. test("Collection: a failing create runs the error callback", function() {
  297. var ValidatingModel = Backbone.Model.extend({
  298. validate: function(attrs) {
  299. return "fail";
  300. }
  301. });
  302. var ValidatingCollection = Backbone.Collection.extend({
  303. model: ValidatingModel
  304. });
  305. var flag = false;
  306. var callback = function(model, error) { flag = true; };
  307. var col = new ValidatingCollection();
  308. col.create({"foo":"bar"}, { error: callback });
  309. equals(flag, true);
  310. });
  311. test("collection: initialize", function() {
  312. var Collection = Backbone.Collection.extend({
  313. initialize: function() {
  314. this.one = 1;
  315. }
  316. });
  317. var coll = new Collection;
  318. equals(coll.one, 1);
  319. });
  320. test("Collection: toJSON", function() {
  321. equals(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
  322. });
  323. test("Collection: Underscore methods", function() {
  324. equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
  325. equals(col.any(function(model){ return model.id === 100; }), false);
  326. equals(col.any(function(model){ return model.id === 0; }), true);
  327. equals(col.indexOf(b), 2);
  328. equals(col.size(), 4);
  329. equals(col.rest().length, 3);
  330. ok(!_.include(col.rest()), a);
  331. ok(!_.include(col.rest()), d);
  332. ok(!col.isEmpty());
  333. ok(!_.include(col.without(d)), d);
  334. equals(col.max(function(model){ return model.id; }).id, 3);
  335. equals(col.min(function(model){ return model.id; }).id, 0);
  336. same(col.chain()
  337. .filter(function(o){ return o.id % 2 === 0; })
  338. .map(function(o){ return o.id * 2; })
  339. .value(),
  340. [0, 4]);
  341. });
  342. test("Collection: reset", function() {
  343. var resetCount = 0;
  344. var models = col.models;
  345. col.bind('reset', function() { resetCount += 1; });
  346. col.reset([]);
  347. equals(resetCount, 1);
  348. equals(col.length, 0);
  349. equals(col.last(), null);
  350. col.reset(models);
  351. equals(resetCount, 2);
  352. equals(col.length, 4);
  353. equals(col.last(), a);
  354. col.reset(_.map(models, function(m){ return m.attributes; }));
  355. equals(resetCount, 3);
  356. equals(col.length, 4);
  357. ok(col.last() !== a);
  358. ok(_.isEqual(col.last().attributes, a.attributes));
  359. });
  360. test("Collection: trigger custom events on models", function() {
  361. var fired = null;
  362. a.bind("custom", function() { fired = true; });
  363. a.trigger("custom");
  364. equals(fired, true);
  365. });
  366. });