/test/collection.js

https://github.com/ustas-v/backbone · JavaScript · 525 lines · 474 code · 50 blank · 1 comment · 26 complexity · 14646f87a93ec52ee492b94ca0f66213 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. equal(col.first(), a, "a should be first");
  16. equal(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. equal(col.first(), a, "a should be first");
  22. equal(col.last(), d, "d should be last");
  23. col.comparator = function(model) { return model.id; };
  24. col.sort();
  25. equal(col.first(), d, "d should be first");
  26. equal(col.last(), a, "a should be last");
  27. equal(col.length, 4);
  28. });
  29. test("Collection: get, getByCid", function() {
  30. equal(col.get(0), d);
  31. equal(col.get(2), b);
  32. equal(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. equal(col.get(100), model);
  42. model.set({_id: 101});
  43. equal(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. equal(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. equal(one.get('name'), 'one');
  76. one.set({id : 101});
  77. equal(col.get(0), null);
  78. equal(col.get(101).get('name'), 'one');
  79. });
  80. test("Collection: at", function() {
  81. equal(col.at(2), b);
  82. });
  83. test("Collection: pluck", function() {
  84. equal(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. equal(options.index, 4);
  96. opts = options;
  97. });
  98. col.add(e, {amazing: true});
  99. equal(added, 'e');
  100. equal(col.length, 5);
  101. equal(col.last(), e);
  102. equal(otherCol.length, 1);
  103. equal(secondAdded, null);
  104. ok(opts.amazing);
  105. var f = new Backbone.Model({id: 20, label : 'f'});
  106. var g = new Backbone.Model({id: 21, label : 'g'});
  107. var h = new Backbone.Model({id: 22, label : 'h'});
  108. var atCol = new Backbone.Collection([f, g, h]);
  109. equal(atCol.length, 3);
  110. atCol.add(e, {at: 1});
  111. equal(atCol.length, 4);
  112. equal(atCol.at(1), e);
  113. equal(atCol.last(), h);
  114. });
  115. test("Collection: add multiple models", function() {
  116. var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
  117. col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
  118. for (var i = 0; i <= 5; i++) {
  119. equal(col.at(i).get('at'), i);
  120. }
  121. });
  122. test("Collection: can't add model to collection twice", function() {
  123. raises(function(){
  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. }, "Can't add the same model to a collection twice");
  130. });
  131. test("Collection: can't add different model with same id to collection twice", function() {
  132. raises(function(){
  133. var col = new Backbone.Collection;
  134. col.add({id: 101});
  135. col.add({id: 101});
  136. ok(false, "duplicate; expected add to fail");
  137. }, "Can't add the same model to a collection twice");
  138. });
  139. test("Collection: add model to multiple collections", function() {
  140. var counter = 0;
  141. var e = new Backbone.Model({id: 10, label : 'e'});
  142. e.bind('add', function(model, collection) {
  143. counter++;
  144. equal(e, model);
  145. if (counter > 1) {
  146. equal(collection, colF);
  147. } else {
  148. equal(collection, colE);
  149. }
  150. });
  151. var colE = new Backbone.Collection([]);
  152. colE.bind('add', function(model, collection) {
  153. equal(e, model);
  154. equal(colE, collection);
  155. });
  156. var colF = new Backbone.Collection([]);
  157. colF.bind('add', function(model, collection) {
  158. equal(e, model);
  159. equal(colF, collection);
  160. });
  161. colE.add(e);
  162. equal(e.collection, colE);
  163. colF.add(e);
  164. equal(e.collection, colE);
  165. });
  166. test("Collection: add model with parse", function() {
  167. var Model = Backbone.Model.extend({
  168. parse: function(obj) {
  169. obj.value += 1;
  170. return obj;
  171. }
  172. });
  173. var Col = Backbone.Collection.extend({model: Model});
  174. var col = new Col;
  175. col.add({value: 1}, {parse: true});
  176. equal(col.at(0).get('value'), 2);
  177. });
  178. test("Collection: add model to collection with sort()-style comparator", function() {
  179. var col = new Backbone.Collection;
  180. col.comparator = function(a, b) {
  181. return a.get('name') < b.get('name') ? -1 : 1;
  182. };
  183. var tom = new Backbone.Model({name: 'Tom'});
  184. var rob = new Backbone.Model({name: 'Rob'});
  185. var tim = new Backbone.Model({name: 'Tim'});
  186. col.add(tom);
  187. col.add(rob);
  188. col.add(tim);
  189. equal(col.indexOf(rob), 0);
  190. equal(col.indexOf(tim), 1);
  191. equal(col.indexOf(tom), 2);
  192. });
  193. test("Collection: comparator that depends on `this`", function() {
  194. var col = new Backbone.Collection;
  195. col.negative = function(num) {
  196. return -num;
  197. };
  198. col.comparator = function(a) {
  199. return this.negative(a.id);
  200. };
  201. col.add([{id: 1}, {id: 2}, {id: 3}]);
  202. equal(col.pluck('id').join(' '), '3 2 1');
  203. });
  204. test("Collection: remove", function() {
  205. var removed = otherRemoved = null;
  206. col.bind('remove', function(model, col, options) {
  207. removed = model.get('label');
  208. equal(options.index, 4);
  209. });
  210. otherCol.bind('remove', function(model, col, options) {
  211. otherRemoved = true;
  212. });
  213. col.remove(e);
  214. equal(removed, 'e');
  215. equal(col.length, 4);
  216. equal(col.first(), d);
  217. equal(otherRemoved, null);
  218. });
  219. test("Collection: events are unbound on remove", function() {
  220. var counter = 0;
  221. var dj = new Backbone.Model();
  222. var emcees = new Backbone.Collection([dj]);
  223. emcees.bind('change', function(){ counter++; });
  224. dj.set({name : 'Kool'});
  225. equal(counter, 1);
  226. emcees.reset([]);
  227. equal(dj.collection, undefined);
  228. dj.set({name : 'Shadow'});
  229. equal(counter, 1);
  230. });
  231. test("Collection: remove in multiple collections", function() {
  232. var modelData = {
  233. id : 5,
  234. title : 'Othello'
  235. };
  236. var passed = false;
  237. var e = new Backbone.Model(modelData);
  238. var f = new Backbone.Model(modelData);
  239. f.bind('remove', function() {
  240. passed = true;
  241. });
  242. var colE = new Backbone.Collection([e]);
  243. var colF = new Backbone.Collection([f]);
  244. ok(e != f);
  245. ok(colE.length == 1);
  246. ok(colF.length == 1);
  247. colE.remove(e);
  248. equal(passed, false);
  249. ok(colE.length == 0);
  250. colF.remove(e);
  251. ok(colF.length == 0);
  252. equal(passed, true);
  253. });
  254. test("Collection: remove same model in multiple collection", function() {
  255. var counter = 0;
  256. var e = new Backbone.Model({id: 5, title: 'Othello'});
  257. e.bind('remove', function(model, collection) {
  258. counter++;
  259. equal(e, model);
  260. if (counter > 1) {
  261. equal(collection, colE);
  262. } else {
  263. equal(collection, colF);
  264. }
  265. });
  266. var colE = new Backbone.Collection([e]);
  267. colE.bind('remove', function(model, collection) {
  268. equal(e, model);
  269. equal(colE, collection);
  270. });
  271. var colF = new Backbone.Collection([e]);
  272. colF.bind('remove', function(model, collection) {
  273. equal(e, model);
  274. equal(colF, collection);
  275. });
  276. equal(colE, e.collection);
  277. colF.remove(e);
  278. ok(colF.length == 0);
  279. ok(colE.length == 1);
  280. equal(counter, 1);
  281. equal(colE, e.collection);
  282. colE.remove(e);
  283. equal(null, e.collection);
  284. ok(colE.length == 0);
  285. equal(counter, 2);
  286. });
  287. test("Collection: model destroy removes from all collections", function() {
  288. var e = new Backbone.Model({id: 5, title: 'Othello'});
  289. e.sync = function(method, model, options) { options.success({}); };
  290. var colE = new Backbone.Collection([e]);
  291. var colF = new Backbone.Collection([e]);
  292. e.destroy();
  293. ok(colE.length == 0);
  294. ok(colF.length == 0);
  295. equal(undefined, e.collection);
  296. });
  297. test("Colllection: non-persisted model destroy removes from all collections", function() {
  298. var e = new Backbone.Model({title: 'Othello'});
  299. e.sync = function(method, model, options) { throw "should not be called"; };
  300. var colE = new Backbone.Collection([e]);
  301. var colF = new Backbone.Collection([e]);
  302. e.destroy();
  303. ok(colE.length == 0);
  304. ok(colF.length == 0);
  305. equal(undefined, e.collection);
  306. });
  307. test("Collection: fetch", function() {
  308. col.fetch();
  309. equal(lastRequest[0], 'read');
  310. equal(lastRequest[1], col);
  311. equal(lastRequest[2].parse, true);
  312. col.fetch({parse: false});
  313. equal(lastRequest[2].parse, false);
  314. });
  315. test("Collection: create", function() {
  316. var model = col.create({label: 'f'}, {wait: true});
  317. equal(lastRequest[0], 'create');
  318. equal(lastRequest[1], model);
  319. equal(model.get('label'), 'f');
  320. equal(model.collection, col);
  321. });
  322. test("Collection: create enforces validation", function() {
  323. var ValidatingModel = Backbone.Model.extend({
  324. validate: function(attrs) {
  325. return "fail";
  326. }
  327. });
  328. var ValidatingCollection = Backbone.Collection.extend({
  329. model: ValidatingModel
  330. });
  331. var col = new ValidatingCollection();
  332. equal(col.create({"foo":"bar"}),false);
  333. });
  334. test("Collection: a failing create runs the error callback", function() {
  335. var ValidatingModel = Backbone.Model.extend({
  336. validate: function(attrs) {
  337. return "fail";
  338. }
  339. });
  340. var ValidatingCollection = Backbone.Collection.extend({
  341. model: ValidatingModel
  342. });
  343. var flag = false;
  344. var callback = function(model, error) { flag = true; };
  345. var col = new ValidatingCollection();
  346. col.create({"foo":"bar"}, { error: callback });
  347. equal(flag, true);
  348. });
  349. test("collection: initialize", function() {
  350. var Collection = Backbone.Collection.extend({
  351. initialize: function() {
  352. this.one = 1;
  353. }
  354. });
  355. var coll = new Collection;
  356. equal(coll.one, 1);
  357. });
  358. test("Collection: toJSON", function() {
  359. equal(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
  360. });
  361. test("Collection: Underscore methods", function() {
  362. equal(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
  363. equal(col.any(function(model){ return model.id === 100; }), false);
  364. equal(col.any(function(model){ return model.id === 0; }), true);
  365. equal(col.indexOf(b), 2);
  366. equal(col.size(), 4);
  367. equal(col.rest().length, 3);
  368. ok(!_.include(col.rest()), a);
  369. ok(!_.include(col.rest()), d);
  370. ok(!col.isEmpty());
  371. ok(!_.include(col.without(d)), d);
  372. equal(col.max(function(model){ return model.id; }).id, 3);
  373. equal(col.min(function(model){ return model.id; }).id, 0);
  374. same(col.chain()
  375. .filter(function(o){ return o.id % 2 === 0; })
  376. .map(function(o){ return o.id * 2; })
  377. .value(),
  378. [0, 4]);
  379. });
  380. test("Collection: reset", function() {
  381. var resetCount = 0;
  382. var models = col.models;
  383. col.bind('reset', function() { resetCount += 1; });
  384. col.reset([]);
  385. equal(resetCount, 1);
  386. equal(col.length, 0);
  387. equal(col.last(), null);
  388. col.reset(models);
  389. equal(resetCount, 2);
  390. equal(col.length, 4);
  391. equal(col.last(), a);
  392. col.reset(_.map(models, function(m){ return m.attributes; }));
  393. equal(resetCount, 3);
  394. equal(col.length, 4);
  395. ok(col.last() !== a);
  396. ok(_.isEqual(col.last().attributes, a.attributes));
  397. });
  398. test("Collection: trigger custom events on models", function() {
  399. var fired = null;
  400. a.bind("custom", function() { fired = true; });
  401. a.trigger("custom");
  402. equal(fired, true);
  403. });
  404. test("Collection: add does not alter arguments", function(){
  405. var attrs = {};
  406. var models = [attrs];
  407. new Backbone.Collection().add(models);
  408. equal(models.length, 1);
  409. ok(attrs === models[0]);
  410. });
  411. test("#714: access `model.collection` in a brand new model.", 2, function() {
  412. var col = new Backbone.Collection;
  413. var Model = Backbone.Model.extend({
  414. set: function(attrs) {
  415. equal(attrs.prop, 'value');
  416. equal(this.collection, col);
  417. }
  418. });
  419. col.model = Model;
  420. col.create({prop: 'value'});
  421. });
  422. test("#574, remove its own reference to the .models array.", function() {
  423. var col = new Backbone.Collection([
  424. {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
  425. ]);
  426. equal(col.length, 6);
  427. col.remove(col.models);
  428. equal(col.length, 0);
  429. });
  430. test("#861, adding models to a collection which do not pass validation", function() {
  431. raises(function() {
  432. var Model = Backbone.Model.extend({
  433. validate: function(attrs) {
  434. if (attrs.id == 3) return "id can't be 3";
  435. }
  436. });
  437. var Collection = Backbone.Collection.extend({
  438. model: Model
  439. });
  440. var col = new Collection;
  441. col.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]);
  442. }, "Can't add an invalid model to a collection");
  443. });
  444. test("Collection: index with comparator", function() {
  445. expect(4);
  446. var counter = 0;
  447. var col = new Backbone.Collection([{id: 2}, {id: 4}], {
  448. comparator: function(model){ return model.id; }
  449. }).on('add', function(model, colleciton, options){
  450. if (model.id == 1) {
  451. equal(options.index, 0);
  452. equal(counter++, 0);
  453. }
  454. if (model.id == 3) {
  455. equal(options.index, 2);
  456. equal(counter++, 1);
  457. }
  458. });
  459. col.add([{id: 3}, {id: 1}]);
  460. });
  461. test("Collection: throwing during add leaves consistent state", function() {
  462. expect(4);
  463. var col = new Backbone.Collection();
  464. col.bind('test', function() { ok(false); });
  465. col.model = Backbone.Model.extend({
  466. validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
  467. });
  468. var model = new col.model({id: 1, valid: true});
  469. raises(function() { col.add([model, {id: 2}]); });
  470. model.trigger('test');
  471. ok(!col.getByCid(model.cid));
  472. ok(!col.get(1));
  473. equal(col.length, 0);
  474. });
  475. });