PageRenderTime 69ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/bower_components/lodash/vendor/backbone/test/collection.js

https://github.com/2sic/eav-ui
JavaScript | 2023 lines | 1803 code | 207 blank | 13 comment | 15 complexity | 2c0abbe7d93f9b998e942cd3f9d445d8 MD5 | raw file
Possible License(s): AGPL-3.0, Apache-2.0
  1. (function(QUnit) {
  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('has', function(assert) {
  92. assert.expect(15);
  93. assert.ok(col.has(a));
  94. assert.ok(col.has(b));
  95. assert.ok(col.has(c));
  96. assert.ok(col.has(d));
  97. assert.ok(col.has(a.id));
  98. assert.ok(col.has(b.id));
  99. assert.ok(col.has(c.id));
  100. assert.ok(col.has(d.id));
  101. assert.ok(col.has(a.cid));
  102. assert.ok(col.has(b.cid));
  103. assert.ok(col.has(c.cid));
  104. assert.ok(col.has(d.cid));
  105. var outsider = new Backbone.Model({id: 4});
  106. assert.notOk(col.has(outsider));
  107. assert.notOk(col.has(outsider.id));
  108. assert.notOk(col.has(outsider.cid));
  109. });
  110. QUnit.test('update index when id changes', function(assert) {
  111. assert.expect(4);
  112. var collection = new Backbone.Collection();
  113. collection.add([
  114. {id: 0, name: 'one'},
  115. {id: 1, name: 'two'}
  116. ]);
  117. var one = collection.get(0);
  118. assert.equal(one.get('name'), 'one');
  119. collection.on('change:name', function(model) { assert.ok(this.get(model)); });
  120. one.set({name: 'dalmatians', id: 101});
  121. assert.equal(collection.get(0), null);
  122. assert.equal(collection.get(101).get('name'), 'dalmatians');
  123. });
  124. QUnit.test('at', function(assert) {
  125. assert.expect(2);
  126. assert.equal(col.at(2), c);
  127. assert.equal(col.at(-2), c);
  128. });
  129. QUnit.test('pluck', function(assert) {
  130. assert.expect(1);
  131. assert.equal(col.pluck('label').join(' '), 'a b c d');
  132. });
  133. QUnit.test('add', function(assert) {
  134. assert.expect(14);
  135. var added, opts, secondAdded;
  136. added = opts = secondAdded = null;
  137. e = new Backbone.Model({id: 10, label: 'e'});
  138. otherCol.add(e);
  139. otherCol.on('add', function() {
  140. secondAdded = true;
  141. });
  142. col.on('add', function(model, collection, options){
  143. added = model.get('label');
  144. opts = options;
  145. });
  146. col.add(e, {amazing: true});
  147. assert.equal(added, 'e');
  148. assert.equal(col.length, 5);
  149. assert.equal(col.last(), e);
  150. assert.equal(otherCol.length, 1);
  151. assert.equal(secondAdded, null);
  152. assert.ok(opts.amazing);
  153. var f = new Backbone.Model({id: 20, label: 'f'});
  154. var g = new Backbone.Model({id: 21, label: 'g'});
  155. var h = new Backbone.Model({id: 22, label: 'h'});
  156. var atCol = new Backbone.Collection([f, g, h]);
  157. assert.equal(atCol.length, 3);
  158. atCol.add(e, {at: 1});
  159. assert.equal(atCol.length, 4);
  160. assert.equal(atCol.at(1), e);
  161. assert.equal(atCol.last(), h);
  162. var coll = new Backbone.Collection(new Array(2));
  163. var addCount = 0;
  164. coll.on('add', function(){
  165. addCount += 1;
  166. });
  167. coll.add([undefined, f, g]);
  168. assert.equal(coll.length, 5);
  169. assert.equal(addCount, 3);
  170. coll.add(new Array(4));
  171. assert.equal(coll.length, 9);
  172. assert.equal(addCount, 7);
  173. });
  174. QUnit.test('add multiple models', function(assert) {
  175. assert.expect(6);
  176. var collection = new Backbone.Collection([{at: 0}, {at: 1}, {at: 9}]);
  177. collection.add([{at: 2}, {at: 3}, {at: 4}, {at: 5}, {at: 6}, {at: 7}, {at: 8}], {at: 2});
  178. for (var i = 0; i <= 5; i++) {
  179. assert.equal(collection.at(i).get('at'), i);
  180. }
  181. });
  182. QUnit.test('add; at should have preference over comparator', function(assert) {
  183. assert.expect(1);
  184. var Col = Backbone.Collection.extend({
  185. comparator: function(m1, m2) {
  186. return m1.id > m2.id ? -1 : 1;
  187. }
  188. });
  189. var collection = new Col([{id: 2}, {id: 3}]);
  190. collection.add(new Backbone.Model({id: 1}), {at: 1});
  191. assert.equal(collection.pluck('id').join(' '), '3 1 2');
  192. });
  193. QUnit.test('add; at should add to the end if the index is out of bounds', function(assert) {
  194. assert.expect(1);
  195. var collection = new Backbone.Collection([{id: 2}, {id: 3}]);
  196. collection.add(new Backbone.Model({id: 1}), {at: 5});
  197. assert.equal(collection.pluck('id').join(' '), '2 3 1');
  198. });
  199. QUnit.test("can't add model to collection twice", function(assert) {
  200. var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 1}, {id: 2}, {id: 3}]);
  201. assert.equal(collection.pluck('id').join(' '), '1 2 3');
  202. });
  203. QUnit.test("can't add different model with same id to collection twice", function(assert) {
  204. assert.expect(1);
  205. var collection = new Backbone.Collection;
  206. collection.unshift({id: 101});
  207. collection.add({id: 101});
  208. assert.equal(collection.length, 1);
  209. });
  210. QUnit.test('merge in duplicate models with {merge: true}', function(assert) {
  211. assert.expect(3);
  212. var collection = new Backbone.Collection;
  213. collection.add([{id: 1, name: 'Moe'}, {id: 2, name: 'Curly'}, {id: 3, name: 'Larry'}]);
  214. collection.add({id: 1, name: 'Moses'});
  215. assert.equal(collection.first().get('name'), 'Moe');
  216. collection.add({id: 1, name: 'Moses'}, {merge: true});
  217. assert.equal(collection.first().get('name'), 'Moses');
  218. collection.add({id: 1, name: 'Tim'}, {merge: true, silent: true});
  219. assert.equal(collection.first().get('name'), 'Tim');
  220. });
  221. QUnit.test('add model to multiple collections', function(assert) {
  222. assert.expect(10);
  223. var counter = 0;
  224. var m = new Backbone.Model({id: 10, label: 'm'});
  225. m.on('add', function(model, collection) {
  226. counter++;
  227. assert.equal(m, model);
  228. if (counter > 1) {
  229. assert.equal(collection, col2);
  230. } else {
  231. assert.equal(collection, col1);
  232. }
  233. });
  234. var col1 = new Backbone.Collection([]);
  235. col1.on('add', function(model, collection) {
  236. assert.equal(m, model);
  237. assert.equal(col1, collection);
  238. });
  239. var col2 = new Backbone.Collection([]);
  240. col2.on('add', function(model, collection) {
  241. assert.equal(m, model);
  242. assert.equal(col2, collection);
  243. });
  244. col1.add(m);
  245. assert.equal(m.collection, col1);
  246. col2.add(m);
  247. assert.equal(m.collection, col1);
  248. });
  249. QUnit.test('add model with parse', function(assert) {
  250. assert.expect(1);
  251. var Model = Backbone.Model.extend({
  252. parse: function(obj) {
  253. obj.value += 1;
  254. return obj;
  255. }
  256. });
  257. var Col = Backbone.Collection.extend({model: Model});
  258. var collection = new Col;
  259. collection.add({value: 1}, {parse: true});
  260. assert.equal(collection.at(0).get('value'), 2);
  261. });
  262. QUnit.test('add with parse and merge', function(assert) {
  263. var collection = new Backbone.Collection();
  264. collection.parse = function(attrs) {
  265. return _.map(attrs, function(model) {
  266. if (model.model) return model.model;
  267. return model;
  268. });
  269. };
  270. collection.add({id: 1});
  271. collection.add({model: {id: 1, name: 'Alf'}}, {parse: true, merge: true});
  272. assert.equal(collection.first().get('name'), 'Alf');
  273. });
  274. QUnit.test('add model to collection with sort()-style comparator', function(assert) {
  275. assert.expect(3);
  276. var collection = new Backbone.Collection;
  277. collection.comparator = function(m1, m2) {
  278. return m1.get('name') < m2.get('name') ? -1 : 1;
  279. };
  280. var tom = new Backbone.Model({name: 'Tom'});
  281. var rob = new Backbone.Model({name: 'Rob'});
  282. var tim = new Backbone.Model({name: 'Tim'});
  283. collection.add(tom);
  284. collection.add(rob);
  285. collection.add(tim);
  286. assert.equal(collection.indexOf(rob), 0);
  287. assert.equal(collection.indexOf(tim), 1);
  288. assert.equal(collection.indexOf(tom), 2);
  289. });
  290. QUnit.test('comparator that depends on `this`', function(assert) {
  291. assert.expect(2);
  292. var collection = new Backbone.Collection;
  293. collection.negative = function(num) {
  294. return -num;
  295. };
  296. collection.comparator = function(model) {
  297. return this.negative(model.id);
  298. };
  299. collection.add([{id: 1}, {id: 2}, {id: 3}]);
  300. assert.deepEqual(collection.pluck('id'), [3, 2, 1]);
  301. collection.comparator = function(m1, m2) {
  302. return this.negative(m2.id) - this.negative(m1.id);
  303. };
  304. collection.sort();
  305. assert.deepEqual(collection.pluck('id'), [1, 2, 3]);
  306. });
  307. QUnit.test('remove', function(assert) {
  308. assert.expect(12);
  309. var removed = null;
  310. var result = null;
  311. col.on('remove', function(model, collection, options) {
  312. removed = model.get('label');
  313. assert.equal(options.index, 3);
  314. assert.equal(collection.get(model), undefined, '#3693: model cannot be fetched from collection');
  315. });
  316. result = col.remove(d);
  317. assert.equal(removed, 'd');
  318. assert.strictEqual(result, d);
  319. //if we try to remove d again, it's not going to actually get removed
  320. result = col.remove(d);
  321. assert.strictEqual(result, undefined);
  322. assert.equal(col.length, 3);
  323. assert.equal(col.first(), a);
  324. col.off();
  325. result = col.remove([c, d]);
  326. assert.equal(result.length, 1, 'only returns removed models');
  327. assert.equal(result[0], c, 'only returns removed models');
  328. result = col.remove([c, b]);
  329. assert.equal(result.length, 1, 'only returns removed models');
  330. assert.equal(result[0], b, 'only returns removed models');
  331. result = col.remove([]);
  332. assert.deepEqual(result, [], 'returns empty array when nothing removed');
  333. });
  334. QUnit.test('add and remove return values', function(assert) {
  335. assert.expect(13);
  336. var Even = Backbone.Model.extend({
  337. validate: function(attrs) {
  338. if (attrs.id % 2 !== 0) return 'odd';
  339. }
  340. });
  341. var collection = new Backbone.Collection;
  342. collection.model = Even;
  343. var list = collection.add([{id: 2}, {id: 4}], {validate: true});
  344. assert.equal(list.length, 2);
  345. assert.ok(list[0] instanceof Backbone.Model);
  346. assert.equal(list[1], collection.last());
  347. assert.equal(list[1].get('id'), 4);
  348. list = collection.add([{id: 3}, {id: 6}], {validate: true});
  349. assert.equal(collection.length, 3);
  350. assert.equal(list[0], false);
  351. assert.equal(list[1].get('id'), 6);
  352. var result = collection.add({id: 6});
  353. assert.equal(result.cid, list[1].cid);
  354. result = collection.remove({id: 6});
  355. assert.equal(collection.length, 2);
  356. assert.equal(result.id, 6);
  357. list = collection.remove([{id: 2}, {id: 8}]);
  358. assert.equal(collection.length, 1);
  359. assert.equal(list[0].get('id'), 2);
  360. assert.equal(list[1], null);
  361. });
  362. QUnit.test('shift and pop', function(assert) {
  363. assert.expect(2);
  364. var collection = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  365. assert.equal(collection.shift().get('a'), 'a');
  366. assert.equal(collection.pop().get('c'), 'c');
  367. });
  368. QUnit.test('slice', function(assert) {
  369. assert.expect(2);
  370. var collection = new Backbone.Collection([{a: 'a'}, {b: 'b'}, {c: 'c'}]);
  371. var array = collection.slice(1, 3);
  372. assert.equal(array.length, 2);
  373. assert.equal(array[0].get('b'), 'b');
  374. });
  375. QUnit.test('events are unbound on remove', function(assert) {
  376. assert.expect(3);
  377. var counter = 0;
  378. var dj = new Backbone.Model();
  379. var emcees = new Backbone.Collection([dj]);
  380. emcees.on('change', function(){ counter++; });
  381. dj.set({name: 'Kool'});
  382. assert.equal(counter, 1);
  383. emcees.reset([]);
  384. assert.equal(dj.collection, undefined);
  385. dj.set({name: 'Shadow'});
  386. assert.equal(counter, 1);
  387. });
  388. QUnit.test('remove in multiple collections', function(assert) {
  389. assert.expect(7);
  390. var modelData = {
  391. id: 5,
  392. title: 'Othello'
  393. };
  394. var passed = false;
  395. var m1 = new Backbone.Model(modelData);
  396. var m2 = new Backbone.Model(modelData);
  397. m2.on('remove', function() {
  398. passed = true;
  399. });
  400. var col1 = new Backbone.Collection([m1]);
  401. var col2 = new Backbone.Collection([m2]);
  402. assert.notEqual(m1, m2);
  403. assert.ok(col1.length === 1);
  404. assert.ok(col2.length === 1);
  405. col1.remove(m1);
  406. assert.equal(passed, false);
  407. assert.ok(col1.length === 0);
  408. col2.remove(m1);
  409. assert.ok(col2.length === 0);
  410. assert.equal(passed, true);
  411. });
  412. QUnit.test('remove same model in multiple collection', function(assert) {
  413. assert.expect(16);
  414. var counter = 0;
  415. var m = new Backbone.Model({id: 5, title: 'Othello'});
  416. m.on('remove', function(model, collection) {
  417. counter++;
  418. assert.equal(m, model);
  419. if (counter > 1) {
  420. assert.equal(collection, col1);
  421. } else {
  422. assert.equal(collection, col2);
  423. }
  424. });
  425. var col1 = new Backbone.Collection([m]);
  426. col1.on('remove', function(model, collection) {
  427. assert.equal(m, model);
  428. assert.equal(col1, collection);
  429. });
  430. var col2 = new Backbone.Collection([m]);
  431. col2.on('remove', function(model, collection) {
  432. assert.equal(m, model);
  433. assert.equal(col2, collection);
  434. });
  435. assert.equal(col1, m.collection);
  436. col2.remove(m);
  437. assert.ok(col2.length === 0);
  438. assert.ok(col1.length === 1);
  439. assert.equal(counter, 1);
  440. assert.equal(col1, m.collection);
  441. col1.remove(m);
  442. assert.equal(null, m.collection);
  443. assert.ok(col1.length === 0);
  444. assert.equal(counter, 2);
  445. });
  446. QUnit.test('model destroy removes from all collections', function(assert) {
  447. assert.expect(3);
  448. var m = new Backbone.Model({id: 5, title: 'Othello'});
  449. m.sync = function(method, model, options) { options.success(); };
  450. var col1 = new Backbone.Collection([m]);
  451. var col2 = new Backbone.Collection([m]);
  452. m.destroy();
  453. assert.ok(col1.length === 0);
  454. assert.ok(col2.length === 0);
  455. assert.equal(undefined, m.collection);
  456. });
  457. QUnit.test('Collection: non-persisted model destroy removes from all collections', function(assert) {
  458. assert.expect(3);
  459. var m = new Backbone.Model({title: 'Othello'});
  460. m.sync = function(method, model, options) { throw 'should not be called'; };
  461. var col1 = new Backbone.Collection([m]);
  462. var col2 = new Backbone.Collection([m]);
  463. m.destroy();
  464. assert.ok(col1.length === 0);
  465. assert.ok(col2.length === 0);
  466. assert.equal(undefined, m.collection);
  467. });
  468. QUnit.test('fetch', function(assert) {
  469. assert.expect(4);
  470. var collection = new Backbone.Collection;
  471. collection.url = '/test';
  472. collection.fetch();
  473. assert.equal(this.syncArgs.method, 'read');
  474. assert.equal(this.syncArgs.model, collection);
  475. assert.equal(this.syncArgs.options.parse, true);
  476. collection.fetch({parse: false});
  477. assert.equal(this.syncArgs.options.parse, false);
  478. });
  479. QUnit.test('fetch with an error response triggers an error event', function(assert) {
  480. assert.expect(1);
  481. var collection = new Backbone.Collection();
  482. collection.on('error', function() {
  483. assert.ok(true);
  484. });
  485. collection.sync = function(method, model, options) { options.error(); };
  486. collection.fetch();
  487. });
  488. QUnit.test('#3283 - fetch with an error response calls error with context', function(assert) {
  489. assert.expect(1);
  490. var collection = new Backbone.Collection();
  491. var obj = {};
  492. var options = {
  493. context: obj,
  494. error: function() {
  495. assert.equal(this, obj);
  496. }
  497. };
  498. collection.sync = function(method, model, opts) {
  499. opts.error.call(opts.context);
  500. };
  501. collection.fetch(options);
  502. });
  503. QUnit.test('ensure fetch only parses once', function(assert) {
  504. assert.expect(1);
  505. var collection = new Backbone.Collection;
  506. var counter = 0;
  507. collection.parse = function(models) {
  508. counter++;
  509. return models;
  510. };
  511. collection.url = '/test';
  512. collection.fetch();
  513. this.syncArgs.options.success([]);
  514. assert.equal(counter, 1);
  515. });
  516. QUnit.test('create', function(assert) {
  517. assert.expect(4);
  518. var collection = new Backbone.Collection;
  519. collection.url = '/test';
  520. var model = collection.create({label: 'f'}, {wait: true});
  521. assert.equal(this.syncArgs.method, 'create');
  522. assert.equal(this.syncArgs.model, model);
  523. assert.equal(model.get('label'), 'f');
  524. assert.equal(model.collection, collection);
  525. });
  526. QUnit.test('create with validate:true enforces validation', function(assert) {
  527. assert.expect(3);
  528. var ValidatingModel = Backbone.Model.extend({
  529. validate: function(attrs) {
  530. return 'fail';
  531. }
  532. });
  533. var ValidatingCollection = Backbone.Collection.extend({
  534. model: ValidatingModel
  535. });
  536. var collection = new ValidatingCollection();
  537. collection.on('invalid', function(coll, error, options) {
  538. assert.equal(error, 'fail');
  539. assert.equal(options.validationError, 'fail');
  540. });
  541. assert.equal(collection.create({foo: 'bar'}, {validate: true}), false);
  542. });
  543. QUnit.test('create will pass extra options to success callback', function(assert) {
  544. assert.expect(1);
  545. var Model = Backbone.Model.extend({
  546. sync: function(method, model, options) {
  547. _.extend(options, {specialSync: true});
  548. return Backbone.Model.prototype.sync.call(this, method, model, options);
  549. }
  550. });
  551. var Collection = Backbone.Collection.extend({
  552. model: Model,
  553. url: '/test'
  554. });
  555. var collection = new Collection;
  556. var success = function(model, response, options) {
  557. assert.ok(options.specialSync, 'Options were passed correctly to callback');
  558. };
  559. collection.create({}, {success: success});
  560. this.ajaxSettings.success();
  561. });
  562. QUnit.test('create with wait:true should not call collection.parse', function(assert) {
  563. assert.expect(0);
  564. var Collection = Backbone.Collection.extend({
  565. url: '/test',
  566. parse: function() {
  567. assert.ok(false);
  568. }
  569. });
  570. var collection = new Collection;
  571. collection.create({}, {wait: true});
  572. this.ajaxSettings.success();
  573. });
  574. QUnit.test('a failing create returns model with errors', function(assert) {
  575. var ValidatingModel = Backbone.Model.extend({
  576. validate: function(attrs) {
  577. return 'fail';
  578. }
  579. });
  580. var ValidatingCollection = Backbone.Collection.extend({
  581. model: ValidatingModel
  582. });
  583. var collection = new ValidatingCollection();
  584. var m = collection.create({foo: 'bar'});
  585. assert.equal(m.validationError, 'fail');
  586. assert.equal(collection.length, 1);
  587. });
  588. QUnit.test('initialize', function(assert) {
  589. assert.expect(1);
  590. var Collection = Backbone.Collection.extend({
  591. initialize: function() {
  592. this.one = 1;
  593. }
  594. });
  595. var coll = new Collection;
  596. assert.equal(coll.one, 1);
  597. });
  598. QUnit.test('preinitialize', function(assert) {
  599. assert.expect(1);
  600. var Collection = Backbone.Collection.extend({
  601. preinitialize: function() {
  602. this.one = 1;
  603. }
  604. });
  605. var coll = new Collection;
  606. assert.equal(coll.one, 1);
  607. });
  608. QUnit.test('preinitialize occurs before the collection is set up', function(assert) {
  609. assert.expect(2);
  610. var Collection = Backbone.Collection.extend({
  611. preinitialize: function() {
  612. assert.notEqual(this.model, FooModel);
  613. }
  614. });
  615. var FooModel = Backbone.Model.extend({id: 'foo'});
  616. var coll = new Collection({}, {
  617. model: FooModel
  618. });
  619. assert.equal(coll.model, FooModel);
  620. });
  621. QUnit.test('toJSON', function(assert) {
  622. assert.expect(1);
  623. assert.equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
  624. });
  625. QUnit.test('where and findWhere', function(assert) {
  626. assert.expect(8);
  627. var model = new Backbone.Model({a: 1});
  628. var coll = new Backbone.Collection([
  629. model,
  630. {a: 1},
  631. {a: 1, b: 2},
  632. {a: 2, b: 2},
  633. {a: 3}
  634. ]);
  635. assert.equal(coll.where({a: 1}).length, 3);
  636. assert.equal(coll.where({a: 2}).length, 1);
  637. assert.equal(coll.where({a: 3}).length, 1);
  638. assert.equal(coll.where({b: 1}).length, 0);
  639. assert.equal(coll.where({b: 2}).length, 2);
  640. assert.equal(coll.where({a: 1, b: 2}).length, 1);
  641. assert.equal(coll.findWhere({a: 1}), model);
  642. assert.equal(coll.findWhere({a: 4}), void 0);
  643. });
  644. QUnit.test('Underscore methods', function(assert) {
  645. assert.expect(21);
  646. assert.equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
  647. assert.equal(col.some(function(model){ return model.id === 100; }), false);
  648. assert.equal(col.some(function(model){ return model.id === 0; }), true);
  649. assert.equal(col.reduce(function(m1, m2) {return m1.id > m2.id ? m1 : m2;}).id, 3);
  650. assert.equal(col.reduceRight(function(m1, m2) {return m1.id > m2.id ? m1 : m2;}).id, 3);
  651. assert.equal(col.indexOf(b), 1);
  652. assert.equal(col.size(), 4);
  653. assert.equal(col.rest().length, 3);
  654. assert.ok(!_.includes(col.rest(), a));
  655. assert.ok(_.includes(col.rest(), d));
  656. assert.ok(!col.isEmpty());
  657. assert.ok(!_.includes(col.without(d), d));
  658. var wrapped = col.chain();
  659. assert.equal(wrapped.map('id').max().value(), 3);
  660. assert.equal(wrapped.map('id').min().value(), 0);
  661. assert.deepEqual(wrapped
  662. .filter(function(o){ return o.id % 2 === 0; })
  663. .map(function(o){ return o.id * 2; })
  664. .value(),
  665. [4, 0]);
  666. assert.deepEqual(col.difference([c, d]), [a, b]);
  667. assert.ok(col.includes(col.sample()));
  668. var first = col.first();
  669. assert.deepEqual(col.groupBy(function(model){ return model.id; })[first.id], [first]);
  670. assert.deepEqual(col.countBy(function(model){ return model.id; }), {0: 1, 1: 1, 2: 1, 3: 1});
  671. assert.deepEqual(col.sortBy(function(model){ return model.id; })[0], col.at(3));
  672. assert.ok(col.indexBy('id')[first.id] === first);
  673. });
  674. QUnit.test('Underscore methods with object-style and property-style iteratee', function(assert) {
  675. assert.expect(26);
  676. var model = new Backbone.Model({a: 4, b: 1, e: 3});
  677. var coll = new Backbone.Collection([
  678. {a: 1, b: 1},
  679. {a: 2, b: 1, c: 1},
  680. {a: 3, b: 1},
  681. model
  682. ]);
  683. assert.equal(coll.find({a: 0}), undefined);
  684. assert.deepEqual(coll.find({a: 4}), model);
  685. assert.equal(coll.find('d'), undefined);
  686. assert.deepEqual(coll.find('e'), model);
  687. assert.equal(coll.filter({a: 0}), false);
  688. assert.deepEqual(coll.filter({a: 4}), [model]);
  689. assert.equal(coll.some({a: 0}), false);
  690. assert.equal(coll.some({a: 1}), true);
  691. assert.equal(coll.reject({a: 0}).length, 4);
  692. assert.deepEqual(coll.reject({a: 4}), _.without(coll.models, model));
  693. assert.equal(coll.every({a: 0}), false);
  694. assert.equal(coll.every({b: 1}), true);
  695. assert.deepEqual(coll.partition({a: 0})[0], []);
  696. assert.deepEqual(coll.partition({a: 0})[1], coll.models);
  697. assert.deepEqual(coll.partition({a: 4})[0], [model]);
  698. assert.deepEqual(coll.partition({a: 4})[1], _.without(coll.models, model));
  699. assert.deepEqual(coll.map({a: 2}), [false, true, false, false]);
  700. assert.deepEqual(coll.map('a'), [1, 2, 3, 4]);
  701. assert.deepEqual(coll.sortBy('a')[3], model);
  702. assert.deepEqual(coll.sortBy('e')[0], model);
  703. assert.deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1});
  704. assert.deepEqual(coll.countBy('d'), {'undefined': 4});
  705. assert.equal(coll.findIndex({b: 1}), 0);
  706. assert.equal(coll.findIndex({b: 9}), -1);
  707. assert.equal(coll.findLastIndex({b: 1}), 3);
  708. assert.equal(coll.findLastIndex({b: 9}), -1);
  709. });
  710. QUnit.test('reset', function(assert) {
  711. assert.expect(16);
  712. var resetCount = 0;
  713. var models = col.models;
  714. col.on('reset', function() { resetCount += 1; });
  715. col.reset([]);
  716. assert.equal(resetCount, 1);
  717. assert.equal(col.length, 0);
  718. assert.equal(col.last(), null);
  719. col.reset(models);
  720. assert.equal(resetCount, 2);
  721. assert.equal(col.length, 4);
  722. assert.equal(col.last(), d);
  723. col.reset(_.map(models, function(m){ return m.attributes; }));
  724. assert.equal(resetCount, 3);
  725. assert.equal(col.length, 4);
  726. assert.ok(col.last() !== d);
  727. assert.ok(_.isEqual(col.last().attributes, d.attributes));
  728. col.reset();
  729. assert.equal(col.length, 0);
  730. assert.equal(resetCount, 4);
  731. var f = new Backbone.Model({id: 20, label: 'f'});
  732. col.reset([undefined, f]);
  733. assert.equal(col.length, 2);
  734. assert.equal(resetCount, 5);
  735. col.reset(new Array(4));
  736. assert.equal(col.length, 4);
  737. assert.equal(resetCount, 6);
  738. });
  739. QUnit.test('reset with different values', function(assert) {
  740. var collection = new Backbone.Collection({id: 1});
  741. collection.reset({id: 1, a: 1});
  742. assert.equal(collection.get(1).get('a'), 1);
  743. });
  744. QUnit.test('same references in reset', function(assert) {
  745. var model = new Backbone.Model({id: 1});
  746. var collection = new Backbone.Collection({id: 1});
  747. collection.reset(model);
  748. assert.equal(collection.get(1), model);
  749. });
  750. QUnit.test('reset passes caller options', function(assert) {
  751. assert.expect(3);
  752. var Model = Backbone.Model.extend({
  753. initialize: function(attrs, options) {
  754. this.modelParameter = options.modelParameter;
  755. }
  756. });
  757. var collection = new (Backbone.Collection.extend({model: Model}))();
  758. collection.reset([{astring: 'green', anumber: 1}, {astring: 'blue', anumber: 2}], {modelParameter: 'model parameter'});
  759. assert.equal(collection.length, 2);
  760. collection.each(function(model) {
  761. assert.equal(model.modelParameter, 'model parameter');
  762. });
  763. });
  764. QUnit.test('reset does not alter options by reference', function(assert) {
  765. assert.expect(2);
  766. var collection = new Backbone.Collection([{id: 1}]);
  767. var origOpts = {};
  768. collection.on('reset', function(coll, opts){
  769. assert.equal(origOpts.previousModels, undefined);
  770. assert.equal(opts.previousModels[0].id, 1);
  771. });
  772. collection.reset([], origOpts);
  773. });
  774. QUnit.test('trigger custom events on models', function(assert) {
  775. assert.expect(1);
  776. var fired = null;
  777. a.on('custom', function() { fired = true; });
  778. a.trigger('custom');
  779. assert.equal(fired, true);
  780. });
  781. QUnit.test('add does not alter arguments', function(assert) {
  782. assert.expect(2);
  783. var attrs = {};
  784. var models = [attrs];
  785. new Backbone.Collection().add(models);
  786. assert.equal(models.length, 1);
  787. assert.ok(attrs === models[0]);
  788. });
  789. QUnit.test('#714: access `model.collection` in a brand new model.', function(assert) {
  790. assert.expect(2);
  791. var collection = new Backbone.Collection;
  792. collection.url = '/test';
  793. var Model = Backbone.Model.extend({
  794. set: function(attrs) {
  795. assert.equal(attrs.prop, 'value');
  796. assert.equal(this.collection, collection);
  797. return this;
  798. }
  799. });
  800. collection.model = Model;
  801. collection.create({prop: 'value'});
  802. });
  803. QUnit.test('#574, remove its own reference to the .models array.', function(assert) {
  804. assert.expect(2);
  805. var collection = new Backbone.Collection([
  806. {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
  807. ]);
  808. assert.equal(collection.length, 6);
  809. collection.remove(collection.models);
  810. assert.equal(collection.length, 0);
  811. });
  812. QUnit.test('#861, adding models to a collection which do not pass validation, with validate:true', function(assert) {
  813. assert.expect(2);
  814. var Model = Backbone.Model.extend({
  815. validate: function(attrs) {
  816. if (attrs.id === 3) return "id can't be 3";
  817. }
  818. });
  819. var Collection = Backbone.Collection.extend({
  820. model: Model
  821. });
  822. var collection = new Collection;
  823. collection.on('invalid', function() { assert.ok(true); });
  824. collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {validate: true});
  825. assert.deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
  826. });
  827. QUnit.test('Invalid models are discarded with validate:true.', function(assert) {
  828. assert.expect(5);
  829. var collection = new Backbone.Collection;
  830. collection.on('test', function() { assert.ok(true); });
  831. collection.model = Backbone.Model.extend({
  832. validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
  833. });
  834. var model = new collection.model({id: 1, valid: true});
  835. collection.add([model, {id: 2}], {validate: true});
  836. model.trigger('test');
  837. assert.ok(collection.get(model.cid));
  838. assert.ok(collection.get(1));
  839. assert.ok(!collection.get(2));
  840. assert.equal(collection.length, 1);
  841. });
  842. QUnit.test('multiple copies of the same model', function(assert) {
  843. assert.expect(3);
  844. var collection = new Backbone.Collection();
  845. var model = new Backbone.Model();
  846. collection.add([model, model]);
  847. assert.equal(collection.length, 1);
  848. collection.add([{id: 1}, {id: 1}]);
  849. assert.equal(collection.length, 2);
  850. assert.equal(collection.last().id, 1);
  851. });
  852. QUnit.test('#964 - collection.get return inconsistent', function(assert) {
  853. assert.expect(2);
  854. var collection = new Backbone.Collection();
  855. assert.ok(collection.get(null) === undefined);
  856. assert.ok(collection.get() === undefined);
  857. });
  858. QUnit.test('#1112 - passing options.model sets collection.model', function(assert) {
  859. assert.expect(2);
  860. var Model = Backbone.Model.extend({});
  861. var collection = new Backbone.Collection([{id: 1}], {model: Model});
  862. assert.ok(collection.model === Model);
  863. assert.ok(collection.at(0) instanceof Model);
  864. });
  865. QUnit.test('null and undefined are invalid ids.', function(assert) {
  866. assert.expect(2);
  867. var model = new Backbone.Model({id: 1});
  868. var collection = new Backbone.Collection([model]);
  869. model.set({id: null});
  870. assert.ok(!collection.get('null'));
  871. model.set({id: 1});
  872. model.set({id: undefined});
  873. assert.ok(!collection.get('undefined'));
  874. });
  875. QUnit.test('falsy comparator', function(assert) {
  876. assert.expect(4);
  877. var Col = Backbone.Collection.extend({
  878. comparator: function(model){ return model.id; }
  879. });
  880. var collection = new Col();
  881. var colFalse = new Col(null, {comparator: false});
  882. var colNull = new Col(null, {comparator: null});
  883. var colUndefined = new Col(null, {comparator: undefined});
  884. assert.ok(collection.comparator);
  885. assert.ok(!colFalse.comparator);
  886. assert.ok(!colNull.comparator);
  887. assert.ok(colUndefined.comparator);
  888. });
  889. QUnit.test('#1355 - `options` is passed to success callbacks', function(assert) {
  890. assert.expect(2);
  891. var m = new Backbone.Model({x: 1});
  892. var collection = new Backbone.Collection();
  893. var opts = {
  894. opts: true,
  895. success: function(coll, resp, options) {
  896. assert.ok(options.opts);
  897. }
  898. };
  899. collection.sync = m.sync = function( method, coll, options ){
  900. options.success({});
  901. };
  902. collection.fetch(opts);
  903. collection.create(m, opts);
  904. });
  905. QUnit.test("#1412 - Trigger 'request' and 'sync' events.", function(assert) {
  906. assert.expect(4);
  907. var collection = new Backbone.Collection;
  908. collection.url = '/test';
  909. Backbone.ajax = function(settings){ settings.success(); };
  910. collection.on('request', function(obj, xhr, options) {
  911. assert.ok(obj === collection, "collection has correct 'request' event after fetching");
  912. });
  913. collection.on('sync', function(obj, response, options) {
  914. assert.ok(obj === collection, "collection has correct 'sync' event after fetching");
  915. });
  916. collection.fetch();
  917. collection.off();
  918. collection.on('request', function(obj, xhr, options) {
  919. assert.ok(obj === collection.get(1), "collection has correct 'request' event after one of its models save");
  920. });
  921. collection.on('sync', function(obj, response, options) {
  922. assert.ok(obj === collection.get(1), "collection has correct 'sync' event after one of its models save");
  923. });
  924. collection.create({id: 1});
  925. collection.off();
  926. });
  927. QUnit.test('#3283 - fetch, create calls success with context', function(assert) {
  928. assert.expect(2);
  929. var collection = new Backbone.Collection;
  930. collection.url = '/test';
  931. Backbone.ajax = function(settings) {
  932. settings.success.call(settings.context);
  933. };
  934. var obj = {};
  935. var options = {
  936. context: obj,
  937. success: function() {
  938. assert.equal(this, obj);
  939. }
  940. };
  941. collection.fetch(options);
  942. collection.create({id: 1}, options);
  943. });
  944. QUnit.test('#1447 - create with wait adds model.', function(assert) {
  945. assert.expect(1);
  946. var collection = new Backbone.Collection;
  947. var model = new Backbone.Model;
  948. model.sync = function(method, m, options){ options.success(); };
  949. collection.on('add', function(){ assert.ok(true); });
  950. collection.create(model, {wait: true});
  951. });
  952. QUnit.test('#1448 - add sorts collection after merge.', function(assert) {
  953. assert.expect(1);
  954. var collection = new Backbone.Collection([
  955. {id: 1, x: 1},
  956. {id: 2, x: 2}
  957. ]);
  958. collection.comparator = function(model){ return model.get('x'); };
  959. collection.add({id: 1, x: 3}, {merge: true});
  960. assert.deepEqual(collection.pluck('id'), [2, 1]);
  961. });
  962. QUnit.test('#1655 - groupBy can be used with a string argument.', function(assert) {
  963. assert.expect(3);
  964. var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
  965. var grouped = collection.groupBy('x');
  966. assert.strictEqual(_.keys(grouped).length, 2);
  967. assert.strictEqual(grouped[1][0].get('x'), 1);
  968. assert.strictEqual(grouped[2][0].get('x'), 2);
  969. });
  970. QUnit.test('#1655 - sortBy can be used with a string argument.', function(assert) {
  971. assert.expect(1);
  972. var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
  973. var values = _.map(collection.sortBy('x'), function(model) {
  974. return model.get('x');
  975. });
  976. assert.deepEqual(values, [1, 2, 3]);
  977. });
  978. QUnit.test('#1604 - Removal during iteration.', function(assert) {
  979. assert.expect(0);
  980. var collection = new Backbone.Collection([{}, {}]);
  981. collection.on('add', function() {
  982. collection.at(0).destroy();
  983. });
  984. collection.add({}, {at: 0});
  985. });
  986. QUnit.test('#1638 - `sort` during `add` triggers correctly.', function(assert) {
  987. var collection = new Backbone.Collection;
  988. collection.comparator = function(model) { return model.get('x'); };
  989. var added = [];
  990. collection.on('add', function(model) {
  991. model.set({x: 3});
  992. collection.sort();
  993. added.push(model.id);
  994. });
  995. collection.add([{id: 1, x: 1}, {id: 2, x: 2}]);
  996. assert.deepEqual(added, [1, 2]);
  997. });
  998. QUnit.test('fetch parses models by default', function(assert) {
  999. assert.expect(1);
  1000. var model = {};
  1001. var Collection = Backbone.Collection.extend({
  1002. url: 'test',
  1003. model: Backbone.Model.extend({
  1004. parse: function(resp) {
  1005. assert.strictEqual(resp, model);
  1006. }
  1007. })
  1008. });
  1009. new Collection().fetch();
  1010. this.ajaxSettings.success([model]);
  1011. });
  1012. QUnit.test("`sort` shouldn't always fire on `add`", function(assert) {
  1013. assert.expect(1);
  1014. var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
  1015. comparator: 'id'
  1016. });
  1017. collection.sort = function(){ assert.ok(true); };
  1018. collection.add([]);
  1019. collection.add({id: 1});
  1020. collection.add([{id: 2}, {id: 3}]);
  1021. collection.add({id: 4});
  1022. });
  1023. QUnit.test('#1407 parse option on constructor parses collection and models', function(assert) {
  1024. assert.expect(2);
  1025. var model = {
  1026. namespace: [{id: 1}, {id: 2}]
  1027. };
  1028. var Collection = Backbone.Collection.extend({
  1029. model: Backbone.Model.extend({
  1030. parse: function(m) {
  1031. m.name = 'test';
  1032. return m;
  1033. }
  1034. }),
  1035. parse: function(m) {
  1036. return m.namespace;
  1037. }
  1038. });
  1039. var collection = new Collection(model, {parse: true});
  1040. assert.equal(collection.length, 2);
  1041. assert.equal(collection.at(0).get('name'), 'test');
  1042. });
  1043. QUnit.test('#1407 parse option on reset parses collection and models', function(assert) {
  1044. assert.expect(2);
  1045. var model = {
  1046. namespace: [{id: 1}, {id: 2}]
  1047. };
  1048. var Collection = Backbone.Collection.extend({
  1049. model: Backbone.Model.extend({
  1050. parse: function(m) {
  1051. m.name = 'test';
  1052. return m;
  1053. }
  1054. }),
  1055. parse: function(m) {
  1056. return m.namespace;
  1057. }
  1058. });
  1059. var collection = new Collection();
  1060. collection.reset(model, {parse: true});
  1061. assert.equal(collection.length, 2);
  1062. assert.equal(collection.at(0).get('name'), 'test');
  1063. });
  1064. QUnit.test('Reset includes previous models in triggered event.', function(assert) {
  1065. assert.expect(1);
  1066. var model = new Backbone.Model();
  1067. var collection = new Backbone.Collection([model]);
  1068. collection.on('reset', function(coll, options) {
  1069. assert.deepEqual(options.previousModels, [model]);
  1070. });
  1071. collection.reset([]);
  1072. });
  1073. QUnit.test('set', function(assert) {
  1074. var m1 = new Backbone.Model();
  1075. var m2 = new Backbone.Model({id: 2});
  1076. var m3 = new Backbone.Model();
  1077. var collection = new Backbone.Collection([m1, m2]);
  1078. // Test add/change/remove events
  1079. collection.on('add', function(model) {
  1080. assert.strictEqual(model, m3);
  1081. });
  1082. collection.on('change', function(model) {
  1083. assert.strictEqual(model, m2);
  1084. });
  1085. collection.on('remove', function(model) {
  1086. assert.strictEqual(model, m1);
  1087. });
  1088. // remove: false doesn't remove any models
  1089. collection.set([], {remove: false});
  1090. assert.strictEqual(collection.length, 2);
  1091. // add: false doesn't add any models
  1092. collection.set([m1, m2, m3], {add: false});
  1093. assert.strictEqual(collection.length, 2);
  1094. // merge: false doesn't change any models
  1095. collection.set([m1, {id: 2, a: 1}], {merge: false});
  1096. assert.strictEqual(m2.get('a'), void 0);
  1097. // add: false, remove: false only merges existing models
  1098. collection.set([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false});
  1099. assert.strictEqual(collection.length, 2);
  1100. assert.strictEqual(m2.get('a'), 0);
  1101. // default options add/remove/merge as appropriate
  1102. collection.set([{id: 2, a: 1}, m3]);
  1103. assert.strictEqual(collection.length, 2);
  1104. assert.strictEqual(m2.get('a'), 1);
  1105. // Test removing models not passing an argument
  1106. collection.off('remove').on('remove', function(model) {
  1107. assert.ok(model === m2 || model === m3);
  1108. });
  1109. collection.set([]);
  1110. assert.strictEqual(collection.length, 0);
  1111. // Test null models on set doesn't clear collection
  1112. collection.off();
  1113. collection.set([{id: 1}]);
  1114. collection.set();
  1115. assert.strictEqual(collection.length, 1);
  1116. });
  1117. QUnit.test('set with only cids', function(assert) {
  1118. assert.expect(3);
  1119. var m1 = new Backbone.Model;
  1120. var m2 = new Backbone.Model;
  1121. var collection = new Backbone.Collection;
  1122. collection.set([m1, m2]);
  1123. assert.equal(collection.length, 2);
  1124. collection.set([m1]);
  1125. assert.equal(collection.length, 1);
  1126. collection.set([m1, m1, m1, m2, m2], {remove: false});
  1127. assert.equal(collection.length, 2);
  1128. });
  1129. QUnit.test('set with only idAttribute', function(assert) {
  1130. assert.expect(3);
  1131. var m1 = {_id: 1};
  1132. var m2 = {_id: 2};
  1133. var Col = Backbone.Collection.extend({
  1134. model: Backbone.Model.extend({
  1135. idAttribute: '_id'
  1136. })
  1137. });
  1138. var collection = new Col;
  1139. collection.set([m1, m2]);
  1140. assert.equal(collection.length, 2);
  1141. collection.set([m1]);
  1142. assert.equal(collection.length, 1);
  1143. collection.set([m1, m1, m1, m2, m2], {remove: false});
  1144. assert.equal(collection.length, 2);
  1145. });
  1146. QUnit.test('set + merge with default values defined', function(assert) {
  1147. var Model = Backbone.Model.extend({
  1148. defaults: {
  1149. key: 'value'
  1150. }
  1151. });
  1152. var m = new Model({id: 1});
  1153. var collection = new Backbone.Collection([m], {model: Model});
  1154. assert.equal(collection.first().get('key'), 'value');
  1155. collection.set({id: 1, key: 'other'});
  1156. assert.equal(collection.first().get('key'), 'other');
  1157. collection.set({id: 1, other: 'value'});
  1158. assert.equal(collection.first().get('key'), 'other');
  1159. assert.equal(collection.length, 1);
  1160. });
  1161. QUnit.test('merge without mutation', function(assert) {
  1162. var Model = Backbone.Model.extend({
  1163. initialize: function(attrs, options) {
  1164. if (attrs.child) {
  1165. this.set('child', new Model(attrs.child, options), options);
  1166. }
  1167. }
  1168. });
  1169. var Collection = Backbone.Collection.extend({model: Model});
  1170. var data = [{id: 1, child: {id: 2}}];
  1171. var collection = new Collection(data);
  1172. assert.equal(collection.first().id, 1);
  1173. collection.set(data);
  1174. assert.equal(collection.first().id, 1);
  1175. collection.set([{id: 2, child: {id: 2}}].concat(data));
  1176. assert.deepEqual(collection.pluck('id'), [2, 1]);
  1177. });
  1178. QUnit.test('`set` and model level `parse`', function(assert) {
  1179. var Model = Backbone.Model.extend({});
  1180. var Collection = Backbone.Collection.extend({
  1181. model: Model,
  1182. parse: function(res) { return _.map(res.models, 'model'); }
  1183. });
  1184. var model = new Model({id: 1});
  1185. var collection = new Collection(model);
  1186. collection.set({models: [
  1187. {model: {id: 1}},
  1188. {model: {id: 2}}
  1189. ]}, {parse: true});
  1190. assert.equal(collection.first(), model);
  1191. });
  1192. QUnit.test('`set` data is only parsed once', function(assert) {
  1193. var collection = new Backbone.Collection();
  1194. collection.model = Backbone.Model.extend({
  1195. parse: function(data) {
  1196. assert.equal(data.parsed, void 0);
  1197. data.parsed = true;
  1198. return data;
  1199. }
  1200. });
  1201. collection.set({}, {parse: true});
  1202. });
  1203. QUnit.test('`set` matches input order in the absence of a comparator', function(assert) {
  1204. var one = new Backbone.Model({id: 1});
  1205. var two = new Backbone.Model({id: 2});
  1206. var three = new Backbone.Model({id: 3});
  1207. var collection = new Backbone.Collection([one, two, three]);
  1208. collection.set([{id: 3}, {id: 2}, {id: 1}]);
  1209. assert.deepEqual(collection.models, [three, two, one]);
  1210. collection.set([{id: 1}, {id: 2}]);
  1211. assert.deepEqual(collection.models, [one, two]);
  1212. collection.set([two, three, one]);
  1213. assert.deepEqual(collection.models, [two, three, one]);
  1214. collection.set([{id: 1}, {id: 2}], {remove: false});
  1215. assert.deepEqual(collection.models, [two, three, one]);
  1216. collection.set([{id: 1}, {id: 2}, {id: 3}], {merge: false});
  1217. assert.deepEqual(collection.models, [one, two, three]);
  1218. collection.set([three, two, one, {id: 4}], {add: false});
  1219. assert.deepEqual(collection.models, [one, two, three]);
  1220. });
  1221. QUnit.test('#1894 - Push should not trigger a sort', function(assert) {
  1222. assert.expect(0);
  1223. var Collection = Backbone.Collection.extend({
  1224. comparator: 'id',
  1225. sort: function() { assert.ok(false); }
  1226. });
  1227. new Collection().push({id: 1});
  1228. });
  1229. QUnit.test('#2428 - push duplicate models, return the correct one', function(assert) {
  1230. assert.expect(1);
  1231. var collection = new Backbone.Collection;
  1232. var model1 = collection.push({id: 101});
  1233. var model2 = collection.push({id: 101});
  1234. assert.ok(model2.cid === model1.cid);
  1235. });
  1236. QUnit.test('`set` with non-normal id', function(assert) {
  1237. var Collection = Backbone.Collection.extend({
  1238. model: Backbone.Model.extend({idAttribute: '_id'})
  1239. });
  1240. var collection = new Collection({_id: 1});
  1241. collection.set([{_id: 1, a: 1}], {add: false});
  1242. assert.equal(collection.first().get('a'), 1);
  1243. });
  1244. QUnit.test('#1894 - `sort` can optionally be turned off', function(assert) {
  1245. assert.expect(0);
  1246. var Collection = Backbone.Collection.extend({
  1247. comparator: 'id',
  1248. sort: function() { assert.ok(false); }
  1249. });
  1250. new Collection().add({id: 1}, {sort: false});
  1251. });
  1252. QUnit.test('#1915 - `parse` data in the right order in `set`', function(assert) {
  1253. var collection = new (Backbone.Collection.extend({
  1254. parse: function(data) {
  1255. assert.strictEqual(data.status, 'ok');
  1256. return data.data;
  1257. }
  1258. }));
  1259. var res = {status: 'ok', data: [{id: 1}]};
  1260. collection.set(res, {parse: true});
  1261. });
  1262. QUnit.test('#1939 - `parse` is passed `options`', function(assert) {
  1263. var done = assert.async();
  1264. assert.expect(1);
  1265. var collection = new (Backbone.Collection.extend({
  1266. url: '/',
  1267. parse: function(data, options) {
  1268. assert.strictEqual(options.xhr.someHeader, 'headerValue');
  1269. return data;
  1270. }
  1271. }));
  1272. var ajax = Backbone.ajax;
  1273. Backbone.ajax = function(params) {
  1274. _.defer(params.success, []);
  1275. return {someHeader: 'headerValue'};
  1276. };
  1277. collection.fetch({
  1278. success: function() { done(); }
  1279. });
  1280. Backbone.ajax = ajax;
  1281. });
  1282. QUnit.test('fetch will pass extra options to success callback', function(assert) {
  1283. assert.expect(1);
  1284. var SpecialSyncCollection = Backbone.Collection.extend({
  1285. url: '/test',
  1286. sync: function(method, collection, options) {
  1287. _.extend(options, {specialSync: true});
  1288. return Backbone.Collection.prototype.sync.call(this, method, collection, options);
  1289. }
  1290. });
  1291. var collection = new SpecialSyncCollection();
  1292. var onSuccess = function(coll, resp, options) {
  1293. assert.ok(options.specialSync, 'Options were passed correctly to callback');
  1294. };
  1295. collection.fetch({success: onSuccess});
  1296. this.ajaxSettings.success();
  1297. });
  1298. QUnit.test('`add` only `sort`s when necessary', function(assert) {
  1299. assert.expect(2);
  1300. var collection = new (Backbone.Collection.extend({
  1301. comparator: 'a'
  1302. }))([{id: 1}, {id: 2}, {id: 3}]);
  1303. collection.on('sort', function() { assert.ok(true); });
  1304. collection.add({id: 4}); // do sort, new model
  1305. collection.add({id: 1, a: 1}, {merge: true}); // do sort, comparator change
  1306. collection.add({id: 1, b: 1}, {merge: true}); // don't sort, no comparator change
  1307. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no comparator change
  1308. collection.add(collection.models); // don't sort, nothing new
  1309. collection.add(collection.models, {merge: true}); // don't sort
  1310. });
  1311. QUnit.test('`add` only `sort`s when necessary with comparator function', function(assert) {
  1312. assert.expect(3);
  1313. var collection = new (Backbone.Collection.extend({
  1314. comparator: function(m1, m2) {
  1315. return m1.get('a') > m2.get('a') ? 1 : (m1.get('a') < m2.get('a') ? -1 : 0);
  1316. }
  1317. }))([{id: 1}, {id: 2}, {id: 3}]);
  1318. collection.on('sort', function() { assert.ok(true); });
  1319. collection.add({id: 4}); // do sort, new model
  1320. collection.add({id: 1, a: 1}, {merge: true}); // do sort, model change
  1321. collection.add({id: 1, b: 1}, {merge: true}); // do sort, model change
  1322. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no model change
  1323. collection.add(collection.models); // don't sort, nothing new
  1324. collection.add(collection.models, {merge: true}); // don't sort
  1325. });
  1326. QUnit.test('Attach options to collection.', function(assert) {
  1327. assert.expect(2);
  1328. var Model = Backbone.Model;
  1329. var comparator = function(){};
  1330. var collection = new Backbone.Collection([], {
  1331. model: Model,
  1332. comparator: comparator
  1333. });
  1334. assert.ok(collection.model === Model);
  1335. assert.ok(collection.comparator === comparator);
  1336. });
  1337. QUnit.test('Pass falsey for `models` for empty Col with `options`', function(assert) {
  1338. assert.expect(9);
  1339. var opts = {a: 1, b: 2};
  1340. _.forEach([undefined, null, false], function(falsey) {
  1341. var Collection = Backbone.Collection.extend({
  1342. initialize: function(models, options) {
  1343. assert.strictEqual(models, falsey);
  1344. assert.strictEqual(options, opts);
  1345. }
  1346. });
  1347. var collection = new Collection(falsey, opts);
  1348. assert.strictEqual(collection.length, 0);
  1349. });
  1350. });
  1351. QUnit.test('`add` overrides `set` flags', function(assert) {
  1352. var collection = new Backbone.Collection();
  1353. collection.once('add', function(model, coll, options) {
  1354. coll.add({id: 2}, options);
  1355. });
  1356. collection.set({id: 1});
  1357. assert.equal(collection.length, 2);
  1358. });
  1359. QUnit.test('#2606 - Collection#create, success arguments', function(assert) {
  1360. assert.expect(1);
  1361. var collection = new Backbone.Collection;
  1362. collection.url = 'test';
  1363. collection.create({}, {
  1364. success: function(model, resp, options) {
  1365. assert.strictEqual(resp, 'response');
  1366. }
  1367. });
  1368. this.ajaxSettings.success('response');
  1369. });
  1370. QUnit.test('#2612 - nested `parse` works with `Collection#set`', function(assert) {
  1371. var Job = Backbone.Model.extend({
  1372. constructor: function() {
  1373. this.items = new Items();
  1374. Backbone.Model.apply(this, arguments);
  1375. },
  1376. parse: function(attrs) {
  1377. this.items.set(attrs.items, {parse: true});
  1378. return _.omit(attrs, 'items');
  1379. }
  1380. });
  1381. var Item = Backbone.Model.extend({
  1382. constructor: function() {
  1383. this.subItems = new Backbone.Collection();
  1384. Backbone.Model.apply(this, arguments);
  1385. },
  1386. parse: function(attrs) {
  1387. this.subItems.set(attrs.subItems, {parse: true});
  1388. return _.omit(attrs, 'subItems');
  1389. }
  1390. });
  1391. var Items = Backbone.Collection.extend({
  1392. model: Item
  1393. });
  1394. var data = {
  1395. name: 'JobName',
  1396. id: 1,
  1397. items: [{
  1398. id: 1,
  1399. name: 'Sub1',
  1400. subItems: [
  1401. {id: 1, subName: 'One'},
  1402. {id: 2, subName: 'Two'}
  1403. ]
  1404. }, {
  1405. id: 2,
  1406. name: 'Sub2',
  1407. subItems: [
  1408. {id: 3, subName: 'Three'},
  1409. {id: 4, subName: 'Four'}
  1410. ]
  1411. }]
  1412. };
  1413. var newData = {
  1414. name: 'NewJobName',
  1415. id: 1,
  1416. items: [{
  1417. id: 1,
  1418. name: 'NewSub1',
  1419. subItems: [
  1420. {id: 1, subName: 'NewOne'},
  1421. {id: 2, subName: 'NewTwo'}
  1422. ]
  1423. }, {
  1424. id: 2,
  1425. name: 'NewSub2',
  1426. subItems: [
  1427. {id: 3, subName: 'NewThree'},
  1428. {id: 4, subName: 'NewFour'}
  1429. ]
  1430. }]
  1431. };
  1432. var job = new Job(data, {parse: true});
  1433. assert.equal(job.get('name'), 'JobName');
  1434. assert.equal(job.items.at(0).get('name'), 'Sub1');
  1435. assert.equal(job.items.length, 2);
  1436. assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'One');
  1437. assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'Three');
  1438. job.set(job.parse(newData, {parse: true}));
  1439. assert.equal(job.get('name'), 'NewJobName');
  1440. assert.equal(job.items.at(0).get('name'), 'NewSub1');
  1441. assert.equal(job.items.length, 2);
  1442. assert.equal(job.items.get(1).subItems.get(1).get('subName'), 'NewOne');
  1443. assert.equal(job.items.get(2).subItems.get(3).get('subName'), 'NewThree');
  1444. });
  1445. QUnit.test('_addReference binds all collection events & adds to the lookup hashes', function(assert) {
  1446. assert.expect(8);
  1447. var calls = {add: 0, remove: 0};
  1448. var Collection = Backbone.Collection.extend({
  1449. _addReference: function(model) {
  1450. Backbone.Collection.prototype._addReference.apply(this, arguments);
  1451. calls.add++;
  1452. assert.equal(model, this._byId[model.id]);
  1453. assert.equal(model, this._byId[model.cid]);
  1454. assert.equal(model._events.all.length, 1);
  1455. },
  1456. _removeReference: function(model) {
  1457. Backbone.Collection.prototype._removeReference.apply(this, arguments);
  1458. calls.remove++;
  1459. assert.equal(this._byId[model.id], void 0);
  1460. assert.equal(this._byId[model.cid], void 0);
  1461. assert.equal(model.collection, void 0);
  1462. }
  1463. });
  1464. var collection = new Collection();
  1465. var model = collection.add({id: 1});
  1466. collection.remove(model);
  1467. assert.equal(calls.add, 1);
  1468. assert.equal(calls.remove, 1);
  1469. });
  1470. QUnit.test('Do not allow duplicate models to be `add`ed or `set`', function(assert) {
  1471. var collection = new Backbone.Collection();
  1472. collection.add([{id: 1}, {id: 1}]);
  1473. assert.equal(collection.length, 1);
  1474. assert.equal(collection.models.length, 1);
  1475. collection.set([{id: 1}, {id: 1}]);
  1476. assert.equal(collection.length, 1);
  1477. assert.equal(collection.models.length, 1);
  1478. });
  1479. QUnit.test('#3020: #set with {add: false} should not throw.', function(assert) {
  1480. assert.expect(2);
  1481. var collection = new Backbone.Collection;
  1482. collection.set([{id: 1}], {add: false});
  1483. assert.strictEqual(collection.length, 0);
  1484. assert.strictEqual(collection.models.length, 0);
  1485. });
  1486. QUnit.test('create with wait, model instance, #3028', function(assert) {
  1487. assert.expect(1);
  1488. var collection = new Backbone.Collection();
  1489. var model = new Backbone.Model({id: 1});
  1490. model.sync = function(){
  1491. assert.equal(this.collection, collection);
  1492. };
  1493. collection.create(model, {wait: true});
  1494. });
  1495. QUnit.test('modelId', function(assert) {
  1496. var Stooge = Backbone.Model.extend();
  1497. var StoogeCollection = Backbone.Collection.extend({model: Stooge});
  1498. // Default to using `Collection::model::idAttribute`.
  1499. assert.equal(StoogeCollection.prototype.modelId({id: 1}), 1);
  1500. Stooge.prototype.idAttribute = '_id';
  1501. assert.equal(StoogeCollection.prototype.modelId({_id: 1}), 1);
  1502. });
  1503. QUnit.test('Polymorphic models work with "simple" constructors', function(assert) {
  1504. var A = Backbone.Model.extend();
  1505. var B = Backbone.Model.extend();
  1506. var C = Backbone.Collection.extend({
  1507. model: function(attrs) {
  1508. return attrs.type === 'a' ? new A(attrs) : new B(attrs);
  1509. }
  1510. });
  1511. var collection = new C([{id: 1, type: 'a'}, {id: 2, type: 'b'}]);
  1512. assert.equal(collection.length, 2);
  1513. assert.ok(collection.at(0) instanceof A);
  1514. assert.equal(collection.at(0).id, 1);
  1515. assert.ok(collection.at(1) instanceof B);
  1516. assert.equal(collection.at(1).id, 2);
  1517. });
  1518. QUnit.test('Polymorphic models work with "advanced" constructors', function(assert) {
  1519. var A = Backbone.Model.extend({idAttribute: '_id'});
  1520. var B = Backbone.Model.extend({idAttribute: '_id'});
  1521. var C = Backbone.Collection.extend({
  1522. model: Backbone.Model.extend({
  1523. constructor: function(attrs) {
  1524. return attrs.type === 'a' ? new A(attrs) : new B(attrs);
  1525. },
  1526. idAttribute: '_id'
  1527. })
  1528. });
  1529. var collection = new C([{_id: 1, type: 'a'}, {_id: 2, type: 'b'}]);
  1530. assert.equal(collection.length, 2);
  1531. assert.ok(collection.at(0) instanceof A);
  1532. assert.equal(collection.at(0), collection.get(1));
  1533. assert.ok(collection.at(1) instanceof B);
  1534. assert.equal(collection.at(1), collection.get(2));
  1535. C = Backbone.Collection.extend({
  1536. model: function(attrs) {
  1537. return attrs.type === 'a' ? new A(attrs) : new B(attrs);
  1538. },
  1539. modelId: function(attrs) {
  1540. return attrs.type + '-' + attrs.id;
  1541. }
  1542. });
  1543. collection = new C([{id: 1, type: 'a'}, {id: 1, type: 'b'}]);
  1544. assert.equal(collection.length, 2);
  1545. assert.ok(collection.at(0) instanceof A);
  1546. assert.equal(collection.at(0), collection.get('a-1'));
  1547. assert.ok(collection.at(1) instanceof B);
  1548. assert.equal(collection.at(1), collection.get('b-1'));
  1549. });
  1550. QUnit.test('Collection with polymorphic models receives default id from modelId', function(assert) {
  1551. assert.expect(6);
  1552. // When the polymorphic models use 'id' for the idAttribute, all is fine.
  1553. var C1 = Backbone.Collection.extend({
  1554. model: function(attrs) {
  1555. return new Backbone.Model(attrs);
  1556. }
  1557. });
  1558. var c1 = new C1({id: 1});
  1559. assert.equal(c1.get(1).id, 1);
  1560. assert.equal(c1.modelId({id: 1}), 1);
  1561. // If the polymorphic models define their own idAttribute,
  1562. // the modelId method should be overridden, for the reason below.
  1563. var M = Backbone.Model.extend({
  1564. idAttribute: '_id'
  1565. });
  1566. var C2 = Backbone.Collection.extend({
  1567. model: function(attrs) {
  1568. return new M(attrs);
  1569. }
  1570. });
  1571. var c2 = new C2({_id: 1});
  1572. assert.equal(c2.get(1), void 0);
  1573. assert.equal(c2.modelId(c2.at(0).attributes), void 0);
  1574. var m = new M({_id: 2});
  1575. c2.add(m);
  1576. assert.equal(c2.get(2), void 0);
  1577. assert.equal(c2.modelId(m.attributes), void 0);
  1578. });
  1579. QUnit.test('#3039 #3951: adding at index fires with correct at', function(assert) {
  1580. assert.expect(4);
  1581. var collection = new Backbone.Collection([{val: 0}, {val: 4}]);
  1582. collection.on('add', function(model, coll, options) {
  1583. assert.equal(model.get('val'), options.index);
  1584. });
  1585. collection.add([{val: 1}, {val: 2}, {val: 3}], {at: 1});
  1586. collection.add({val: 5}, {at: 10});
  1587. });
  1588. QUnit.test('#3039: index is not sent when at is not specified', function(assert) {
  1589. assert.expect(2);
  1590. var collection = new Backbone.Collection([{at: 0}]);
  1591. collection.on('add', function(model, coll, options) {
  1592. assert.equal(undefined, options.index);
  1593. });
  1594. collection.add([{at: 1}, {at: 2}]);
  1595. });
  1596. QUnit.test('#3199 - Order changing should trigger a sort', function(assert) {
  1597. assert.expect(1);
  1598. var one = new Backbone.Model({id: 1});
  1599. var two = new Backbone.Model({id: 2});
  1600. var three = new Backbone.Model({id: 3});
  1601. var collection = new Backbone.Collection([one, two, three]);
  1602. collection.on('sort', function() {
  1603. assert.ok(true);
  1604. });
  1605. collection.set([{id: 3}, {id: 2}, {id: 1}]);
  1606. });
  1607. QUnit.test('#3199 - Adding a model should trigger a sort', function(assert) {
  1608. assert.expect(1);
  1609. var one = new Backbone.Model({id: 1});
  1610. var two = new Backbone.Model({id: 2});
  1611. var three = new Backbone.Model({id: 3});
  1612. var collection = new Backbone.Collection([one, two, three]);
  1613. collection.on('sort', function() {
  1614. assert.ok(true);
  1615. });
  1616. collection.set([{id: 1}, {id: 2}, {id: 3}, {id: 0}]);
  1617. });
  1618. QUnit.test('#3199 - Order not changing should not trigger a sort', function(assert) {
  1619. assert.expect(0);
  1620. var one = new Backbone.Model({id: 1});
  1621. var two = new Backbone.Model({id: 2});
  1622. var three = new Backbone.Model({id: 3});
  1623. var collection = new Backbone.Collection([one, two, three]);
  1624. collection.on('sort', function() {
  1625. assert.ok(false);
  1626. });
  1627. collection.set([{id: 1}, {id: 2}, {id: 3}]);
  1628. });
  1629. QUnit.test('add supports negative indexes', function(assert) {
  1630. assert.expect(1);
  1631. var collection = new Backbone.Collection([{id: 1}]);
  1632. collection.add([{id: 2}, {id: 3}], {at: -1});
  1633. collection.add([{id: 2.5}], {at: -2});
  1634. collection.add([{id: 0.5}], {at: -6});
  1635. assert.equal(collection.pluck('id').join(','), '0.5,1,2,2.5,3');
  1636. });
  1637. QUnit.test('#set accepts options.at as a string', function(assert) {
  1638. assert.expect(1);
  1639. var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
  1640. collection.add([{id: 3}], {at: '1'});
  1641. assert.deepEqual(collection.pluck('id'), [1, 3, 2]);
  1642. });
  1643. QUnit.test('adding multiple models triggers `update` event once', function(assert) {
  1644. assert.expect(1);
  1645. var collection = new Backbone.Collection;
  1646. collection.on('update', function() { assert.ok(true); });
  1647. collection.add([{id: 1}, {id: 2}, {id: 3}]);
  1648. });
  1649. QUnit.test('removing models triggers `update` event once', function(assert) {
  1650. assert.expect(1);
  1651. var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}]);
  1652. collection.on('update', function() { assert.ok(true); });
  1653. collection.remove([{id: 1}, {id: 2}]);
  1654. });
  1655. QUnit.test('remove does not trigger `update` when nothing removed', function(assert) {
  1656. assert.expect(0);
  1657. var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
  1658. collection.on('update', function() { assert.ok(false); });
  1659. collection.remove([{id: 3}]);
  1660. });
  1661. QUnit.test('set triggers `set` event once', function(assert) {
  1662. assert.expect(1);
  1663. var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
  1664. collection.on('update', function() { assert.ok(true); });
  1665. collection.set([{id: 1}, {id: 3}]);
  1666. });
  1667. QUnit.test('set does not trigger `update` event when nothing added nor removed', function(assert) {
  1668. var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
  1669. collection.on('update', function(coll, options) {
  1670. assert.equal(options.changes.added.length, 0);
  1671. assert.equal(options.changes.removed.length, 0);
  1672. assert.equal(options.changes.merged.length, 2);
  1673. });
  1674. collection.set([{id: 1}, {id: 2}]);
  1675. });
  1676. QUnit.test('#3610 - invoke collects arguments', function(assert) {
  1677. assert.expect(3);
  1678. var Model = Backbone.Model.extend({
  1679. method: function(x, y, z) {
  1680. assert.equal(x, 1);
  1681. assert.equal(y, 2);
  1682. assert.equal(z, 3);
  1683. }
  1684. });
  1685. var Collection = Backbone.Collection.extend({
  1686. model: Model
  1687. });
  1688. var collection = new Collection([{id: 1}]);
  1689. collection.invoke('method', 1, 2, 3);
  1690. });
  1691. QUnit.test('#3662 - triggering change without model will not error', function(assert) {
  1692. assert.expect(1);
  1693. var collection = new Backbone.Collection([{id: 1}]);
  1694. var model = collection.first();
  1695. collection.on('change', function(m) {
  1696. assert.equal(m, undefined);
  1697. });
  1698. model.trigger('change');
  1699. });
  1700. QUnit.test('#3871 - falsy parse result creates empty collection', function(assert) {
  1701. var collection = new (Backbone.Collection.extend({
  1702. parse: function(data, options) {}
  1703. }));
  1704. collection.set('', {parse: true});
  1705. assert.equal(collection.length, 0);
  1706. });
  1707. QUnit.test("#3711 - remove's `update` event returns one removed model", function(assert) {
  1708. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1709. var collection = new Backbone.Collection([model]);
  1710. collection.on('update', function(context, options) {
  1711. var changed = options.changes;
  1712. assert.deepEqual(changed.added, []);
  1713. assert.deepEqual(changed.merged, []);
  1714. assert.strictEqual(changed.removed[0], model);
  1715. });
  1716. collection.remove(model);
  1717. });
  1718. QUnit.test("#3711 - remove's `update` event returns multiple removed models", function(assert) {
  1719. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1720. var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
  1721. var collection = new Backbone.Collection([model, model2]);
  1722. collection.on('update', function(context, options) {
  1723. var changed = options.changes;
  1724. assert.deepEqual(changed.added, []);
  1725. assert.deepEqual(changed.merged, []);
  1726. assert.ok(changed.removed.length === 2);
  1727. assert.ok(_.indexOf(changed.removed, model) > -1 && _.indexOf(changed.removed, model2) > -1);
  1728. });
  1729. collection.remove([model, model2]);
  1730. });
  1731. QUnit.test("#3711 - set's `update` event returns one added model", function(assert) {
  1732. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1733. var collection = new Backbone.Collection();
  1734. collection.on('update', function(context, options) {
  1735. var addedModels = options.changes.added;
  1736. assert.ok(addedModels.length === 1);
  1737. assert.strictEqual(addedModels[0], model);
  1738. });
  1739. collection.set(model);
  1740. });
  1741. QUnit.test("#3711 - set's `update` event returns multiple added models", function(assert) {
  1742. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1743. var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
  1744. var collection = new Backbone.Collection();
  1745. collection.on('update', function(context, options) {
  1746. var addedModels = options.changes.added;
  1747. assert.ok(addedModels.length === 2);
  1748. assert.strictEqual(addedModels[0], model);
  1749. assert.strictEqual(addedModels[1], model2);
  1750. });
  1751. collection.set([model, model2]);
  1752. });
  1753. QUnit.test("#3711 - set's `update` event returns one removed model", function(assert) {
  1754. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1755. var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
  1756. var model3 = new Backbone.Model({id: 3, title: 'My Last Post'});
  1757. var collection = new Backbone.Collection([model]);
  1758. collection.on('update', function(context, options) {
  1759. var changed = options.changes;
  1760. assert.equal(changed.added.length, 2);
  1761. assert.equal(changed.merged.length, 0);
  1762. assert.ok(changed.removed.length === 1);
  1763. assert.strictEqual(changed.removed[0], model);
  1764. });
  1765. collection.set([model2, model3]);
  1766. });
  1767. QUnit.test("#3711 - set's `update` event returns multiple removed models", function(assert) {
  1768. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1769. var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
  1770. var model3 = new Backbone.Model({id: 3, title: 'My Last Post'});
  1771. var collection = new Backbone.Collection([model, model2]);
  1772. collection.on('update', function(context, options) {
  1773. var removedModels = options.changes.removed;
  1774. assert.ok(removedModels.length === 2);
  1775. assert.strictEqual(removedModels[0], model);
  1776. assert.strictEqual(removedModels[1], model2);
  1777. });
  1778. collection.set([model3]);
  1779. });
  1780. QUnit.test("#3711 - set's `update` event returns one merged model", function(assert) {
  1781. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1782. var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
  1783. var model2Update = new Backbone.Model({id: 2, title: 'Second Post V2'});
  1784. var collection = new Backbone.Collection([model, model2]);
  1785. collection.on('update', function(context, options) {
  1786. var mergedModels = options.changes.merged;
  1787. assert.ok(mergedModels.length === 1);
  1788. assert.strictEqual(mergedModels[0].get('title'), model2Update.get('title'));
  1789. });
  1790. collection.set([model2Update]);
  1791. });
  1792. QUnit.test("#3711 - set's `update` event returns multiple merged models", function(assert) {
  1793. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1794. var modelUpdate = new Backbone.Model({id: 1, title: 'First Post V2'});
  1795. var model2 = new Backbone.Model({id: 2, title: 'Second Post'});
  1796. var model2Update = new Backbone.Model({id: 2, title: 'Second Post V2'});
  1797. var collection = new Backbone.Collection([model, model2]);
  1798. collection.on('update', function(context, options) {
  1799. var mergedModels = options.changes.merged;
  1800. assert.ok(mergedModels.length === 2);
  1801. assert.strictEqual(mergedModels[0].get('title'), model2Update.get('title'));
  1802. assert.strictEqual(mergedModels[1].get('title'), modelUpdate.get('title'));
  1803. });
  1804. collection.set([model2Update, modelUpdate]);
  1805. });
  1806. QUnit.test("#3711 - set's `update` event should not be triggered adding a model which already exists exactly alike", function(assert) {
  1807. var fired = false;
  1808. var model = new Backbone.Model({id: 1, title: 'First Post'});
  1809. var collection = new Backbone.Collection([model]);
  1810. collection.on('update', function(context, options) {
  1811. fired = true;
  1812. });
  1813. collection.set([model]);
  1814. assert.equal(fired, false);
  1815. });
  1816. })(QUnit);