PageRenderTime 53ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/collection.js

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