PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test/collection.js

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