PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/test/collection.js

https://github.com/djmitche/backbone
JavaScript | 264 lines | 241 code | 23 blank | 0 comment | 14 complexity | 17cc73efcef3f6f68e86101810a7b1f4 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: update index when id changes", function() {
  29. var col = new Backbone.Collection();
  30. col.add([
  31. {id : 0, name : 'one'},
  32. {id : 1, name : 'two'}
  33. ]);
  34. var one = col.get(0);
  35. equals(one.get('name'), 'one');
  36. one.set({id : 101});
  37. equals(col.get(0), null);
  38. equals(col.get(101).get('name'), 'one');
  39. });
  40. test("Collection: at", function() {
  41. equals(col.at(2), b);
  42. });
  43. test("Collection: pluck", function() {
  44. equals(col.pluck('label').join(' '), 'd c b a');
  45. });
  46. test("Collection: add", function() {
  47. var added = opts = secondAdded = null;
  48. e = new Backbone.Model({id: 10, label : 'e'});
  49. otherCol.add(e);
  50. otherCol.bind('add', function() {
  51. secondAdded = true;
  52. });
  53. col.bind('add', function(model, collection, options){
  54. added = model.get('label');
  55. opts = options;
  56. });
  57. col.add(e, {amazing: true});
  58. equals(added, 'e');
  59. equals(col.length, 5);
  60. equals(col.last(), e);
  61. equals(otherCol.length, 1);
  62. equals(secondAdded, null);
  63. ok(opts.amazing);
  64. });
  65. test("Collection: add model to multiple collections", function() {
  66. var counter = 0;
  67. var e = new Backbone.Model({id: 10, label : 'e'});
  68. e.bind('add', function(model, collection) {
  69. counter++;
  70. equals(e, model);
  71. if (counter > 1) {
  72. equals(collection, colF);
  73. } else {
  74. equals(collection, colE);
  75. }
  76. });
  77. var colE = new Backbone.Collection([]);
  78. colE.bind('add', function(model, collection) {
  79. equals(e, model);
  80. equals(colE, collection);
  81. });
  82. var colF = new Backbone.Collection([]);
  83. colF.bind('add', function(model, collection) {
  84. equals(e, model);
  85. equals(colF, collection);
  86. });
  87. colE.add(e);
  88. equals(e.collection, colE);
  89. colF.add(e);
  90. equals(e.collection, colE);
  91. });
  92. test("Collection: remove", function() {
  93. var removed = otherRemoved = null;
  94. col.bind('remove', function(model){ removed = model.get('label'); });
  95. otherCol.bind('remove', function(){ otherRemoved = true; });
  96. col.remove(e);
  97. equals(removed, 'e');
  98. equals(col.length, 4);
  99. equals(col.first(), d);
  100. equals(otherRemoved, null);
  101. });
  102. test("Collection: events are unbound on remove", function() {
  103. var counter = 0;
  104. var dj = new Backbone.Model();
  105. var emcees = new Backbone.Collection([dj]);
  106. emcees.bind('change', function(){ counter++; });
  107. dj.set({name : 'Kool'});
  108. equals(counter, 1);
  109. emcees.refresh([]);
  110. equals(dj.collection, undefined);
  111. dj.set({name : 'Shadow'});
  112. equals(counter, 1);
  113. });
  114. test("Collection: remove in multiple collections", function() {
  115. var modelData = {
  116. id : 5,
  117. title : 'Othello'
  118. };
  119. var passed = false;
  120. var e = new Backbone.Model(modelData);
  121. var f = new Backbone.Model(modelData);
  122. f.bind('remove', function() {
  123. passed = true;
  124. });
  125. var colE = new Backbone.Collection([e]);
  126. var colF = new Backbone.Collection([f]);
  127. ok(e != f);
  128. ok(colE.length == 1);
  129. ok(colF.length == 1);
  130. colE.remove(e);
  131. equals(passed, false);
  132. ok(colE.length == 0);
  133. colF.remove(e);
  134. ok(colF.length == 0);
  135. equals(passed, true);
  136. });
  137. test("Collection: remove same model in multiple collection", function() {
  138. var counter = 0;
  139. var e = new Backbone.Model({id: 5, title: 'Othello'});
  140. e.bind('remove', function(model, collection) {
  141. counter++;
  142. equals(e, model);
  143. if (counter > 1) {
  144. equals(collection, colE);
  145. } else {
  146. equals(collection, colF);
  147. }
  148. });
  149. var colE = new Backbone.Collection([e]);
  150. colE.bind('remove', function(model, collection) {
  151. equals(e, model);
  152. equals(colE, collection);
  153. });
  154. var colF = new Backbone.Collection([e]);
  155. colF.bind('remove', function(model, collection) {
  156. equals(e, model);
  157. equals(colF, collection);
  158. });
  159. equals(colE, e.collection);
  160. colF.remove(e);
  161. ok(colF.length == 0);
  162. ok(colE.length == 1);
  163. equals(counter, 1);
  164. equals(colE, e.collection);
  165. colE.remove(e);
  166. equals(null, e.collection);
  167. ok(colE.length == 0);
  168. equals(counter, 2);
  169. });
  170. test("Colllection: model destroy removes from all collections", function() {
  171. var e = new Backbone.Model({id: 5, title: 'Othello'});
  172. e.sync = function(method, model, options) { options.success({}); };
  173. var colE = new Backbone.Collection([e]);
  174. var colF = new Backbone.Collection([e]);
  175. e.destroy();
  176. ok(colE.length == 0);
  177. ok(colF.length == 0);
  178. equals(null, e.collection);
  179. });
  180. test("Collection: fetch", function() {
  181. col.fetch();
  182. equals(lastRequest[0], 'read');
  183. equals(lastRequest[1], col);
  184. });
  185. test("Collection: create", function() {
  186. var model = col.create({label: 'f'});
  187. equals(lastRequest[0], 'create');
  188. equals(lastRequest[1], model);
  189. equals(model.get('label'), 'f');
  190. equals(model.collection, col);
  191. });
  192. test("collection: initialize", function() {
  193. var Collection = Backbone.Collection.extend({
  194. initialize: function() {
  195. this.one = 1;
  196. }
  197. });
  198. var coll = new Collection;
  199. equals(coll.one, 1);
  200. });
  201. test("Collection: toJSON", function() {
  202. equals(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
  203. });
  204. test("Collection: Underscore methods", function() {
  205. equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
  206. equals(col.any(function(model){ return model.id === 100; }), false);
  207. equals(col.any(function(model){ return model.id === 0; }), true);
  208. equals(col.indexOf(b), 2);
  209. equals(col.size(), 4);
  210. equals(col.rest().length, 3);
  211. ok(!_.include(col.rest()), a);
  212. ok(!_.include(col.rest()), d);
  213. ok(!col.isEmpty());
  214. ok(!_.include(col.without(d)), d);
  215. equals(col.max(function(model){ return model.id; }).id, 3);
  216. equals(col.min(function(model){ return model.id; }).id, 0);
  217. same(col.chain()
  218. .filter(function(o){ return o.id % 2 === 0; })
  219. .map(function(o){ return o.id * 2; })
  220. .value(),
  221. [0, 4]);
  222. });
  223. test("Collection: refresh", function() {
  224. var refreshed = 0;
  225. var models = col.models;
  226. col.bind('refresh', function() { refreshed += 1; });
  227. col.refresh([]);
  228. equals(refreshed, 1);
  229. equals(col.length, 0);
  230. equals(col.last(), null);
  231. col.refresh(models);
  232. equals(refreshed, 2);
  233. equals(col.length, 4);
  234. equals(col.last(), a);
  235. col.refresh(_.map(models, function(m){ return m.attributes; }));
  236. equals(refreshed, 3);
  237. equals(col.length, 4);
  238. ok(col.last() !== a);
  239. ok(_.isEqual(col.last().attributes, a.attributes));
  240. });
  241. });