PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/backbone/test/collection.js

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