PageRenderTime 61ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/view.js

https://bitbucket.org/dlisboa/backbone
JavaScript | 350 lines | 279 code | 71 blank | 0 comment | 0 complexity | 3d39b9aa2f774f7728680f6f1e7f3eed MD5 | raw file
  1. $(document).ready(function() {
  2. var view;
  3. module("Backbone.View", {
  4. setup: function() {
  5. view = new Backbone.View({
  6. id : 'test-view',
  7. className : 'test-view',
  8. other : 'non-special-option'
  9. });
  10. }
  11. });
  12. test("constructor", 6, function() {
  13. equal(view.el.id, 'test-view');
  14. equal(view.el.className, 'test-view');
  15. equal(view.el.other, void 0);
  16. equal(view.options.id, 'test-view');
  17. equal(view.options.className, 'test-view');
  18. equal(view.options.other, 'non-special-option');
  19. });
  20. test("jQuery", 1, function() {
  21. var view = new Backbone.View;
  22. view.setElement('<p><a><b>test</b></a></p>');
  23. strictEqual(view.$('a b').html(), 'test');
  24. });
  25. test("initialize", 1, function() {
  26. var View = Backbone.View.extend({
  27. initialize: function() {
  28. this.one = 1;
  29. }
  30. });
  31. strictEqual(new View().one, 1);
  32. });
  33. test("delegateEvents", 6, function() {
  34. var counter1 = 0, counter2 = 0;
  35. var view = new Backbone.View({el: '<p><a id="test"></a></p>'});
  36. view.increment = function(){ counter1++; };
  37. view.$el.on('click', function(){ counter2++; });
  38. var events = {'click #test': 'increment'};
  39. view.delegateEvents(events);
  40. view.$('#test').trigger('click');
  41. equal(counter1, 1);
  42. equal(counter2, 1);
  43. view.$('#test').trigger('click');
  44. equal(counter1, 2);
  45. equal(counter2, 2);
  46. view.delegateEvents(events);
  47. view.$('#test').trigger('click');
  48. equal(counter1, 3);
  49. equal(counter2, 3);
  50. });
  51. test("delegateEvents allows functions for callbacks", 3, function() {
  52. var view = new Backbone.View({el: '<p></p>'});
  53. view.counter = 0;
  54. var events = {
  55. click: function() {
  56. this.counter++;
  57. }
  58. };
  59. view.delegateEvents(events);
  60. view.$el.trigger('click');
  61. equal(view.counter, 1);
  62. view.$el.trigger('click');
  63. equal(view.counter, 2);
  64. view.delegateEvents(events);
  65. view.$el.trigger('click');
  66. equal(view.counter, 3);
  67. });
  68. test("undelegateEvents", 6, function() {
  69. var counter1 = 0, counter2 = 0;
  70. var view = new Backbone.View({el: '<p><a id="test"></a></p>'});
  71. view.increment = function(){ counter1++; };
  72. view.$el.on('click', function(){ counter2++; });
  73. var events = {'click #test': 'increment'};
  74. view.delegateEvents(events);
  75. view.$('#test').trigger('click');
  76. equal(counter1, 1);
  77. equal(counter2, 1);
  78. view.undelegateEvents();
  79. view.$('#test').trigger('click');
  80. equal(counter1, 1);
  81. equal(counter2, 2);
  82. view.delegateEvents(events);
  83. view.$('#test').trigger('click');
  84. equal(counter1, 2);
  85. equal(counter2, 3);
  86. });
  87. test("_ensureElement with DOM node el", 1, function() {
  88. var View = Backbone.View.extend({
  89. el: document.body
  90. });
  91. equal(new View().el, document.body);
  92. });
  93. test("_ensureElement with string el", 3, function() {
  94. var View = Backbone.View.extend({
  95. el: "body"
  96. });
  97. strictEqual(new View().el, document.body);
  98. View = Backbone.View.extend({
  99. el: "#testElement > h1"
  100. });
  101. strictEqual(new View().el, $("#testElement > h1").get(0));
  102. View = Backbone.View.extend({
  103. el: "#nonexistent"
  104. });
  105. ok(!new View().el);
  106. });
  107. test("with className and id functions", 2, function() {
  108. var View = Backbone.View.extend({
  109. className: function() {
  110. return 'className';
  111. },
  112. id: function() {
  113. return 'id';
  114. }
  115. });
  116. strictEqual(new View().el.className, 'className');
  117. strictEqual(new View().el.id, 'id');
  118. });
  119. test("with options function", 3, function() {
  120. var View1 = Backbone.View.extend({
  121. options: function() {
  122. return {
  123. title: 'title1',
  124. acceptText: 'confirm'
  125. };
  126. }
  127. });
  128. var View2 = View1.extend({
  129. options: function() {
  130. return _.extend(View1.prototype.options.call(this), {
  131. title: 'title2',
  132. fixed: true
  133. });
  134. }
  135. });
  136. strictEqual(new View2().options.title, 'title2');
  137. strictEqual(new View2().options.acceptText, 'confirm');
  138. strictEqual(new View2().options.fixed, true);
  139. });
  140. test("with attributes", 2, function() {
  141. var View = Backbone.View.extend({
  142. attributes: {
  143. id: 'id',
  144. 'class': 'class'
  145. }
  146. });
  147. strictEqual(new View().el.className, 'class');
  148. strictEqual(new View().el.id, 'id');
  149. });
  150. test("with attributes as a function", 1, function() {
  151. var View = Backbone.View.extend({
  152. attributes: function() {
  153. return {'class': 'dynamic'};
  154. }
  155. });
  156. strictEqual(new View().el.className, 'dynamic');
  157. });
  158. test("multiple views per element", 3, function() {
  159. var count = 0;
  160. var $el = $('<p></p>');
  161. var View = Backbone.View.extend({
  162. el: $el,
  163. events: {
  164. click: function() {
  165. count++;
  166. }
  167. }
  168. });
  169. var view1 = new View;
  170. $el.trigger("click");
  171. equal(1, count);
  172. var view2 = new View;
  173. $el.trigger("click");
  174. equal(3, count);
  175. view1.delegateEvents();
  176. $el.trigger("click");
  177. equal(5, count);
  178. });
  179. test("custom events, with namespaces", 2, function() {
  180. var count = 0;
  181. var View = Backbone.View.extend({
  182. el: $('body'),
  183. events: function() {
  184. return {"fake$event.namespaced": "run"};
  185. },
  186. run: function() {
  187. count++;
  188. }
  189. });
  190. var view = new View;
  191. $('body').trigger('fake$event').trigger('fake$event');
  192. equal(count, 2);
  193. $('body').unbind('.namespaced');
  194. $('body').trigger('fake$event');
  195. equal(count, 2);
  196. });
  197. test("#1048 - setElement uses provided object.", 2, function() {
  198. var $el = $('body');
  199. var view = new Backbone.View({el: $el});
  200. ok(view.$el === $el);
  201. view.setElement($el = $($el));
  202. ok(view.$el === $el);
  203. });
  204. test("#986 - Undelegate before changing element.", 1, function() {
  205. var button1 = $('<button></button>');
  206. var button2 = $('<button></button>');
  207. var View = Backbone.View.extend({
  208. events: {
  209. click: function(e) {
  210. ok(view.el === e.target);
  211. }
  212. }
  213. });
  214. var view = new View({el: button1});
  215. view.setElement(button2);
  216. button1.trigger('click');
  217. button2.trigger('click');
  218. });
  219. test("#1172 - Clone attributes object", 2, function() {
  220. var View = Backbone.View.extend({
  221. attributes: {foo: 'bar'}
  222. });
  223. var view1 = new View({id: 'foo'});
  224. strictEqual(view1.el.id, 'foo');
  225. var view2 = new View();
  226. ok(!view2.el.id);
  227. });
  228. test("#1228 - tagName can be provided as a function", 1, function() {
  229. var View = Backbone.View.extend({
  230. tagName: function() {
  231. return 'p';
  232. }
  233. });
  234. ok(new View().$el.is('p'));
  235. });
  236. test("views stopListening", 0, function() {
  237. var View = Backbone.View.extend({
  238. initialize: function() {
  239. this.listenTo(this.model, 'all x', function(){ ok(false); }, this);
  240. this.listenTo(this.collection, 'all x', function(){ ok(false); }, this);
  241. }
  242. });
  243. var view = new View({
  244. model: new Backbone.Model,
  245. collection: new Backbone.Collection
  246. });
  247. view.stopListening();
  248. view.model.trigger('x');
  249. view.collection.trigger('x');
  250. });
  251. test("Provide function for el.", 1, function() {
  252. var View = Backbone.View.extend({
  253. el: function() {
  254. return "<p><a></a></p>";
  255. }
  256. });
  257. var view = new View;
  258. ok(view.$el.is('p:has(a)'));
  259. });
  260. test("events passed in options", 2, function() {
  261. var counter = 0;
  262. var View = Backbone.View.extend({
  263. el: '<p><a id="test"></a></p>',
  264. increment: function() {
  265. counter++;
  266. }
  267. });
  268. var view = new View({events:{'click #test':'increment'}});
  269. var view2 = new View({events:function(){
  270. return {'click #test':'increment'};
  271. }});
  272. view.$('#test').trigger('click');
  273. view2.$('#test').trigger('click');
  274. equal(counter, 2);
  275. view.$('#test').trigger('click');
  276. view2.$('#test').trigger('click');
  277. equal(counter, 4);
  278. });
  279. });