PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/collection.js

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