PageRenderTime 32ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/lodash/vendor/backbone/test/collection.js

https://gitlab.com/KongMono/GuitarChord
JavaScript | 1478 lines | 1320 code | 149 blank | 9 comment | 14 complexity | 4eb101c99a806c134b749a0490a19184 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('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('toJSON', function(assert) {
  599. assert.expect(1);
  600. assert.equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
  601. });
  602. QUnit.test('where and findWhere', function(assert) {
  603. assert.expect(8);
  604. var model = new Backbone.Model({a: 1});
  605. var coll = new Backbone.Collection([
  606. model,
  607. {a: 1},
  608. {a: 1, b: 2},
  609. {a: 2, b: 2},
  610. {a: 3}
  611. ]);
  612. assert.equal(coll.where({a: 1}).length, 3);
  613. assert.equal(coll.where({a: 2}).length, 1);
  614. assert.equal(coll.where({a: 3}).length, 1);
  615. assert.equal(coll.where({b: 1}).length, 0);
  616. assert.equal(coll.where({b: 2}).length, 2);
  617. assert.equal(coll.where({a: 1, b: 2}).length, 1);
  618. assert.equal(coll.findWhere({a: 1}), model);
  619. assert.equal(coll.findWhere({a: 4}), void 0);
  620. });
  621. QUnit.test('Underscore methods', function(assert) {
  622. assert.expect(21);
  623. assert.equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
  624. assert.equal(col.some(function(model){ return model.id === 100; }), false);
  625. assert.equal(col.some(function(model){ return model.id === 0; }), true);
  626. assert.equal(col.reduce(function(m1, m2) {return m1.id > m2.id ? m1 : m2;}).id, 3);
  627. assert.equal(col.reduceRight(function(m1, m2) {return m1.id > m2.id ? m1 : m2;}).id, 3);
  628. assert.equal(col.indexOf(b), 1);
  629. assert.equal(col.size(), 4);
  630. assert.equal(col.rest().length, 3);
  631. assert.ok(!_.includes(col.rest(), a));
  632. assert.ok(_.includes(col.rest(), d));
  633. assert.ok(!col.isEmpty());
  634. assert.ok(!_.includes(col.without(d), d));
  635. var wrapped = col.chain();
  636. assert.equal(wrapped.map('id').max().value(), 3);
  637. assert.equal(wrapped.map('id').min().value(), 0);
  638. assert.deepEqual(wrapped
  639. .filter(function(o){ return o.id % 2 === 0; })
  640. .map(function(o){ return o.id * 2; })
  641. .value(),
  642. [4, 0]);
  643. assert.deepEqual(col.difference([c, d]), [a, b]);
  644. assert.ok(col.includes(col.sample()));
  645. var first = col.first();
  646. assert.deepEqual(col.groupBy(function(model){ return model.id; })[first.id], [first]);
  647. assert.deepEqual(col.countBy(function(model){ return model.id; }), {0: 1, 1: 1, 2: 1, 3: 1});
  648. assert.deepEqual(col.sortBy(function(model){ return model.id; })[0], col.at(3));
  649. assert.ok(col.indexBy('id')[first.id] === first);
  650. });
  651. QUnit.test('Underscore methods with object-style and property-style iteratee', function(assert) {
  652. assert.expect(26);
  653. var model = new Backbone.Model({a: 4, b: 1, e: 3});
  654. var coll = new Backbone.Collection([
  655. {a: 1, b: 1},
  656. {a: 2, b: 1, c: 1},
  657. {a: 3, b: 1},
  658. model
  659. ]);
  660. assert.equal(coll.find({a: 0}), undefined);
  661. assert.deepEqual(coll.find({a: 4}), model);
  662. assert.equal(coll.find('d'), undefined);
  663. assert.deepEqual(coll.find('e'), model);
  664. assert.equal(coll.filter({a: 0}), false);
  665. assert.deepEqual(coll.filter({a: 4}), [model]);
  666. assert.equal(coll.some({a: 0}), false);
  667. assert.equal(coll.some({a: 1}), true);
  668. assert.equal(coll.reject({a: 0}).length, 4);
  669. assert.deepEqual(coll.reject({a: 4}), _.without(coll.models, model));
  670. assert.equal(coll.every({a: 0}), false);
  671. assert.equal(coll.every({b: 1}), true);
  672. assert.deepEqual(coll.partition({a: 0})[0], []);
  673. assert.deepEqual(coll.partition({a: 0})[1], coll.models);
  674. assert.deepEqual(coll.partition({a: 4})[0], [model]);
  675. assert.deepEqual(coll.partition({a: 4})[1], _.without(coll.models, model));
  676. assert.deepEqual(coll.map({a: 2}), [false, true, false, false]);
  677. assert.deepEqual(coll.map('a'), [1, 2, 3, 4]);
  678. assert.deepEqual(coll.sortBy('a')[3], model);
  679. assert.deepEqual(coll.sortBy('e')[0], model);
  680. assert.deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1});
  681. assert.deepEqual(coll.countBy('d'), {'undefined': 4});
  682. assert.equal(coll.findIndex({b: 1}), 0);
  683. assert.equal(coll.findIndex({b: 9}), -1);
  684. assert.equal(coll.findLastIndex({b: 1}), 3);
  685. assert.equal(coll.findLastIndex({b: 9}), -1);
  686. });
  687. QUnit.test('reset', function(assert) {
  688. assert.expect(16);
  689. var resetCount = 0;
  690. var models = col.models;
  691. col.on('reset', function() { resetCount += 1; });
  692. col.reset([]);
  693. assert.equal(resetCount, 1);
  694. assert.equal(col.length, 0);
  695. assert.equal(col.last(), null);
  696. col.reset(models);
  697. assert.equal(resetCount, 2);
  698. assert.equal(col.length, 4);
  699. assert.equal(col.last(), d);
  700. col.reset(_.map(models, function(m){ return m.attributes; }));
  701. assert.equal(resetCount, 3);
  702. assert.equal(col.length, 4);
  703. assert.ok(col.last() !== d);
  704. assert.ok(_.isEqual(col.last().attributes, d.attributes));
  705. col.reset();
  706. assert.equal(col.length, 0);
  707. assert.equal(resetCount, 4);
  708. var f = new Backbone.Model({id: 20, label: 'f'});
  709. col.reset([undefined, f]);
  710. assert.equal(col.length, 2);
  711. assert.equal(resetCount, 5);
  712. col.reset(new Array(4));
  713. assert.equal(col.length, 4);
  714. assert.equal(resetCount, 6);
  715. });
  716. QUnit.test('reset with different values', function(assert) {
  717. var collection = new Backbone.Collection({id: 1});
  718. collection.reset({id: 1, a: 1});
  719. assert.equal(collection.get(1).get('a'), 1);
  720. });
  721. QUnit.test('same references in reset', function(assert) {
  722. var model = new Backbone.Model({id: 1});
  723. var collection = new Backbone.Collection({id: 1});
  724. collection.reset(model);
  725. assert.equal(collection.get(1), model);
  726. });
  727. QUnit.test('reset passes caller options', function(assert) {
  728. assert.expect(3);
  729. var Model = Backbone.Model.extend({
  730. initialize: function(attrs, options) {
  731. this.modelParameter = options.modelParameter;
  732. }
  733. });
  734. var collection = new (Backbone.Collection.extend({model: Model}))();
  735. collection.reset([{astring: 'green', anumber: 1}, {astring: 'blue', anumber: 2}], {modelParameter: 'model parameter'});
  736. assert.equal(collection.length, 2);
  737. collection.each(function(model) {
  738. assert.equal(model.modelParameter, 'model parameter');
  739. });
  740. });
  741. QUnit.test('reset does not alter options by reference', function(assert) {
  742. assert.expect(2);
  743. var collection = new Backbone.Collection([{id: 1}]);
  744. var origOpts = {};
  745. collection.on('reset', function(coll, opts){
  746. assert.equal(origOpts.previousModels, undefined);
  747. assert.equal(opts.previousModels[0].id, 1);
  748. });
  749. collection.reset([], origOpts);
  750. });
  751. QUnit.test('trigger custom events on models', function(assert) {
  752. assert.expect(1);
  753. var fired = null;
  754. a.on('custom', function() { fired = true; });
  755. a.trigger('custom');
  756. assert.equal(fired, true);
  757. });
  758. QUnit.test('add does not alter arguments', function(assert) {
  759. assert.expect(2);
  760. var attrs = {};
  761. var models = [attrs];
  762. new Backbone.Collection().add(models);
  763. assert.equal(models.length, 1);
  764. assert.ok(attrs === models[0]);
  765. });
  766. QUnit.test('#714: access `model.collection` in a brand new model.', function(assert) {
  767. assert.expect(2);
  768. var collection = new Backbone.Collection;
  769. collection.url = '/test';
  770. var Model = Backbone.Model.extend({
  771. set: function(attrs) {
  772. assert.equal(attrs.prop, 'value');
  773. assert.equal(this.collection, collection);
  774. return this;
  775. }
  776. });
  777. collection.model = Model;
  778. collection.create({prop: 'value'});
  779. });
  780. QUnit.test('#574, remove its own reference to the .models array.', function(assert) {
  781. assert.expect(2);
  782. var collection = new Backbone.Collection([
  783. {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}
  784. ]);
  785. assert.equal(collection.length, 6);
  786. collection.remove(collection.models);
  787. assert.equal(collection.length, 0);
  788. });
  789. QUnit.test('#861, adding models to a collection which do not pass validation, with validate:true', function(assert) {
  790. assert.expect(2);
  791. var Model = Backbone.Model.extend({
  792. validate: function(attrs) {
  793. if (attrs.id === 3) return "id can't be 3";
  794. }
  795. });
  796. var Collection = Backbone.Collection.extend({
  797. model: Model
  798. });
  799. var collection = new Collection;
  800. collection.on('invalid', function() { assert.ok(true); });
  801. collection.add([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}], {validate: true});
  802. assert.deepEqual(collection.pluck('id'), [1, 2, 4, 5, 6]);
  803. });
  804. QUnit.test('Invalid models are discarded with validate:true.', function(assert) {
  805. assert.expect(5);
  806. var collection = new Backbone.Collection;
  807. collection.on('test', function() { assert.ok(true); });
  808. collection.model = Backbone.Model.extend({
  809. validate: function(attrs){ if (!attrs.valid) return 'invalid'; }
  810. });
  811. var model = new collection.model({id: 1, valid: true});
  812. collection.add([model, {id: 2}], {validate: true});
  813. model.trigger('test');
  814. assert.ok(collection.get(model.cid));
  815. assert.ok(collection.get(1));
  816. assert.ok(!collection.get(2));
  817. assert.equal(collection.length, 1);
  818. });
  819. QUnit.test('multiple copies of the same model', function(assert) {
  820. assert.expect(3);
  821. var collection = new Backbone.Collection();
  822. var model = new Backbone.Model();
  823. collection.add([model, model]);
  824. assert.equal(collection.length, 1);
  825. collection.add([{id: 1}, {id: 1}]);
  826. assert.equal(collection.length, 2);
  827. assert.equal(collection.last().id, 1);
  828. });
  829. QUnit.test('#964 - collection.get return inconsistent', function(assert) {
  830. assert.expect(2);
  831. var collection = new Backbone.Collection();
  832. assert.ok(collection.get(null) === undefined);
  833. assert.ok(collection.get() === undefined);
  834. });
  835. QUnit.test('#1112 - passing options.model sets collection.model', function(assert) {
  836. assert.expect(2);
  837. var Model = Backbone.Model.extend({});
  838. var collection = new Backbone.Collection([{id: 1}], {model: Model});
  839. assert.ok(collection.model === Model);
  840. assert.ok(collection.at(0) instanceof Model);
  841. });
  842. QUnit.test('null and undefined are invalid ids.', function(assert) {
  843. assert.expect(2);
  844. var model = new Backbone.Model({id: 1});
  845. var collection = new Backbone.Collection([model]);
  846. model.set({id: null});
  847. assert.ok(!collection.get('null'));
  848. model.set({id: 1});
  849. model.set({id: undefined});
  850. assert.ok(!collection.get('undefined'));
  851. });
  852. QUnit.test('falsy comparator', function(assert) {
  853. assert.expect(4);
  854. var Col = Backbone.Collection.extend({
  855. comparator: function(model){ return model.id; }
  856. });
  857. var collection = new Col();
  858. var colFalse = new Col(null, {comparator: false});
  859. var colNull = new Col(null, {comparator: null});
  860. var colUndefined = new Col(null, {comparator: undefined});
  861. assert.ok(collection.comparator);
  862. assert.ok(!colFalse.comparator);
  863. assert.ok(!colNull.comparator);
  864. assert.ok(colUndefined.comparator);
  865. });
  866. QUnit.test('#1355 - `options` is passed to success callbacks', function(assert) {
  867. assert.expect(2);
  868. var m = new Backbone.Model({x: 1});
  869. var collection = new Backbone.Collection();
  870. var opts = {
  871. opts: true,
  872. success: function(coll, resp, options) {
  873. assert.ok(options.opts);
  874. }
  875. };
  876. collection.sync = m.sync = function( method, coll, options ){
  877. options.success({});
  878. };
  879. collection.fetch(opts);
  880. collection.create(m, opts);
  881. });
  882. QUnit.test("#1412 - Trigger 'request' and 'sync' events.", function(assert) {
  883. assert.expect(4);
  884. var collection = new Backbone.Collection;
  885. collection.url = '/test';
  886. Backbone.ajax = function(settings){ settings.success(); };
  887. collection.on('request', function(obj, xhr, options) {
  888. assert.ok(obj === collection, "collection has correct 'request' event after fetching");
  889. });
  890. collection.on('sync', function(obj, response, options) {
  891. assert.ok(obj === collection, "collection has correct 'sync' event after fetching");
  892. });
  893. collection.fetch();
  894. collection.off();
  895. collection.on('request', function(obj, xhr, options) {
  896. assert.ok(obj === collection.get(1), "collection has correct 'request' event after one of its models save");
  897. });
  898. collection.on('sync', function(obj, response, options) {
  899. assert.ok(obj === collection.get(1), "collection has correct 'sync' event after one of its models save");
  900. });
  901. collection.create({id: 1});
  902. collection.off();
  903. });
  904. QUnit.test('#3283 - fetch, create calls success with context', function(assert) {
  905. assert.expect(2);
  906. var collection = new Backbone.Collection;
  907. collection.url = '/test';
  908. Backbone.ajax = function(settings) {
  909. settings.success.call(settings.context);
  910. };
  911. var obj = {};
  912. var options = {
  913. context: obj,
  914. success: function() {
  915. assert.equal(this, obj);
  916. }
  917. };
  918. collection.fetch(options);
  919. collection.create({id: 1}, options);
  920. });
  921. QUnit.test('#1447 - create with wait adds model.', function(assert) {
  922. assert.expect(1);
  923. var collection = new Backbone.Collection;
  924. var model = new Backbone.Model;
  925. model.sync = function(method, m, options){ options.success(); };
  926. collection.on('add', function(){ assert.ok(true); });
  927. collection.create(model, {wait: true});
  928. });
  929. QUnit.test('#1448 - add sorts collection after merge.', function(assert) {
  930. assert.expect(1);
  931. var collection = new Backbone.Collection([
  932. {id: 1, x: 1},
  933. {id: 2, x: 2}
  934. ]);
  935. collection.comparator = function(model){ return model.get('x'); };
  936. collection.add({id: 1, x: 3}, {merge: true});
  937. assert.deepEqual(collection.pluck('id'), [2, 1]);
  938. });
  939. QUnit.test('#1655 - groupBy can be used with a string argument.', function(assert) {
  940. assert.expect(3);
  941. var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
  942. var grouped = collection.groupBy('x');
  943. assert.strictEqual(_.keys(grouped).length, 2);
  944. assert.strictEqual(grouped[1][0].get('x'), 1);
  945. assert.strictEqual(grouped[2][0].get('x'), 2);
  946. });
  947. QUnit.test('#1655 - sortBy can be used with a string argument.', function(assert) {
  948. assert.expect(1);
  949. var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
  950. var values = _.map(collection.sortBy('x'), function(model) {
  951. return model.get('x');
  952. });
  953. assert.deepEqual(values, [1, 2, 3]);
  954. });
  955. QUnit.test('#1604 - Removal during iteration.', function(assert) {
  956. assert.expect(0);
  957. var collection = new Backbone.Collection([{}, {}]);
  958. collection.on('add', function() {
  959. collection.at(0).destroy();
  960. });
  961. collection.add({}, {at: 0});
  962. });
  963. QUnit.test('#1638 - `sort` during `add` triggers correctly.', function(assert) {
  964. var collection = new Backbone.Collection;
  965. collection.comparator = function(model) { return model.get('x'); };
  966. var added = [];
  967. collection.on('add', function(model) {
  968. model.set({x: 3});
  969. collection.sort();
  970. added.push(model.id);
  971. });
  972. collection.add([{id: 1, x: 1}, {id: 2, x: 2}]);
  973. assert.deepEqual(added, [1, 2]);
  974. });
  975. QUnit.test('fetch parses models by default', function(assert) {
  976. assert.expect(1);
  977. var model = {};
  978. var Collection = Backbone.Collection.extend({
  979. url: 'test',
  980. model: Backbone.Model.extend({
  981. parse: function(resp) {
  982. assert.strictEqual(resp, model);
  983. }
  984. })
  985. });
  986. new Collection().fetch();
  987. this.ajaxSettings.success([model]);
  988. });
  989. QUnit.test("`sort` shouldn't always fire on `add`", function(assert) {
  990. assert.expect(1);
  991. var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}], {
  992. comparator: 'id'
  993. });
  994. collection.sort = function(){ assert.ok(true); };
  995. collection.add([]);
  996. collection.add({id: 1});
  997. collection.add([{id: 2}, {id: 3}]);
  998. collection.add({id: 4});
  999. });
  1000. QUnit.test('#1407 parse option on constructor parses collection and models', function(assert) {
  1001. assert.expect(2);
  1002. var model = {
  1003. namespace: [{id: 1}, {id: 2}]
  1004. };
  1005. var Collection = Backbone.Collection.extend({
  1006. model: Backbone.Model.extend({
  1007. parse: function(m) {
  1008. m.name = 'test';
  1009. return m;
  1010. }
  1011. }),
  1012. parse: function(m) {
  1013. return m.namespace;
  1014. }
  1015. });
  1016. var collection = new Collection(model, {parse: true});
  1017. assert.equal(collection.length, 2);
  1018. assert.equal(collection.at(0).get('name'), 'test');
  1019. });
  1020. QUnit.test('#1407 parse option on reset parses collection and models', function(assert) {
  1021. assert.expect(2);
  1022. var model = {
  1023. namespace: [{id: 1}, {id: 2}]
  1024. };
  1025. var Collection = Backbone.Collection.extend({
  1026. model: Backbone.Model.extend({
  1027. parse: function(m) {
  1028. m.name = 'test';
  1029. return m;
  1030. }
  1031. }),
  1032. parse: function(m) {
  1033. return m.namespace;
  1034. }
  1035. });
  1036. var collection = new Collection();
  1037. collection.reset(model, {parse: true});
  1038. assert.equal(collection.length, 2);
  1039. assert.equal(collection.at(0).get('name'), 'test');
  1040. });
  1041. QUnit.test('Reset includes previous models in triggered event.', function(assert) {
  1042. assert.expect(1);
  1043. var model = new Backbone.Model();
  1044. var collection = new Backbone.Collection([model]);
  1045. collection.on('reset', function(coll, options) {
  1046. assert.deepEqual(options.previousModels, [model]);
  1047. });
  1048. collection.reset([]);
  1049. });
  1050. QUnit.test('set', function(assert) {
  1051. var m1 = new Backbone.Model();
  1052. var m2 = new Backbone.Model({id: 2});
  1053. var m3 = new Backbone.Model();
  1054. var collection = new Backbone.Collection([m1, m2]);
  1055. // Test add/change/remove events
  1056. collection.on('add', function(model) {
  1057. assert.strictEqual(model, m3);
  1058. });
  1059. collection.on('change', function(model) {
  1060. assert.strictEqual(model, m2);
  1061. });
  1062. collection.on('remove', function(model) {
  1063. assert.strictEqual(model, m1);
  1064. });
  1065. // remove: false doesn't remove any models
  1066. collection.set([], {remove: false});
  1067. assert.strictEqual(collection.length, 2);
  1068. // add: false doesn't add any models
  1069. collection.set([m1, m2, m3], {add: false});
  1070. assert.strictEqual(collection.length, 2);
  1071. // merge: false doesn't change any models
  1072. collection.set([m1, {id: 2, a: 1}], {merge: false});
  1073. assert.strictEqual(m2.get('a'), void 0);
  1074. // add: false, remove: false only merges existing models
  1075. collection.set([m1, {id: 2, a: 0}, m3, {id: 4}], {add: false, remove: false});
  1076. assert.strictEqual(collection.length, 2);
  1077. assert.strictEqual(m2.get('a'), 0);
  1078. // default options add/remove/merge as appropriate
  1079. collection.set([{id: 2, a: 1}, m3]);
  1080. assert.strictEqual(collection.length, 2);
  1081. assert.strictEqual(m2.get('a'), 1);
  1082. // Test removing models not passing an argument
  1083. collection.off('remove').on('remove', function(model) {
  1084. assert.ok(model === m2 || model === m3);
  1085. });
  1086. collection.set([]);
  1087. assert.strictEqual(collection.length, 0);
  1088. // Test null models on set doesn't clear collection
  1089. collection.off();
  1090. collection.set([{id: 1}]);
  1091. collection.set();
  1092. assert.strictEqual(collection.length, 1);
  1093. });
  1094. QUnit.test('set with only cids', function(assert) {
  1095. assert.expect(3);
  1096. var m1 = new Backbone.Model;
  1097. var m2 = new Backbone.Model;
  1098. var collection = new Backbone.Collection;
  1099. collection.set([m1, m2]);
  1100. assert.equal(collection.length, 2);
  1101. collection.set([m1]);
  1102. assert.equal(collection.length, 1);
  1103. collection.set([m1, m1, m1, m2, m2], {remove: false});
  1104. assert.equal(collection.length, 2);
  1105. });
  1106. QUnit.test('set with only idAttribute', function(assert) {
  1107. assert.expect(3);
  1108. var m1 = {_id: 1};
  1109. var m2 = {_id: 2};
  1110. var Col = Backbone.Collection.extend({
  1111. model: Backbone.Model.extend({
  1112. idAttribute: '_id'
  1113. })
  1114. });
  1115. var collection = new Col;
  1116. collection.set([m1, m2]);
  1117. assert.equal(collection.length, 2);
  1118. collection.set([m1]);
  1119. assert.equal(collection.length, 1);
  1120. collection.set([m1, m1, m1, m2, m2], {remove: false});
  1121. assert.equal(collection.length, 2);
  1122. });
  1123. QUnit.test('set + merge with default values defined', function(assert) {
  1124. var Model = Backbone.Model.extend({
  1125. defaults: {
  1126. key: 'value'
  1127. }
  1128. });
  1129. var m = new Model({id: 1});
  1130. var collection = new Backbone.Collection([m], {model: Model});
  1131. assert.equal(collection.first().get('key'), 'value');
  1132. collection.set({id: 1, key: 'other'});
  1133. assert.equal(collection.first().get('key'), 'other');
  1134. collection.set({id: 1, other: 'value'});
  1135. assert.equal(collection.first().get('key'), 'other');
  1136. assert.equal(collection.length, 1);
  1137. });
  1138. QUnit.test('merge without mutation', function(assert) {
  1139. var Model = Backbone.Model.extend({
  1140. initialize: function(attrs, options) {
  1141. if (attrs.child) {
  1142. this.set('child', new Model(attrs.child, options), options);
  1143. }
  1144. }
  1145. });
  1146. var Collection = Backbone.Collection.extend({model: Model});
  1147. var data = [{id: 1, child: {id: 2}}];
  1148. var collection = new Collection(data);
  1149. assert.equal(collection.first().id, 1);
  1150. collection.set(data);
  1151. assert.equal(collection.first().id, 1);
  1152. collection.set([{id: 2, child: {id: 2}}].concat(data));
  1153. assert.deepEqual(collection.pluck('id'), [2, 1]);
  1154. });
  1155. QUnit.test('`set` and model level `parse`', function(assert) {
  1156. var Model = Backbone.Model.extend({});
  1157. var Collection = Backbone.Collection.extend({
  1158. model: Model,
  1159. parse: function(res) { return _.map(res.models, 'model'); }
  1160. });
  1161. var model = new Model({id: 1});
  1162. var collection = new Collection(model);
  1163. collection.set({models: [
  1164. {model: {id: 1}},
  1165. {model: {id: 2}}
  1166. ]}, {parse: true});
  1167. assert.equal(collection.first(), model);
  1168. });
  1169. QUnit.test('`set` data is only parsed once', function(assert) {
  1170. var collection = new Backbone.Collection();
  1171. collection.model = Backbone.Model.extend({
  1172. parse: function(data) {
  1173. assert.equal(data.parsed, void 0);
  1174. data.parsed = true;
  1175. return data;
  1176. }
  1177. });
  1178. collection.set({}, {parse: true});
  1179. });
  1180. QUnit.test('`set` matches input order in the absence of a comparator', function(assert) {
  1181. var one = new Backbone.Model({id: 1});
  1182. var two = new Backbone.Model({id: 2});
  1183. var three = new Backbone.Model({id: 3});
  1184. var collection = new Backbone.Collection([one, two, three]);
  1185. collection.set([{id: 3}, {id: 2}, {id: 1}]);
  1186. assert.deepEqual(collection.models, [three, two, one]);
  1187. collection.set([{id: 1}, {id: 2}]);
  1188. assert.deepEqual(collection.models, [one, two]);
  1189. collection.set([two, three, one]);
  1190. assert.deepEqual(collection.models, [two, three, one]);
  1191. collection.set([{id: 1}, {id: 2}], {remove: false});
  1192. assert.deepEqual(collection.models, [two, three, one]);
  1193. collection.set([{id: 1}, {id: 2}, {id: 3}], {merge: false});
  1194. assert.deepEqual(collection.models, [one, two, three]);
  1195. collection.set([three, two, one, {id: 4}], {add: false});
  1196. assert.deepEqual(collection.models, [one, two, three]);
  1197. });
  1198. QUnit.test('#1894 - Push should not trigger a sort', function(assert) {
  1199. assert.expect(0);
  1200. var Collection = Backbone.Collection.extend({
  1201. comparator: 'id',
  1202. sort: function() { assert.ok(false); }
  1203. });
  1204. new Collection().push({id: 1});
  1205. });
  1206. QUnit.test('#2428 - push duplicate models, return the correct one', function(assert) {
  1207. assert.expect(1);
  1208. var collection = new Backbone.Collection;
  1209. var model1 = collection.push({id: 101});
  1210. var model2 = collection.push({id: 101});
  1211. assert.ok(model2.cid === model1.cid);
  1212. });
  1213. QUnit.test('`set` with non-normal id', function(assert) {
  1214. var Collection = Backbone.Collection.extend({
  1215. model: Backbone.Model.extend({idAttribute: '_id'})
  1216. });
  1217. var collection = new Collection({_id: 1});
  1218. collection.set([{_id: 1, a: 1}], {add: false});
  1219. assert.equal(collection.first().get('a'), 1);
  1220. });
  1221. QUnit.test('#1894 - `sort` can optionally be turned off', 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().add({id: 1}, {sort: false});
  1228. });
  1229. QUnit.test('#1915 - `parse` data in the right order in `set`', function(assert) {
  1230. var collection = new (Backbone.Collection.extend({
  1231. parse: function(data) {
  1232. assert.strictEqual(data.status, 'ok');
  1233. return data.data;
  1234. }
  1235. }));
  1236. var res = {status: 'ok', data: [{id: 1}]};
  1237. collection.set(res, {parse: true});
  1238. });
  1239. QUnit.test('#1939 - `parse` is passed `options`', function(assert) {
  1240. var done = assert.async();
  1241. assert.expect(1);
  1242. var collection = new (Backbone.Collection.extend({
  1243. url: '/',
  1244. parse: function(data, options) {
  1245. assert.strictEqual(options.xhr.someHeader, 'headerValue');
  1246. return data;
  1247. }
  1248. }));
  1249. var ajax = Backbone.ajax;
  1250. Backbone.ajax = function(params) {
  1251. _.defer(params.success, []);
  1252. return {someHeader: 'headerValue'};
  1253. };
  1254. collection.fetch({
  1255. success: function() { done(); }
  1256. });
  1257. Backbone.ajax = ajax;
  1258. });
  1259. QUnit.test('fetch will pass extra options to success callback', function(assert) {
  1260. assert.expect(1);
  1261. var SpecialSyncCollection = Backbone.Collection.extend({
  1262. url: '/test',
  1263. sync: function(method, collection, options) {
  1264. _.extend(options, {specialSync: true});
  1265. return Backbone.Collection.prototype.sync.call(this, method, collection, options);
  1266. }
  1267. });
  1268. var collection = new SpecialSyncCollection();
  1269. var onSuccess = function(coll, resp, options) {
  1270. assert.ok(options.specialSync, 'Options were passed correctly to callback');
  1271. };
  1272. collection.fetch({success: onSuccess});
  1273. this.ajaxSettings.success();
  1274. });
  1275. QUnit.test('`add` only `sort`s when necessary', function(assert) {
  1276. assert.expect(2);
  1277. var collection = new (Backbone.Collection.extend({
  1278. comparator: 'a'
  1279. }))([{id: 1}, {id: 2}, {id: 3}]);
  1280. collection.on('sort', function() { assert.ok(true); });
  1281. collection.add({id: 4}); // do sort, new model
  1282. collection.add({id: 1, a: 1}, {merge: true}); // do sort, comparator change
  1283. collection.add({id: 1, b: 1}, {merge: true}); // don't sort, no comparator change
  1284. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no comparator change
  1285. collection.add(collection.models); // don't sort, nothing new
  1286. collection.add(collection.models, {merge: true}); // don't sort
  1287. });
  1288. QUnit.test('`add` only `sort`s when necessary with comparator function', function(assert) {
  1289. assert.expect(3);
  1290. var collection = new (Backbone.Collection.extend({
  1291. comparator: function(m1, m2) {
  1292. return m1.get('a') > m2.get('a') ? 1 : (m1.get('a') < m2.get('a') ? -1 : 0);
  1293. }
  1294. }))([{id: 1}, {id: 2}, {id: 3}]);
  1295. collection.on('sort', function() { assert.ok(true); });
  1296. collection.add({id: 4}); // do sort, new model
  1297. collection.add({id: 1, a: 1}, {merge: true}); // do sort, model change
  1298. collection.add({id: 1, b: 1}, {merge: true}); // do sort, model change
  1299. collection.add({id: 1, a: 1}, {merge: true}); // don't sort, no model change
  1300. collection.add(collection.models); // don't sort, nothing new
  1301. collection.add(collection.models, {merge: true}); // don't sort
  1302. });
  1303. QUnit.test('Attach options to collection.', function(assert) {
  1304. assert.expect(2);
  1305. var Model = Backbone.Model;
  1306. var comparator = function(){};
  1307. var collection = new Backbone.Collection([], {
  1308. model: Model,
  1309. comparator: comparator
  1310. });
  1311. assert.ok(collection.model === Model);
  1312. assert.ok(collection.comparator === comparator);
  1313. });
  1314. QUnit.test('Pass falsey for `models` for empty Col with `options`', function(assert) {
  1315. assert.expect(9);
  1316. var opts = {a: 1, b: 2};
  1317. _.forEach([undefined, null, false], function(falsey) {
  1318. var Collection = Backbone.Collection.extend({
  1319. initialize: function(models, options) {
  1320. assert.strictEqual(models, falsey);
  1321. assert.strictEqual(options, opts);
  1322. }
  1323. });
  1324. var collection = new Collection(falsey, opts);
  1325. assert.strictEqual(collection.length, 0);
  1326. });
  1327. });
  1328. QUnit.test('`add` overrides `set` flags', function(assert) {
  1329. var collection = new Backbone.Col