PageRenderTime 789ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/test/collection.js

https://github.com/crnt/backbone
JavaScript | 345 lines | 314 code | 30 blank | 1 comment | 16 complexity | cae9a650ea497899f41b647e4eb45320 MD5 | raw file
  1. $(document).ready(function() {
  2. module("Backbone.Collection");
  3. window.lastRequest = null;
  4. Backbone.sync = function() {
  5. lastRequest = _.toArray(arguments);
  6. };
  7. var a = new Backbone.Model({id: 3, label: 'a'});
  8. var b = new Backbone.Model({id: 2, label: 'b'});
  9. var c = new Backbone.Model({id: 1, label: 'c'});
  10. var d = new Backbone.Model({id: 0, label: 'd'});
  11. var e = null;
  12. var col = new Backbone.Collection([a,b,c,d]);
  13. var otherCol = new Backbone.Collection();
  14. test("Collection: new and sort", function() {
  15. equals(col.first(), a, "a should be first");
  16. equals(col.last(), d, "d should be last");
  17. col.comparator = function(model) { return model.id; };
  18. col.sort();
  19. equals(col.first(), d, "d should be first");
  20. equals(col.last(), a, "a should be last");
  21. equals(col.length, 4);
  22. });
  23. test("Collection: get, getByCid", function() {
  24. equals(col.get(0), d);
  25. equals(col.get(2), b);
  26. equals(col.getByCid(col.first().cid), col.first());
  27. });
  28. test("Collection: get with non-default ids", function() {
  29. var col = new Backbone.Collection();
  30. var MongoModel = Backbone.Model.extend({
  31. idAttribute: '_id'
  32. });
  33. var model = new MongoModel({_id: 100});
  34. col.add(model);
  35. equals(col.get(100), model);
  36. model.set({_id: 101});
  37. equals(col.get(101), model);
  38. });
  39. test("Collection: update index when id changes", function() {
  40. var col = new Backbone.Collection();
  41. col.add([
  42. {id : 0, name : 'one'},
  43. {id : 1, name : 'two'}
  44. ]);
  45. var one = col.get(0);
  46. equals(one.get('name'), 'one');
  47. one.set({id : 101});
  48. equals(col.get(0), null);
  49. equals(col.get(101).get('name'), 'one');
  50. });
  51. test("Collection: at", function() {
  52. equals(col.at(2), b);
  53. });
  54. test("Collection: pluck", function() {
  55. equals(col.pluck('label').join(' '), 'd c b a');
  56. });
  57. test("Collection: add", function() {
  58. var added = opts = secondAdded = null;
  59. e = new Backbone.Model({id: 10, label : 'e'});
  60. otherCol.add(e);
  61. otherCol.bind('add', function() {
  62. secondAdded = true;
  63. });
  64. col.bind('add', function(model, collection, options){
  65. added = model.get('label');
  66. opts = options;
  67. });
  68. col.add(e, {amazing: true});
  69. equals(added, 'e');
  70. equals(col.length, 5);
  71. equals(col.last(), e);
  72. equals(otherCol.length, 1);
  73. equals(secondAdded, null);
  74. ok(opts.amazing);
  75. var f = new Backbone.Model({id: 20, label : 'f'});
  76. var g = new Backbone.Model({id: 21, label : 'g'});
  77. var h = new Backbone.Model({id: 22, label : 'h'});
  78. var atCol = new Backbone.Collection([f, g, h]);
  79. equals(atCol.length, 3);
  80. atCol.add(e, {at: 1});
  81. equals(atCol.length, 4);
  82. equals(atCol.at(1), e);
  83. equals(atCol.last(), h);
  84. });
  85. test("Collection: add model to collection twice", function() {
  86. try {
  87. // no id, same cid
  88. var a2 = new Backbone.Model({label: a.label});
  89. a2.cid = a.cid;
  90. col.add(a2);
  91. ok(false, "duplicate; expected add to fail");
  92. } catch (e) {
  93. equals(e.message, "Can't add the same model to a set twice,3");
  94. }
  95. });
  96. test("Collection: add model to multiple collections", function() {
  97. var counter = 0;
  98. var e = new Backbone.Model({id: 10, label : 'e'});
  99. e.bind('add', function(model, collection) {
  100. counter++;
  101. equals(e, model);
  102. if (counter > 1) {
  103. equals(collection, colF);
  104. } else {
  105. equals(collection, colE);
  106. }
  107. });
  108. var colE = new Backbone.Collection([]);
  109. colE.bind('add', function(model, collection) {
  110. equals(e, model);
  111. equals(colE, collection);
  112. });
  113. var colF = new Backbone.Collection([]);
  114. colF.bind('add', function(model, collection) {
  115. equals(e, model);
  116. equals(colF, collection);
  117. });
  118. colE.add(e);
  119. equals(e.collection, colE);
  120. colF.add(e);
  121. equals(e.collection, colE);
  122. });
  123. test("Collection: remove", function() {
  124. var removed = otherRemoved = null;
  125. col.bind('remove', function(model){ removed = model.get('label'); });
  126. otherCol.bind('remove', function(){ otherRemoved = true; });
  127. col.remove(e);
  128. equals(removed, 'e');
  129. equals(col.length, 4);
  130. equals(col.first(), d);
  131. equals(otherRemoved, null);
  132. });
  133. test("Collection: events are unbound on remove", function() {
  134. var counter = 0;
  135. var dj = new Backbone.Model();
  136. var emcees = new Backbone.Collection([dj]);
  137. emcees.bind('change', function(){ counter++; });
  138. dj.set({name : 'Kool'});
  139. equals(counter, 1);
  140. emcees.reset([]);
  141. equals(dj.collection, undefined);
  142. dj.set({name : 'Shadow'});
  143. equals(counter, 1);
  144. });
  145. test("Collection: remove in multiple collections", function() {
  146. var modelData = {
  147. id : 5,
  148. title : 'Othello'
  149. };
  150. var passed = false;
  151. var e = new Backbone.Model(modelData);
  152. var f = new Backbone.Model(modelData);
  153. f.bind('remove', function() {
  154. passed = true;
  155. });
  156. var colE = new Backbone.Collection([e]);
  157. var colF = new Backbone.Collection([f]);
  158. ok(e != f);
  159. ok(colE.length == 1);
  160. ok(colF.length == 1);
  161. colE.remove(e);
  162. equals(passed, false);
  163. ok(colE.length == 0);
  164. colF.remove(e);
  165. ok(colF.length == 0);
  166. equals(passed, true);
  167. });
  168. test("Collection: remove same model in multiple collection", function() {
  169. var counter = 0;
  170. var e = new Backbone.Model({id: 5, title: 'Othello'});
  171. e.bind('remove', function(model, collection) {
  172. counter++;
  173. equals(e, model);
  174. if (counter > 1) {
  175. equals(collection, colE);
  176. } else {
  177. equals(collection, colF);
  178. }
  179. });
  180. var colE = new Backbone.Collection([e]);
  181. colE.bind('remove', function(model, collection) {
  182. equals(e, model);
  183. equals(colE, collection);
  184. });
  185. var colF = new Backbone.Collection([e]);
  186. colF.bind('remove', function(model, collection) {
  187. equals(e, model);
  188. equals(colF, collection);
  189. });
  190. equals(colE, e.collection);
  191. colF.remove(e);
  192. ok(colF.length == 0);
  193. ok(colE.length == 1);
  194. equals(counter, 1);
  195. equals(colE, e.collection);
  196. colE.remove(e);
  197. equals(null, e.collection);
  198. ok(colE.length == 0);
  199. equals(counter, 2);
  200. });
  201. test("Collection: model destroy removes from all collections", function() {
  202. var e = new Backbone.Model({id: 5, title: 'Othello'});
  203. e.sync = function(method, model, options) { options.success({}); };
  204. var colE = new Backbone.Collection([e]);
  205. var colF = new Backbone.Collection([e]);
  206. e.destroy();
  207. ok(colE.length == 0);
  208. ok(colF.length == 0);
  209. equals(null, e.collection);
  210. });
  211. test("Colllection: non-persisted model destroy removes from all collections", function() {
  212. var e = new Backbone.Model({title: 'Othello'});
  213. e.sync = function(method, model, options) { throw "should not be called"; };
  214. var colE = new Backbone.Collection([e]);
  215. var colF = new Backbone.Collection([e]);
  216. e.destroy();
  217. ok(colE.length == 0);
  218. ok(colF.length == 0);
  219. equals(null, e.collection);
  220. });
  221. test("Collection: fetch", function() {
  222. col.fetch();
  223. equals(lastRequest[0], 'read');
  224. equals(lastRequest[1], col);
  225. });
  226. test("Collection: create", function() {
  227. var model = col.create({label: 'f'});
  228. equals(lastRequest[0], 'create');
  229. equals(lastRequest[1], model);
  230. equals(model.get('label'), 'f');
  231. equals(model.collection, col);
  232. });
  233. test("Collection: create enforces validation", function() {
  234. var ValidatingModel = Backbone.Model.extend({
  235. validate: function(attrs) {
  236. return "fail";
  237. }
  238. });
  239. var ValidatingCollection = Backbone.Collection.extend({
  240. model: ValidatingModel
  241. });
  242. var col = new ValidatingCollection();
  243. equals(col.create({"foo":"bar"}),false);
  244. });
  245. test("Collection: a failing create runs the error callback", function() {
  246. var ValidatingModel = Backbone.Model.extend({
  247. validate: function(attrs) {
  248. return "fail";
  249. }
  250. });
  251. var ValidatingCollection = Backbone.Collection.extend({
  252. model: ValidatingModel
  253. });
  254. var flag = false;
  255. var callback = function(model, error) { flag = true; };
  256. var col = new ValidatingCollection();
  257. col.create({"foo":"bar"}, { error: callback });
  258. equals(flag, true);
  259. });
  260. test("collection: initialize", function() {
  261. var Collection = Backbone.Collection.extend({
  262. initialize: function() {
  263. this.one = 1;
  264. }
  265. });
  266. var coll = new Collection;
  267. equals(coll.one, 1);
  268. });
  269. test("Collection: toJSON", function() {
  270. equals(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
  271. });
  272. test("Collection: Underscore methods", function() {
  273. equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
  274. equals(col.any(function(model){ return model.id === 100; }), false);
  275. equals(col.any(function(model){ return model.id === 0; }), true);
  276. equals(col.indexOf(b), 2);
  277. equals(col.size(), 4);
  278. equals(col.rest().length, 3);
  279. ok(!_.include(col.rest()), a);
  280. ok(!_.include(col.rest()), d);
  281. ok(!col.isEmpty());
  282. ok(!_.include(col.without(d)), d);
  283. equals(col.max(function(model){ return model.id; }).id, 3);
  284. equals(col.min(function(model){ return model.id; }).id, 0);
  285. same(col.chain()
  286. .filter(function(o){ return o.id % 2 === 0; })
  287. .map(function(o){ return o.id * 2; })
  288. .value(),
  289. [0, 4]);
  290. });
  291. test("Collection: reset", function() {
  292. var resetCount = 0;
  293. var models = col.models;
  294. col.bind('reset', function() { resetCount += 1; });
  295. col.reset([]);
  296. equals(resetCount, 1);
  297. equals(col.length, 0);
  298. equals(col.last(), null);
  299. col.reset(models);
  300. equals(resetCount, 2);
  301. equals(col.length, 4);
  302. equals(col.last(), a);
  303. col.reset(_.map(models, function(m){ return m.attributes; }));
  304. equals(resetCount, 3);
  305. equals(col.length, 4);
  306. ok(col.last() !== a);
  307. ok(_.isEqual(col.last().attributes, a.attributes));
  308. });
  309. test("Collection: trigger custom events on models", function() {
  310. var fired = null;
  311. a.bind("custom", function() { fired = true; });
  312. a.trigger("custom");
  313. equals(fired, true);
  314. });
  315. });