PageRenderTime 100ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/frontend/js/vendor/lodash/vendor/backbone/test/collection.js

https://gitlab.com/132nd-etcher/dcs-witchcraft
JavaScript | 1277 lines | 1142 code | 128 blank | 7 comment | 15 complexity | 02997f3a387bb94f2e295b657739d4c5 MD5 | raw file
  1. (function() {
  2. var a, b, c, d, e, col, otherCol;
  3. module("Backbone.Collection", {
  4. setup: function() {
  5. a = new Backbone.Model({id: 3, label: 'a'});
  6. b = new Backbone.Model({id: 2, label: 'b'});
  7. c = new Backbone.Model({id: 1, label: 'c'});
  8. d = new Backbone.Model({id: 0, label: 'd'});
  9. e = null;
  10. col = new Backbone.Collection([a,b,c,d]);
  11. otherCol = new Backbone.Collection();
  12. }
  13. });
  14. test("new and sort", 9, function() {
  15. var counter = 0;
  16. col.on('sort', function(){ counter++; });
  17. equal(col.first(), a, "a should be first");
  18. equal(col.last(), d, "d should be last");
  19. col.comparator = function(a, b) {
  20. return a.id > b.id ? -1 : 1;
  21. };
  22. col.sort();
  23. equal(counter, 1);
  24. equal(col.first(), a, "a should be first");
  25. equal(col.last(), d, "d should be last");
  26. col.comparator = function(model) { return model.id; };
  27. col.sort();
  28. equal(counter, 2);
  29. equal(col.first(), d, "d should be first");
  30. equal(col.last(), a, "a should be last");
  31. equal(col.length, 4);
  32. });
  33. test("String comparator.", 1, function() {
  34. var collection = new Backbone.Collection([
  35. {id: 3},
  36. {id: 1},
  37. {id: 2}
  38. ], {comparator: 'id'});
  39. deepEqual(collection.pluck('id'), [1, 2, 3]);
  40. });
  41. test("new and parse", 3, function() {
  42. var Collection = Backbone.Collection.extend({
  43. parse : function(data) {
  44. return _.filter(data, function(datum) {
  45. return datum.a % 2 === 0;
  46. });
  47. }
  48. });
  49. var models = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
  50. var collection = new Collection(models, {parse: true});
  51. strictEqual(collection.length, 2);
  52. strictEqual(collection.first().get('a'), 2);
  53. strictEqual(collection.last().get('a'), 4);
  54. });
  55. test("get", 6, function() {
  56. equal(col.get(0), d);
  57. equal(col.get(d.clone()), d);
  58. equal(col.get(2), b);
  59. equal(col.get({id: 1}), c);
  60. equal(col.get(c.clone()), c);
  61. equal(col.get(col.first().cid), col.first());
  62. });
  63. test("get with non-default ids", 5, function() {
  64. var col = new Backbone.Collection();
  65. var MongoModel = Backbone.Model.extend({idAttribute: '_id'});
  66. var model = new MongoModel({_id: 100});
  67. col.add(model);
  68. equal(col.get(100), model);
  69. equal(col.get(model.cid), model);
  70. equal(col.get(model), model);
  71. equal(col.get(101), void 0);
  72. var col2 = new Backbone.Collection();
  73. col2.model = MongoModel;
  74. col2.add(model.attributes);
  75. equal(col2.get(model.clone()), col2.first());
  76. });
  77. test("update index when id changes", 4, function() {
  78. var col = new Backbone.Collection();
  79. col.add([
  80. {id : 0, name : 'one'},
  81. {id : 1, name : 'two'}
  82. ]);
  83. var one = col.get(0);
  84. equal(one.get('name'), 'one');
  85. col.on('change:name', function (model) { ok(this.get(model)); });
  86. one.set({name: 'dalmatians', id : 101});
  87. equal(col.get(0), null);
  88. equal(col.get(101).get('name'), 'dalmatians');
  89. });
  90. test("at", 1, function() {
  91. equal(col.at(2), c);
  92. });
  93. test("pluck", 1, function() {
  94. equal(col.pluck('label').join(' '), 'a b c d');
  95. });
  96. test("add", 10, function() {
  97. var added, opts, secondAdded;
  98. added = opts = secondAdded = null;
  99. e = new Backbone.Model({id: 10, label : 'e'});
  100. otherCol.add(e);
  101. otherCol.on('add', function() {
  102. secondAdded = true;
  103. });
  104. col.on('add', function(model, collection, options){
  105. added = model.get('label');
  106. opts = options;
  107. });
  108. col.add(e, {amazing: true});
  109. equal(added, 'e');
  110. equal(col.length, 5);
  111. equal(col.last(), e);
  112. equal(otherCol.length, 1);
  113. equal(secondAdded, null);
  114. ok(opts.amazing);
  115. var f = new Backbone.Model({id: 20, label : 'f'});
  116. var g = new Backbone.Model({id: 21, label : 'g'});
  117. var h = new Backbone.Model({id: 22, label : 'h'});
  118. var atCol = new Backbone.Collection([f, g, h]);
  119. equal(atCol.length, 3);
  120. atCol.add(e, {at: 1});
  121. equal(atCol.length, 4);
  122. equal(atCol.at(1), e);
  123. equal(atCol.last(), h);
  124. });
  125. test("add multiple models", 6, function() {
  126. var col = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
  127. col.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
  128. for (var i = 0; i <= 5; i++) {
  129. equal(col.at(i).get('at'), i);
  130. }
  131. });
  132. test("add; at should have preference over comparator", 1, function() {
  133. var Col = Backbone.Collection.extend({
  134. comparator: function(a,b) {
  135. return a.id > b.id ? -1 : 1;
  136. }
  137. });
  138. var col = new Col([{id: 2}, {id: 3}]);
  139. col.add(new Backbone.Model({id: 1}), {at: 1});
  140. equal(col.pluck('id').join(' '), '3 1 2');
  141. });
  142. test("can't add model to collection twice", function() {
  143. var col = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
  144. equal(col.pluck('id').join(' '), '1 2 3');
  145. });
  146. test("can't add different model with same id to collection twice", 1, function() {
  147. var col = new Backbone.Collection;
  148. col.unshift({id: 101});
  149. col.add({id: 101});
  150. equal(col.length, 1);
  151. });
  152. test("merge in duplicate models with {merge: true}", 3, function() {
  153. var col = new Backbone.Collection;
  154. col.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]);
  155. col.add({id: 1, name: 'Moses'});
  156. equal(col.first().get('name'), 'Moe');
  157. col.add({id: 1, name: 'Moses'}, {merge: true});
  158. equal(col.first().get('name'), 'Moses');
  159. col.add({id: 1, name: 'Tim'}, {merge: true, silent: true});
  160. equal(col.first().get('name'), 'Tim');
  161. });
  162. test("add model to multiple collections", 10, function() {
  163. var counter = 0;
  164. var e = new Backbone.Model({id: 10, label : 'e'});
  165. e.on('add', function(model, collection) {
  166. counter++;
  167. equal(e, model);
  168. if (counter > 1) {
  169. equal(collection, colF);
  170. } else {
  171. equal(collection, colE);
  172. }
  173. });
  174. var colE = new Backbone.Collection([]);
  175. colE.on('add', function(model, collection) {
  176. equal(e, model);
  177. equal(colE, collection);
  178. });
  179. var colF = new Backbone.Collection([]);
  180. colF.on('add', function(model, collection) {
  181. equal(e, model);
  182. equal(colF, collection);
  183. });
  184. colE.add(e);
  185. equal(e.collection, colE);
  186. colF.add(e);
  187. equal(e.collection, colE);
  188. });
  189. test("add model with parse", 1, function() {
  190. var Model = Backbone.Model.extend({
  191. parse: function(obj) {
  192. obj.value += 1;
  193. return obj;
  194. }
  195. });
  196. var Col = Backbone.Collection.extend({model: Model});
  197. var col = new Col;
  198. col.add({value: 1}, {parse: true});
  199. equal(col.at(0).get('value'), 2);
  200. });
  201. test("add with parse and merge", function() {
  202. var collection = new Backbone.Collection();
  203. collection.parse = function(attrs) {
  204. return _.map(attrs, function(model) {
  205. if (model.model) return model.model;
  206. return model;
  207. });
  208. };
  209. collection.add({id: 1});
  210. collection.add({model: {id: 1, name: 'Alf'}}, {parse: true, merge: true});
  211. equal(collection.first().get('name'), 'Alf');
  212. });
  213. test("add model to collection with sort()-style comparator", 3, function() {
  214. var col = new Backbone.Collection;
  215. col.comparator = function(a, b) {
  216. return a.get('name') < b.get('name') ? -1 : 1;
  217. };
  218. var tom = new Backbone.Model({name: 'Tom'});
  219. var rob = new Backbone.Model({name: 'Rob'});
  220. var tim = new Backbone.Model({name: 'Tim'});
  221. col.add(tom);
  222. col.add(rob);
  223. col.add(tim);
  224. equal(col.indexOf(rob), 0);
  225. equal(col.indexOf(tim), 1);
  226. equal(col.indexOf(tom), 2);
  227. });
  228. test("comparator that depends on `this`", 2, function() {
  229. var col = new Backbone.Collection;
  230. col.negative = function(num) {
  231. return -num;
  232. };
  233. col.comparator = function(a) {
  234. return this.negative(a.id);
  235. };
  236. col.add([{id: 1}, {id: 2}, {id: 3}]);
  237. deepEqual(col.pluck('id'), [3, 2, 1]);
  238. col.comparator = function(a, b) {
  239. return this.negative(b.id) - this.negative(a.id);
  240. };
  241. col.sort();
  242. deepEqual(col.pluck('id'), [1, 2, 3]);
  243. });
  244. test("remove", 5, function() {
  245. var removed = null;
  246. var otherRemoved = null;
  247. col.on('remove', function(model, col, options) {
  248. removed = model.get('label');
  249. equal(options.index, 3);
  250. });
  251. otherCol.on('remove', function(model, col, options) {
  252. otherRemoved = true;
  253. });
  254. col.remove(d);
  255. equal(removed, 'd');
  256. equal(col.length, 3);
  257. equal(col.first(), a);
  258. equal(otherRemoved, null);
  259. });
  260. test("add and remove return values", 13, function() {
  261. var Even = Backbone.Model.extend({
  262. validate: function(attrs) {
  263. if (attrs.id % 2 !== 0) return "odd";
  264. }
  265. });
  266. var col = new Backbone.Collection;
  267. col.model = Even;
  268. var list = col.add([{id: 2}, {id: 4}], {validate: true});
  269. equal(list.length, 2);
  270. ok(list[0] instanceof Backbone.Model);
  271. equal(list[1], col.last());
  272. equal(list[1].get('id'), 4);
  273. list = col.add([{id: 3}, {id: 6}], {validate: true});
  274. equal(col.length, 3);
  275. equal(list[0], false);
  276. equal(list[1].get('id'), 6);
  277. var result = col.add({id: 6});
  278. equal(result.cid, list[1].cid);
  279. result = col.remove({id: 6});
  280. equal(col.length, 2);
  281. equal(result.id, 6);
  282. list = col.remove([{id: 2}, {id: 8}]);
  283. equal(col.length, 1);
  284. equal(list[0].get('id'), 2);
  285. equal(list[1], null);
  286. });
  287. test("shift and pop", 2, function() {
  288. var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  289. equal(col.shift().get('a'), 'a');
  290. equal(col.pop().get('c'), 'c');
  291. });
  292. test("slice", 2, function() {
  293. var col = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  294. var array = col.slice(1, 3);
  295. equal(array.length, 2);
  296. equal(array[0].get('b'), 'b');
  297. });
  298. test("events are unbound on remove", 3, function() {
  299. var counter = 0;
  300. var dj = new Backbone.Model();
  301. var emcees = new Backbone.Collection([dj]);
  302. emcees.on('change', function(){ counter++; });
  303. dj.set({name : 'Kool'});
  304. equal(counter, 1);
  305. emcees.reset([]);
  306. equal(dj.collection, undefined);
  307. dj.set({name : 'Shadow'});
  308. equal(counter, 1);
  309. });
  310. test("remove in multiple collections", 7, function() {
  311. var modelData = {
  312. id : 5,
  313. title : 'Othello'
  314. };
  315. var passed = false;
  316. var e = new Backbone.Model(modelData);
  317. var f = new Backbone.Model(modelData);
  318. f.on('remove', function() {
  319. passed = true;
  320. });
  321. var colE = new Backbone.Collection([e]);
  322. var colF = new Backbone.Collection([f]);
  323. ok(e != f);
  324. ok(colE.length === 1);
  325. ok(colF.length === 1);
  326. colE.remove(e);
  327. equal(passed, false);
  328. ok(colE.length === 0);
  329. colF.remove(e);
  330. ok(colF.length === 0);
  331. equal(passed, true);
  332. });
  333. test("remove same model in multiple collection", 16, function() {
  334. var counter = 0;
  335. var e = new Backbone.Model({id: 5, title: 'Othello'});
  336. e.on('remove', function(model, collection) {
  337. counter++;
  338. equal(e, model);
  339. if (counter > 1) {
  340. equal(collection, colE);
  341. } else {
  342. equal(collection, colF);
  343. }
  344. });
  345. var colE = new Backbone.Collection([e]);
  346. colE.on('remove', function(model, collection) {
  347. equal(e, model);
  348. equal(colE, collection);
  349. });
  350. var colF = new Backbone.Collection([e]);
  351. colF.on('remove', function(model, collection) {
  352. equal(e, model);
  353. equal(colF, collection);
  354. });
  355. equal(colE, e.collection);
  356. colF.remove(e);
  357. ok(colF.length === 0);
  358. ok(colE.length === 1);
  359. equal(counter, 1);
  360. equal(colE, e.collection);
  361. colE.remove(e);
  362. equal(null, e.collection);
  363. ok(colE.length === 0);
  364. equal(counter, 2);
  365. });
  366. test("model destroy removes from all collections", 3, function() {
  367. var e = new Backbone.Model({id: 5, title: 'Othello'});
  368. e.sync = function(method, model, options) { options.success(); };
  369. var colE = new Backbone.Collection([e]);
  370. var colF = new Backbone.Collection([e]);
  371. e.destroy();
  372. ok(colE.length === 0);
  373. ok(colF.length === 0);
  374. equal(undefined, e.collection);
  375. });
  376. test("Colllection: non-persisted model destroy removes from all collections", 3, function() {
  377. var e = new Backbone.Model({title: 'Othello'});
  378. e.sync = function(method, model, options) { throw "should not be called"; };
  379. var colE = new Backbone.Collection([e]);
  380. var colF = new Backbone.Collection([e]);
  381. e.destroy();
  382. ok(colE.length === 0);
  383. ok(colF.length === 0);
  384. equal(undefined, e.collection);
  385. });
  386. test("fetch", 4, function() {
  387. var collection = new Backbone.Collection;
  388. collection.url = '/test';
  389. collection.fetch();
  390. equal(this.syncArgs.method, 'read');
  391. equal(this.syncArgs.model, collection);
  392. equal(this.syncArgs.options.parse, true);
  393. collection.fetch({parse: false});
  394. equal(this.syncArgs.options.parse, false);
  395. });
  396. test("fetch with an error response triggers an error event", 1, function () {
  397. var collection = new Backbone.Collection();
  398. collection.on('error', function () {
  399. ok(true);
  400. });
  401. collection.sync = function (method, model, options) { options.error(); };
  402. collection.fetch();
  403. });
  404. test("ensure fetch only parses once", 1, function() {
  405. var collection = new Backbone.Collection;
  406. var counter = 0;
  407. collection.parse = function(models) {
  408. counter++;
  409. return models;
  410. };
  411. collection.url = '/test';
  412. collection.fetch();
  413. this.syncArgs.options.success();
  414. equal(counter, 1);
  415. });
  416. test("create", 4, function() {
  417. var collection = new Backbone.Collection;
  418. collection.url = '/test';
  419. var model = collection.create({label: 'f'}, {wait: true});
  420. equal(this.syncArgs.method, 'create');
  421. equal(this.syncArgs.model, model);
  422. equal(model.get('label'), 'f');
  423. equal(model.collection, collection);
  424. });
  425. test("create with validate:true enforces validation", 3, function() {
  426. var ValidatingModel = Backbone.Model.extend({
  427. validate: function(attrs) {
  428. return "fail";
  429. }
  430. });
  431. var ValidatingCollection = Backbone.Collection.extend({
  432. model: ValidatingModel
  433. });
  434. var col = new ValidatingCollection();
  435. col.on('invalid', function (collection, error, options) {
  436. equal(error, "fail");
  437. equal(options.validationError, 'fail');
  438. });
  439. equal(col.create({"foo":"bar"}, {validate:true}), false);
  440. });
  441. test("a failing create returns model with errors", function() {
  442. var ValidatingModel = Backbone.Model.extend({
  443. validate: function(attrs) {
  444. return "fail";
  445. }
  446. });
  447. var ValidatingCollection = Backbone.Collection.extend({
  448. model: ValidatingModel
  449. });
  450. var col = new ValidatingCollection();
  451. var m = col.create({"foo":"bar"});
  452. equal(m.validationError, 'fail');
  453. equal(col.length, 1);
  454. });
  455. test("initialize", 1, function() {
  456. var Collection = Backbone.Collection.extend({
  457. initialize: function() {
  458. this.one = 1;
  459. }
  460. });
  461. var coll = new Collection;
  462. equal(coll.one, 1);
  463. });
  464. test("toJSON", 1, function() {
  465. equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
  466. });
  467. test("where and findWhere", 8, function() {
  468. var model = new Backbone.Model({a: 1});
  469. var coll = new Backbone.Collection([
  470. model,
  471. {a: 1},
  472. {a: 1, b: 2},
  473. {a: 2, b: 2},
  474. {a: 3}
  475. ]);
  476. equal(coll.where({a: 1}).length, 3);
  477. equal(coll.where({a: 2}).length, 1);
  478. equal(coll.where({a: 3}).length, 1);
  479. equal(coll.where({b: 1}).length, 0);
  480. equal(coll.where({b: 2}).length, 2);
  481. equal(coll.where({a: 1, b: 2}).length, 1);
  482. equal(coll.findWhere({a: 1}), model);
  483. equal(coll.findWhere({a: 4}), void 0);
  484. });
  485. test("Underscore methods", 14, function() {
  486. equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
  487. equal(col.any(function(model){ return model.id === 100; }), false);
  488. equal(col.any(function(model){ return model.id === 0; }), true);
  489. equal(col.indexOf(b), 1);
  490. equal(col.size(), 4);
  491. equal(col.rest().length, 3);
  492. ok(!_.include(col.rest(), a));
  493. ok(_.include(col.rest(), d));
  494. ok(!col.isEmpty());
  495. ok(!_.include(col.without(d), d));
  496. equal(col.max(function(model){ return model.id; }).id, 3);
  497. equal(col.min(function(model){ return model.id; }).id, 0);
  498. deepEqual(col.chain()
  499. .filter(function(o){ return o.id % 2 === 0; })
  500. .map(function(o){ return o.id * 2; })
  501. .value(),
  502. [4, 0]);
  503. deepEqual(col.difference([c, d]), [a, b]);
  504. });
  505. test("reset", 12, function() {
  506. var resetCount = 0;
  507. var models = col.models;
  508. col.on('reset', function() { resetCount += 1; });
  509. col.reset([]);
  510. equal(resetCount, 1);
  511. equal(col.length, 0);
  512. equal(col.last(), null);
  513. col.reset(models);
  514. equal(resetCount, 2);
  515. equal(col.length, 4);
  516. equal(col.last(), d);
  517. col.reset(_.map(models, function(m){ return m.attributes; }));
  518. equal(resetCount, 3);
  519. equal(col.length, 4);
  520. ok(col.last() !== d);
  521. ok(_.isEqual(col.last().attributes, d.attributes));
  522. col.reset();
  523. equal(col.length, 0);
  524. equal(resetCount, 4);
  525. });
  526. test ("reset with different values", function(){
  527. var col = new Backbone.Collection({id: 1});
  528. col.reset({id: 1, a: 1});
  529. equal(col.get(1).get('a'), 1);
  530. });
  531. test("same references in reset", function() {
  532. var model = new Backbone.Model({id: 1});
  533. var collection = new Backbone.Collection({id: 1});
  534. collection.reset(model);
  535. equal(collection.get(1), model);
  536. });
  537. test("reset passes caller options", 3, function() {
  538. var Model = Backbone.Model.extend({
  539. initialize: function(attrs, options) {
  540. this.model_parameter = options.model_parameter;
  541. }
  542. });
  543. var col = new (Backbone.Collection.extend({ model: Model }))();
  544. col.reset([{ astring: "green", anumber: 1 }, { astring: "blue", anumber: 2 }], { model_parameter: 'model parameter' });
  545. equal(col.length, 2);
  546. col.each(function(model) {
  547. equal(model.model_parameter, 'model parameter');
  548. });
  549. });
  550. test("trigger custom events on models", 1, function() {
  551. var fired = null;
  552. a.on("custom", function() { fired = true; });
  553. a.trigger("custom");
  554. equal(fired, true);
  555. });
  556. test("add does not alter arguments", 2, function(){
  557. var attrs = {};
  558. var models = [attrs];
  559. new Backbone.Collection().add(models);
  560. equal(models.length, 1);
  561. ok(attrs === models[0]);
  562. });
  563. test("#714: access `model.collection` in a brand new model.", 2, function() {
  564. var collection = new Backbone.Collection;
  565. collection.url = '/test';
  566. var Model = Backbone.Model.extend({
  567. set: function(attrs) {
  568. equal(attrs.prop, 'value');
  569. equal(this.collection, collection);
  570. return this;
  571. }
  572. });
  573. collection.model = Model;
  574. collection.create({prop: 'value'});
  575. });
  576. test("#574, remove its own reference to the .models array.", 2, function() {
  577. var col = new Backbone.Collection([
  578. {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
  579. ]);
  580. equal(col.length, 6);
  581. col.remove(col.models);
  582. equal(col.length, 0);
  583. });
  584. test("#861, adding models to a collection which do not pass validation, with validate:true", function() {
  585. var Model = Backbone.Model.extend({
  586. validate: function(attrs) {
  587. if (attrs.id == 3) return "id can't be 3";
  588. }
  589. });
  590. var Collection = Backbone.Collection.extend({
  591. model: Model
  592. });
  593. var collection = new Collection;
  594. collection.on("error", function() { ok(true); });
  595. collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {validate:true});
  596. deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
  597. });
  598. test("Invalid models are discarded with validate:true.", 5, function() {
  599. var collection = new Backbone.Collection;
  600. collection.on('test', function() { ok(true); });
  601. collection.model = Backbone.Model.extend({
  602. validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
  603. });
  604. var model = new collection.model({id: 1, valid: true});
  605. collection.add([model, {id: 2}], {validate:true});
  606. model.trigger('test');
  607. ok(collection.get(model.cid));
  608. ok(collection.get(1));
  609. ok(!collection.get(2));
  610. equal(collection.length, 1);
  611. });
  612. test("multiple copies of the same model", 3, function() {
  613. var col = new Backbone.Collection();
  614. var model = new Backbone.Model();
  615. col.add([model, model]);
  616. equal(col.length, 1);
  617. col.add([{id: 1}, {id: 1}]);
  618. equal(col.length, 2);
  619. equal(col.last().id, 1);
  620. });
  621. test("#964 - collection.get return inconsistent", 2, function() {
  622. var c = new Backbone.Collection();
  623. ok(c.get(null) === undefined);
  624. ok(c.get() === undefined);
  625. });
  626. test("#1112 - passing options.model sets collection.model", 2, function() {
  627. var Model = Backbone.Model.extend({});
  628. var c = new Backbone.Collection([{id: 1}], {model: Model});
  629. ok(c.model === Model);
  630. ok(c.at(0) instanceof Model);
  631. });
  632. test("null and undefined are invalid ids.", 2, function() {
  633. var model = new Backbone.Model({id: 1});
  634. var collection = new Backbone.Collection([model]);
  635. model.set({id: null});
  636. ok(!collection.get('null'));
  637. model.set({id: 1});
  638. model.set({id: undefined});
  639. ok(!collection.get('undefined'));
  640. });
  641. test("falsy comparator", 4, function(){
  642. var Col = Backbone.Collection.extend({
  643. comparator: function(model){ return model.id; }
  644. });
  645. var col = new Col();
  646. var colFalse = new Col(null, {comparator: false});
  647. var colNull = new Col(null, {comparator: null});
  648. var colUndefined = new Col(null, {comparator: undefined});
  649. ok(col.comparator);
  650. ok(!colFalse.comparator);
  651. ok(!colNull.comparator);
  652. ok(colUndefined.comparator);
  653. });
  654. test("#1355 - `options` is passed to success callbacks", 2, function(){
  655. var m = new Backbone.Model({x:1});
  656. var col = new Backbone.Collection();
  657. var opts = {
  658. success: function(collection, resp, options){
  659. ok(options);
  660. }
  661. };
  662. col.sync = m.sync = function( method, collection, options ){
  663. options.success(collection, [], options);
  664. };
  665. col.fetch(opts);
  666. col.create(m, opts);
  667. });
  668. test("#1412 - Trigger 'request' and 'sync' events.", 4, function() {
  669. var collection = new Backbone.Collection;
  670. collection.url = '/test';
  671. Backbone.ajax = function(settings){ settings.success(); };
  672. collection.on('request', function(obj, xhr, options) {
  673. ok(obj === collection, "collection has correct 'request' event after fetching");
  674. });
  675. collection.on('sync', function(obj, response, options) {
  676. ok(obj === collection, "collection has correct 'sync' event after fetching");
  677. });
  678. collection.fetch();
  679. collection.off();
  680. collection.on('request', function(obj, xhr, options) {
  681. ok(obj === collection.get(1), "collection has correct 'request' event after one of its models save");
  682. });
  683. collection.on('sync', function(obj, response, options) {
  684. ok(obj === collection.get(1), "collection has correct 'sync' event after one of its models save");
  685. });
  686. collection.create({id: 1});
  687. collection.off();
  688. });
  689. test("#1447 - create with wait adds model.", 1, function() {
  690. var collection = new Backbone.Collection;
  691. var model = new Backbone.Model;
  692. model.sync = function(method, model, options){ options.success(); };
  693. collection.on('add', function(){ ok(true); });
  694. collection.create(model, {wait: true});
  695. });
  696. test("#1448 - add sorts collection after merge.", 1, function() {
  697. var collection = new Backbone.Collection([
  698. {id: 1, x: 1},
  699. {id: 2, x: 2}
  700. ]);
  701. collection.comparator = function(model){ return model.get('x'); };
  702. collection.add({id: 1, x: 3}, {merge: true});
  703. deepEqual(collection.pluck('id'), [2, 1]);
  704. });
  705. test("#1655 - groupBy can be used with a string argument.", 3, function() {
  706. var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
  707. var grouped = collection.groupBy('x');
  708. strictEqual(_.keys(grouped).length, 2);
  709. strictEqual(grouped[1][0].get('x'), 1);
  710. strictEqual(grouped[2][0].get('x'), 2);
  711. });
  712. test("#1655 - sortBy can be used with a string argument.", 1, function() {
  713. var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
  714. var values = _.map(collection.sortBy('x'), function(model) {
  715. return model.get('x');
  716. });
  717. deepEqual(values, [1, 2, 3]);
  718. });
  719. test("#1604 - Removal during iteration.", 0, function() {
  720. var collection = new Backbone.Collection([{}, {}]);
  721. collection.on('add', function() {
  722. collection.at(0).destroy();
  723. });
  724. collection.add({}, {at: 0});
  725. });
  726. test("#1638 - `sort` during `add` triggers correctly.", function() {
  727. var collection = new Backbone.Collection;
  728. collection.comparator = function(model) { return model.get('x'); };
  729. var added = [];
  730. collection.on('add', function(model) {
  731. model.set({x: 3});
  732. collection.sort();
  733. added.push(model.id);
  734. });
  735. collection.add([{id: 1, x: 1}, {id: 2, x: 2}]);
  736. deepEqual(added, [1, 2]);
  737. });
  738. test("fetch parses models by default", 1, function() {
  739. var model = {};
  740. var Collection = Backbone.Collection.extend({
  741. url: 'test',
  742. model: Backbone.Model.extend({
  743. parse: function(resp) {
  744. strictEqual(resp, model);
  745. }
  746. })
  747. });
  748. new Collection().fetch();
  749. this.ajaxSettings.success([model]);
  750. });
  751. test("`sort` shouldn't always fire on `add`", 1, function() {
  752. var c = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
  753. comparator: 'id'
  754. });
  755. c.sort = function(){ ok(true); };
  756. c.add([]);
  757. c.add({id: 1});
  758. c.add([{id: 2}, {id: 3}]);
  759. c.add({id: 4});
  760. });
  761. test("#1407 parse option on constructor parses collection and models", 2, function() {
  762. var model = {
  763. namespace : [{id: 1}, {id:2}]
  764. };
  765. var Collection = Backbone.Collection.extend({
  766. model: Backbone.Model.extend({
  767. parse: function(model) {
  768. model.name = 'test';
  769. return model;
  770. }
  771. }),
  772. parse: function(model) {
  773. return model.namespace;
  774. }
  775. });
  776. var c = new Collection(model, {parse:true});
  777. equal(c.length, 2);
  778. equal(c.at(0).get('name'), 'test');
  779. });
  780. test("#1407 parse option on reset parses collection and models", 2, function() {
  781. var model = {
  782. namespace : [{id: 1}, {id:2}]
  783. };
  784. var Collection = Backbone.Collection.extend({
  785. model: Backbone.Model.extend({
  786. parse: function(model) {
  787. model.name = 'test';
  788. return model;
  789. }
  790. }),
  791. parse: function(model) {
  792. return model.namespace;
  793. }
  794. });
  795. var c = new Collection();
  796. c.reset(model, {parse:true});
  797. equal(c.length, 2);
  798. equal(c.at(0).get('name'), 'test');
  799. });
  800. test("Reset includes previous models in triggered event.", 1, function() {
  801. var model = new Backbone.Model();
  802. var collection = new Backbone.Collection([model])
  803. .on('reset', function(collection, options) {
  804. deepEqual(options.previousModels, [model]);
  805. });
  806. collection.reset([]);
  807. });
  808. test("set", function() {
  809. var m1 = new Backbone.Model();
  810. var m2 = new Backbone.Model({id: 2});
  811. var m3 = new Backbone.Model();
  812. var c = new Backbone.Collection([m1, m2]);
  813. // Test add/change/remove events
  814. c.on('add', function(model) {
  815. strictEqual(model, m3);
  816. });
  817. c.on('change', function(model) {
  818. strictEqual(model, m2);
  819. });
  820. c.on('remove', function(model) {
  821. strictEqual(model, m1);
  822. });
  823. // remove: false doesn't remove any models
  824. c.set([], {remove: false});
  825. strictEqual(c.length, 2);
  826. // add: false doesn't add any models
  827. c.set([m1, m2, m3], {add: false});
  828. strictEqual(c.length, 2);
  829. // merge: false doesn't change any models
  830. c.set([m1, {id: 2, a: 1}], {merge: false});
  831. strictEqual(m2.get('a'), void 0);
  832. // add: false, remove: false only merges existing models
  833. c.set([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false});
  834. strictEqual(c.length, 2);
  835. strictEqual(m2.get('a'), 0);
  836. // default options add/remove/merge as appropriate
  837. c.set([{id: 2, a: 1}, m3]);
  838. strictEqual(c.length, 2);
  839. strictEqual(m2.get('a'), 1);
  840. // Test removing models not passing an argument
  841. c.off('remove').on('remove', function(model) {
  842. ok(model === m2 || model === m3);
  843. });
  844. c.set([]);
  845. strictEqual(c.length, 0);
  846. });
  847. test("set with many models does not overflow the stack", function() {
  848. var n = 150000;
  849. var collection = new Backbone.Collection();
  850. var models = [];
  851. for (var i = 0; i < n; i++) {
  852. models.push({id: i});
  853. }
  854. collection.set(models);
  855. equal(collection.length, n);
  856. collection.reset();
  857. collection.set(models, {at: 0});
  858. equal(collection.length, n);
  859. });
  860. test("set with only cids", 3, function() {
  861. var m1 = new Backbone.Model;
  862. var m2 = new Backbone.Model;
  863. var c = new Backbone.Collection;
  864. c.set([m1, m2]);
  865. equal(c.length, 2);
  866. c.set([m1]);
  867. equal(c.length, 1);
  868. c.set([m1, m1, m1, m2, m2], {remove: false});
  869. equal(c.length, 2);
  870. });
  871. test("set with only idAttribute", 3, function() {
  872. var m1 = { _id: 1 };
  873. var m2 = { _id: 2 };
  874. var col = Backbone.Collection.extend({
  875. model: Backbone.Model.extend({
  876. idAttribute: '_id'
  877. })
  878. });
  879. var c = new col;
  880. c.set([m1, m2]);
  881. equal(c.length, 2);
  882. c.set([m1]);
  883. equal(c.length, 1);
  884. c.set([m1, m1, m1, m2, m2], {remove: false});
  885. equal(c.length, 2);
  886. });
  887. test("set + merge with default values defined", function() {
  888. var Model = Backbone.Model.extend({
  889. defaults: {
  890. key: 'value'
  891. }
  892. });
  893. var m = new Model({id: 1});
  894. var col = new Backbone.Collection([m], {model: Model});
  895. equal(col.first().get('key'), 'value');
  896. col.set({id: 1, key: 'other'});
  897. equal(col.first().get('key'), 'other');
  898. col.set({id: 1, other: 'value'});
  899. equal(col.first().get('key'), 'other');
  900. equal(col.length, 1);
  901. });
  902. test('merge without mutation', function () {
  903. var Model = Backbone.Model.extend({
  904. initialize: function (attrs, options) {
  905. if (attrs.child) {
  906. this.set('child', new Model(attrs.child, options), options);
  907. }
  908. }
  909. });
  910. var Collection = Backbone.Collection.extend({model: Model});
  911. var data = [{id: 1, child: {id: 2}}];
  912. var collection = new Collection(data);
  913. equal(collection.first().id, 1);
  914. collection.set(data);
  915. equal(collection.first().id, 1);
  916. collection.set([{id: 2, child: {id: 2}}].concat(data));
  917. deepEqual(collection.pluck('id'), [2, 1]);
  918. });
  919. test("`set` and model level `parse`", function() {
  920. var Model = Backbone.Model.extend({});
  921. var Collection = Backbone.Collection.extend({
  922. model: Model,
  923. parse: function (res) { return _.pluck(res.models, 'model'); }
  924. });
  925. var model = new Model({id: 1});
  926. var collection = new Collection(model);
  927. collection.set({models: [
  928. {model: {id: 1}},
  929. {model: {id: 2}}
  930. ]}, {parse: true});
  931. equal(collection.first(), model);
  932. });
  933. test("`set` data is only parsed once", function() {
  934. var collection = new Backbone.Collection();
  935. collection.model = Backbone.Model.extend({
  936. parse: function (data) {
  937. equal(data.parsed, void 0);
  938. data.parsed = true;
  939. return data;
  940. }
  941. });
  942. collection.set({}, {parse: true});
  943. });
  944. test('`set` matches input order in the absence of a comparator', function () {
  945. var one = new Backbone.Model({id: 1});
  946. var two = new Backbone.Model({id: 2});
  947. var three = new Backbone.Model({id: 3});
  948. var collection = new Backbone.Collection([one, two, three]);
  949. collection.set([{id: 3}, {id: 2}, {id: 1}]);
  950. deepEqual(collection.models, [three, two, one]);
  951. collection.set([{id: 1}, {id: 2}]);
  952. deepEqual(collection.models, [one, two]);
  953. collection.set([two, three, one]);
  954. deepEqual(collection.models, [two, three, one]);
  955. collection.set([{id: 1}, {id: 2}], {remove: false});
  956. deepEqual(collection.models, [two, three, one]);
  957. collection.set([{id: 1}, {id: 2}, {id: 3}], {merge: false});
  958. deepEqual(collection.models, [one, two, three]);
  959. collection.set([three, two, one, {id: 4}], {add: false});
  960. deepEqual(collection.models, [one, two, three]);
  961. });
  962. test("#1894 - Push should not trigger a sort", 0, function() {
  963. var Collection = Backbone.Collection.extend({
  964. comparator: 'id',
  965. sort: function() {
  966. ok(false);
  967. }
  968. });
  969. new Collection().push({id: 1});
  970. });
  971. test("#2428 - push duplicate models, return the correct one", 1, function() {
  972. var col = new Backbone.Collection;
  973. var model1 = col.push({id: 101});
  974. var model2 = col.push({id: 101})
  975. ok(model2.cid == model1.cid);
  976. });
  977. test("`set` with non-normal id", function() {
  978. var Collection = Backbone.Collection.extend({
  979. model: Backbone.Model.extend({idAttribute: '_id'})
  980. });
  981. var collection = new Collection({_id: 1});
  982. collection.set([{_id: 1, a: 1}], {add: false});
  983. equal(collection.first().get('a'), 1);
  984. });
  985. test("#1894 - `sort` can optionally be turned off", 0, function() {
  986. var Collection = Backbone.Collection.extend({
  987. comparator: 'id',
  988. sort: function() { ok(true); }
  989. });
  990. new Collection().add({id: 1}, {sort: false});
  991. });
  992. test("#1915 - `parse` data in the right order in `set`", function() {
  993. var collection = new (Backbone.Collection.extend({
  994. parse: function (data) {
  995. strictEqual(data.status, 'ok');
  996. return data.data;
  997. }
  998. }));
  999. var res = {status: 'ok', data:[{id: 1}]};
  1000. collection.set(res, {parse: true});
  1001. });
  1002. asyncTest("#1939 - `parse` is passed `options`", 1, function () {
  1003. var collection = new (Backbone.Collection.extend({
  1004. url: '/',
  1005. parse: function (data, options) {
  1006. strictEqual(options.xhr.someHeader, 'headerValue');
  1007. return data;
  1008. }
  1009. }));
  1010. var ajax = Backbone.ajax;
  1011. Backbone.ajax = function (params) {
  1012. _.defer(params.success);
  1013. return {someHeader: 'headerValue'};
  1014. };
  1015. collection.fetch({
  1016. success: function () { start(); }
  1017. });
  1018. Backbone.ajax = ajax;
  1019. });
  1020. test("`add` only `sort`s when necessary", 2, function () {
  1021. var collection = new (Backbone.Collection.extend({
  1022. comparator: 'a'
  1023. }))([{id: 1}, {id: 2}, {id: 3}]);
  1024. collection.on('sort', function () { ok(true); });
  1025. collection.add({id: 4}); // do sort, new model
  1026. collection.add({id: 1, a: 1}, {merge: true}); // do sort, comparator change
  1027. collection.add({id: 1, b: 1}, {merge: true}); // don't sort, no comparator change
  1028. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no comparator change
  1029. collection.add(collection.models); // don't sort, nothing new
  1030. collection.add(collection.models, {merge: true}); // don't sort
  1031. });
  1032. test("`add` only `sort`s when necessary with comparator function", 3, function () {
  1033. var collection = new (Backbone.Collection.extend({
  1034. comparator: function(a, b) {
  1035. return a.get('a') > b.get('a') ? 1 : (a.get('a') < b.get('a') ? -1 : 0);
  1036. }
  1037. }))([{id: 1}, {id: 2}, {id: 3}]);
  1038. collection.on('sort', function () { ok(true); });
  1039. collection.add({id: 4}); // do sort, new model
  1040. collection.add({id: 1, a: 1}, {merge: true}); // do sort, model change
  1041. collection.add({id: 1, b: 1}, {merge: true}); // do sort, model change
  1042. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no model change
  1043. collection.add(collection.models); // don't sort, nothing new
  1044. collection.add(collection.models, {merge: true}); // don't sort
  1045. });
  1046. test("Attach options to collection.", 2, function() {
  1047. var model = new Backbone.Model;
  1048. var comparator = function(){};
  1049. var collection = new Backbone.Collection([], {
  1050. model: model,
  1051. comparator: comparator
  1052. });
  1053. ok(collection.model === model);
  1054. ok(collection.comparator === comparator);
  1055. });
  1056. test("`add` overrides `set` flags", function () {
  1057. var collection = new Backbone.Collection();
  1058. collection.once('add', function (model, collection, options) {
  1059. collection.add({id: 2}, options);
  1060. });
  1061. collection.set({id: 1});
  1062. equal(collection.length, 2);
  1063. });
  1064. test("#2606 - Collection#create, success arguments", 1, function() {
  1065. var collection = new Backbone.Collection;
  1066. collection.url = 'test';
  1067. collection.create({}, {
  1068. success: function(model, resp, options) {
  1069. strictEqual(resp, 'response');
  1070. }
  1071. });
  1072. this.ajaxSettings.success('response');
  1073. });
  1074. test("#2612 - nested `parse` works with `Collection#set`", function() {
  1075. var Job = Backbone.Model.extend({
  1076. constructor: function() {
  1077. this.items = new Items();
  1078. Backbone.Model.apply(this, arguments);
  1079. },
  1080. parse: function(attrs) {
  1081. this.items.set(attrs.items, {parse: true});
  1082. return _.omit(attrs, 'items');
  1083. }
  1084. });
  1085. var Item = Backbone.Model.extend({
  1086. constructor: function() {
  1087. this.subItems = new Backbone.Collection();
  1088. Backbone.Model.apply(this, arguments);
  1089. },
  1090. parse: function(attrs) {
  1091. this.subItems.set(attrs.subItems, {parse: true});
  1092. return _.omit(attrs, 'subItems');
  1093. }
  1094. });
  1095. var Items = Backbone.Collection.extend({
  1096. model: Item
  1097. });
  1098. var data = {
  1099. name: 'JobName',
  1100. id: 1,
  1101. items: [{
  1102. id: 1,
  1103. name: 'Sub1',
  1104. subItems: [
  1105. {id: 1, subName: 'One'},
  1106. {id: 2, subName: 'Two'}
  1107. ]
  1108. }, {
  1109. id: 2,
  1110. name: 'Sub2',
  1111. subItems: [
  1112. {id: 3, subName: 'Three'},
  1113. {id: 4, subName: 'Four'}
  1114. ]
  1115. }]
  1116. };
  1117. var newData = {
  1118. name: 'NewJobName',
  1119. id: 1,
  1120. items: [{
  1121. id: 1,
  1122. name: 'NewSub1',
  1123. subItems: [
  1124. {id: 1,subName: 'NewOne'},
  1125. {id: 2,subName: 'NewTwo'}
  1126. ]
  1127. }, {
  1128. id: 2,
  1129. name: 'NewSub2',
  1130. subItems: [
  1131. {id: 3,subName: 'NewThree'},
  1132. {id: 4,subName: 'NewFour'}
  1133. ]
  1134. }]
  1135. };
  1136. var job = new Job(data, {parse: true});
  1137. equal(job.get('name'), 'JobName');
  1138. equal(job.items.at(0).get('name'), 'Sub1');
  1139. equal(job.items.length, 2);
  1140. equal(job.items.get(1).subItems.get(1).get('subName'), 'One');
  1141. equal(job.items.get(2).subItems.get(3).get('subName'), 'Three');
  1142. job.set(job.parse(newData, {parse: true}));
  1143. equal(job.get('name'), 'NewJobName');
  1144. equal(job.items.at(0).get('name'), 'NewSub1');
  1145. equal(job.items.length, 2);
  1146. equal(job.items.get(1).subItems.get(1).get('subName'), 'NewOne');
  1147. equal(job.items.get(2).subItems.get(3).get('subName'), 'NewThree');
  1148. });
  1149. })();