PageRenderTime 84ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 1ms

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

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