PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/collection.js

https://github.com/chandlerkent/backbone
JavaScript | 191 lines | 171 code | 20 blank | 0 comment | 5 complexity | ff71e8227a3cd38ad4cafcf039f7adfe 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: remove", function() {
  66. var removed = otherRemoved = null;
  67. col.bind('remove', function(model){ removed = model.get('label'); });
  68. otherCol.bind('remove', function(){ otherRemoved = true; });
  69. col.remove(e);
  70. equals(removed, 'e');
  71. equals(col.length, 4);
  72. equals(col.first(), d);
  73. equals(otherRemoved, null);
  74. });
  75. test("Collection: events are unbound on remove", function() {
  76. var counter = 0;
  77. var dj = new Backbone.Model();
  78. var emcees = new Backbone.Collection([dj]);
  79. emcees.bind('change', function(){ counter++; });
  80. dj.set({name : 'Kool'});
  81. equals(counter, 1);
  82. emcees.refresh([]);
  83. equals(dj.collection, undefined);
  84. dj.set({name : 'Shadow'});
  85. equals(counter, 1);
  86. });
  87. test("Collection: remove in multiple collections", function() {
  88. var modelData = {
  89. id : 5,
  90. title : 'Othello'
  91. };
  92. var passed = false;
  93. var e = new Backbone.Model(modelData);
  94. var f = new Backbone.Model(modelData);
  95. f.bind('remove', function() {
  96. passed = true;
  97. });
  98. var colE = new Backbone.Collection([e]);
  99. var colF = new Backbone.Collection([f]);
  100. ok(e != f);
  101. ok(colE.length == 1);
  102. ok(colF.length == 1);
  103. colE.remove(e);
  104. equals(passed, false);
  105. ok(colE.length == 0);
  106. colF.remove(e);
  107. ok(colF.length == 0);
  108. equals(passed, true);
  109. });
  110. test("Collection: fetch", function() {
  111. col.fetch();
  112. equals(lastRequest[0], 'read');
  113. equals(lastRequest[1], col);
  114. });
  115. test("Collection: create", function() {
  116. var model = col.create({label: 'f'});
  117. equals(lastRequest[0], 'create');
  118. equals(lastRequest[1], model);
  119. equals(model.get('label'), 'f');
  120. equals(model.collection, col);
  121. });
  122. test("collection: initialize", function() {
  123. var Collection = Backbone.Collection.extend({
  124. initialize: function() {
  125. this.one = 1;
  126. }
  127. });
  128. var coll = new Collection;
  129. equals(coll.one, 1);
  130. });
  131. test("Collection: toJSON", function() {
  132. equals(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
  133. });
  134. test("Collection: Underscore methods", function() {
  135. equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
  136. equals(col.any(function(model){ return model.id === 100; }), false);
  137. equals(col.any(function(model){ return model.id === 0; }), true);
  138. equals(col.indexOf(b), 2);
  139. equals(col.size(), 4);
  140. equals(col.rest().length, 3);
  141. ok(!_.include(col.rest()), a);
  142. ok(!_.include(col.rest()), d);
  143. ok(!col.isEmpty());
  144. ok(!_.include(col.without(d)), d);
  145. equals(col.max(function(model){ return model.id; }).id, 3);
  146. equals(col.min(function(model){ return model.id; }).id, 0);
  147. same(col.chain()
  148. .filter(function(o){ return o.id % 2 === 0; })
  149. .map(function(o){ return o.id * 2; })
  150. .value(),
  151. [0, 4]);
  152. });
  153. test("Collection: refresh", function() {
  154. var refreshed = 0;
  155. var models = col.models;
  156. col.bind('refresh', function() { refreshed += 1; });
  157. col.refresh([]);
  158. equals(refreshed, 1);
  159. equals(col.length, 0);
  160. equals(col.last(), null);
  161. col.refresh(models);
  162. equals(refreshed, 2);
  163. equals(col.length, 4);
  164. equals(col.last(), a);
  165. col.refresh(_.map(models, function(m){ return m.attributes; }));
  166. equals(refreshed, 3);
  167. equals(col.length, 4);
  168. ok(col.last() !== a);
  169. ok(_.isEqual(col.last().attributes, a.attributes));
  170. });
  171. });