/node_modules/backbone/test/collection.js

https://github.com/rsimba/nodechat-tutorial · JavaScript · 169 lines · 150 code · 19 blank · 0 comment · 5 complexity · 16041eee6b89e85f8f3a5909100765c5 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 = window.col = new Backbone.Collection([a,b,c,d]);
  13. test("Collection: new and sort", function() {
  14. equals(col.first(), a, "a should be first");
  15. equals(col.last(), d, "d should be last");
  16. col.comparator = function(model) { return model.id; };
  17. col.sort();
  18. equals(col.first(), d, "d should be first");
  19. equals(col.last(), a, "a should be last");
  20. equals(col.length, 4);
  21. });
  22. test("Collection: get, getByCid", function() {
  23. equals(col.get(0), d);
  24. equals(col.get(2), b);
  25. equals(col.getByCid(col.first().cid), col.first());
  26. });
  27. test("Collection: update index when id changes", function() {
  28. var col = new Backbone.Collection();
  29. col.add([
  30. {id : 0, name : 'one'},
  31. {id : 1, name : 'two'}
  32. ]);
  33. var one = col.get(0);
  34. equals(one.get('name'), 'one');
  35. one.set({id : 101});
  36. equals(col.get(0), null);
  37. equals(col.get(101).get('name'), 'one');
  38. });
  39. test("Collection: at", function() {
  40. equals(col.at(2), b);
  41. });
  42. test("Collection: pluck", function() {
  43. equals(col.pluck('label').join(' '), 'd c b a');
  44. });
  45. test("Collection: add", function() {
  46. var added = opts = null;
  47. col.bind('add', function(model, collection, options){
  48. added = model.get('label');
  49. opts = options;
  50. });
  51. e = new Backbone.Model({id: 10, label : 'e'});
  52. col.add(e, {amazing: true});
  53. equals(added, 'e');
  54. equals(col.length, 5);
  55. equals(col.last(), e);
  56. ok(opts.amazing);
  57. });
  58. test("Collection: remove", function() {
  59. var removed = null;
  60. col.bind('remove', function(model){ removed = model.get('label'); });
  61. col.remove(e);
  62. equals(removed, 'e');
  63. equals(col.length, 4);
  64. equals(col.first(), d);
  65. });
  66. test("Collection: remove in multiple collections", function() {
  67. var modelData = {
  68. id : 5,
  69. title : 'Othello'
  70. };
  71. var passed = false;
  72. var e = new Backbone.Model(modelData);
  73. var f = new Backbone.Model(modelData);
  74. f.bind('remove', function() {
  75. passed = true;
  76. });
  77. var colE = new Backbone.Collection([e]);
  78. var colF = new Backbone.Collection([f]);
  79. ok(e != f);
  80. ok(colE.length == 1);
  81. ok(colF.length == 1);
  82. colE.remove(e);
  83. equals(passed, false);
  84. ok(colE.length == 0);
  85. colF.remove(e);
  86. ok(colF.length == 0);
  87. equals(passed, true);
  88. });
  89. test("Collection: fetch", function() {
  90. col.fetch();
  91. equals(lastRequest[0], 'read');
  92. equals(lastRequest[1], col);
  93. });
  94. test("Collection: create", function() {
  95. var model = col.create({label: 'f'});
  96. equals(lastRequest[0], 'create');
  97. equals(lastRequest[1], model);
  98. equals(model.get('label'), 'f');
  99. equals(model.collection, col);
  100. });
  101. test("collection: initialize", function() {
  102. var Collection = Backbone.Collection.extend({
  103. initialize: function() {
  104. this.one = 1;
  105. }
  106. });
  107. var coll = new Collection;
  108. equals(coll.one, 1);
  109. });
  110. test("Collection: toJSON", function() {
  111. equals(JSON.stringify(col), '[{"id":0,"label":"d"},{"id":1,"label":"c"},{"id":2,"label":"b"},{"id":3,"label":"a"}]');
  112. });
  113. test("Collection: Underscore methods", function() {
  114. equals(col.map(function(model){ return model.get('label'); }).join(' '), 'd c b a');
  115. equals(col.any(function(model){ return model.id === 100; }), false);
  116. equals(col.any(function(model){ return model.id === 0; }), true);
  117. equals(col.indexOf(b), 2);
  118. equals(col.size(), 4);
  119. equals(col.rest().length, 3);
  120. ok(!_.include(col.rest()), a);
  121. ok(!_.include(col.rest()), d);
  122. ok(!col.isEmpty());
  123. ok(!_.include(col.without(d)), d);
  124. equals(col.max(function(model){ return model.id; }).id, 3);
  125. equals(col.min(function(model){ return model.id; }).id, 0);
  126. same(col.chain()
  127. .filter(function(o){ return o.id % 2 === 0; })
  128. .map(function(o){ return o.id * 2; })
  129. .value(),
  130. [0, 4]);
  131. });
  132. test("Collection: refresh", function() {
  133. var refreshed = 0;
  134. var models = col.models;
  135. col.bind('refresh', function() { refreshed += 1; });
  136. col.refresh([]);
  137. equals(refreshed, 1);
  138. equals(col.length, 0);
  139. equals(col.last(), null);
  140. col.refresh(models);
  141. equals(refreshed, 2);
  142. equals(col.length, 4);
  143. equals(col.last(), a);
  144. col.refresh(_.map(models, function(m){ return m.attributes; }));
  145. equals(refreshed, 3);
  146. equals(col.length, 4);
  147. ok(col.last() !== a);
  148. ok(_.isEqual(col.last().attributes, a.attributes));
  149. });
  150. });