PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/test/view.js

https://github.com/gotomypc/backbone
JavaScript | 268 lines | 235 code | 33 blank | 0 comment | 0 complexity | cbf537db8dfb59e15a35d13fa9cc0f67 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. });
  9. }
  10. });
  11. test("View: constructor", 4, function() {
  12. equal(view.el.id, 'test-view');
  13. equal(view.el.className, 'test-view');
  14. equal(view.options.id, 'test-view');
  15. equal(view.options.className, 'test-view');
  16. });
  17. test("View: jQuery", 2, function() {
  18. view.setElement(document.body);
  19. ok(view.$('#qunit-header a').get(0).innerHTML.match(/Backbone Test Suite/));
  20. ok(view.$('#qunit-header a').get(1).innerHTML.match(/Backbone Speed Suite/));
  21. });
  22. test("View: make", 3, function() {
  23. var div = view.make('div', {id: 'test-div'}, "one two three");
  24. equal(div.tagName.toLowerCase(), 'div');
  25. equal(div.id, 'test-div');
  26. equal($(div).text(), 'one two three');
  27. });
  28. test("View: make can take falsy values for content", 2, function() {
  29. var div = view.make('div', {id: 'test-div'}, 0);
  30. equal($(div).text(), '0');
  31. var div = view.make('div', {id: 'test-div'}, '');
  32. equal($(div).text(), '');
  33. });
  34. test("View: initialize", 1, function() {
  35. var View = Backbone.View.extend({
  36. initialize: function() {
  37. this.one = 1;
  38. }
  39. });
  40. var view = new View;
  41. equal(view.one, 1);
  42. });
  43. test("View: delegateEvents", 6, function() {
  44. var counter = 0;
  45. var counter2 = 0;
  46. view.setElement(document.body);
  47. view.increment = function(){ counter++; };
  48. view.$el.bind('click', function(){ counter2++; });
  49. var events = {"click #qunit-banner": "increment"};
  50. view.delegateEvents(events);
  51. $('#qunit-banner').trigger('click');
  52. equal(counter, 1);
  53. equal(counter2, 1);
  54. $('#qunit-banner').trigger('click');
  55. equal(counter, 2);
  56. equal(counter2, 2);
  57. view.delegateEvents(events);
  58. $('#qunit-banner').trigger('click');
  59. equal(counter, 3);
  60. equal(counter2, 3);
  61. });
  62. test("View: delegateEvents allows functions for callbacks", 3, function() {
  63. view.counter = 0;
  64. view.setElement("#qunit-banner");
  65. var events = {"click": function() { this.counter++; }};
  66. view.delegateEvents(events);
  67. $('#qunit-banner').trigger('click');
  68. equal(view.counter, 1);
  69. $('#qunit-banner').trigger('click');
  70. equal(view.counter, 2);
  71. view.delegateEvents(events);
  72. $('#qunit-banner').trigger('click');
  73. equal(view.counter, 3);
  74. });
  75. test("View: undelegateEvents", 6, function() {
  76. var counter = 0;
  77. var counter2 = 0;
  78. view.setElement(document.body);
  79. view.increment = function(){ counter++; };
  80. $(view.el).unbind('click');
  81. $(view.el).bind('click', function(){ counter2++; });
  82. var events = {"click #qunit-userAgent": "increment"};
  83. view.delegateEvents(events);
  84. $('#qunit-userAgent').trigger('click');
  85. equal(counter, 1);
  86. equal(counter2, 1);
  87. view.undelegateEvents();
  88. $('#qunit-userAgent').trigger('click');
  89. equal(counter, 1);
  90. equal(counter2, 2);
  91. view.delegateEvents(events);
  92. $('#qunit-userAgent').trigger('click');
  93. equal(counter, 2);
  94. equal(counter2, 3);
  95. });
  96. test("View: _ensureElement with DOM node el", 1, function() {
  97. var ViewClass = Backbone.View.extend({
  98. el: document.body
  99. });
  100. var view = new ViewClass;
  101. equal(view.el, document.body);
  102. });
  103. test("View: _ensureElement with string el", 3, function() {
  104. var ViewClass = Backbone.View.extend({
  105. el: "body"
  106. });
  107. var view = new ViewClass;
  108. strictEqual(view.el, document.body);
  109. ViewClass = Backbone.View.extend({
  110. el: "#testElement > h1"
  111. });
  112. view = new ViewClass;
  113. strictEqual(view.el, $("#testElement > h1").get(0));
  114. ViewClass = Backbone.View.extend({
  115. el: "#nonexistent"
  116. });
  117. view = new ViewClass;
  118. ok(!view.el);
  119. });
  120. test("View: with className and id functions", 2, function() {
  121. var View = Backbone.View.extend({
  122. className: function() {
  123. return 'className';
  124. },
  125. id: function() {
  126. return 'id';
  127. }
  128. });
  129. var view = new View();
  130. strictEqual(view.el.className, 'className');
  131. strictEqual(view.el.id, 'id');
  132. });
  133. test("View: with attributes", 2, function() {
  134. var view = new Backbone.View({attributes : {'class': 'one', id: 'two'}});
  135. equal(view.el.className, 'one');
  136. equal(view.el.id, 'two');
  137. });
  138. test("View: with attributes as a function", 1, function() {
  139. var viewClass = Backbone.View.extend({
  140. attributes: function() {
  141. return {'class': 'dynamic'};
  142. }
  143. });
  144. equal((new viewClass).el.className, 'dynamic');
  145. });
  146. test("View: multiple views per element", 3, function() {
  147. var count = 0, ViewClass = Backbone.View.extend({
  148. el: $("body"),
  149. events: {
  150. "click": "click"
  151. },
  152. click: function() {
  153. count++;
  154. }
  155. });
  156. var view1 = new ViewClass;
  157. $("body").trigger("click");
  158. equal(1, count);
  159. var view2 = new ViewClass;
  160. $("body").trigger("click");
  161. equal(3, count);
  162. view1.delegateEvents();
  163. $("body").trigger("click");
  164. equal(5, count);
  165. });
  166. test("View: custom events, with namespaces", 2, function() {
  167. var count = 0;
  168. var ViewClass = Backbone.View.extend({
  169. el: $('body'),
  170. events: function() {
  171. return {"fake$event.namespaced": "run"};
  172. },
  173. run: function() {
  174. count++;
  175. }
  176. });
  177. var view = new ViewClass;
  178. $('body').trigger('fake$event').trigger('fake$event');
  179. equal(count, 2);
  180. $('body').unbind('.namespaced');
  181. $('body').trigger('fake$event');
  182. equal(count, 2);
  183. });
  184. test("#1048 - setElement uses provided object.", 2, function() {
  185. var $el = $('body');
  186. var view = new Backbone.View({el: $el});
  187. ok(view.$el === $el);
  188. view.setElement($el = $($el));
  189. ok(view.$el === $el);
  190. });
  191. test("#986 - Undelegate before changing element.", 1, function() {
  192. var a = $('<button></button>');
  193. var b = $('<button></button>');
  194. var View = Backbone.View.extend({
  195. events: {click: function(e) { ok(view.el === e.target); }}
  196. });
  197. var view = new View({el: a});
  198. view.setElement(b);
  199. a.trigger('click');
  200. b.trigger('click');
  201. });
  202. test("#1172 - Clone attributes object", 2, function() {
  203. var View = Backbone.View.extend({attributes: {foo: 'bar'}});
  204. var v1 = new View({id: 'foo'});
  205. strictEqual(v1.el.id, 'foo');
  206. var v2 = new View();
  207. ok(!v2.el.id);
  208. });
  209. test("#1228 - tagName can be provided as a function", 1, function() {
  210. var View = Backbone.View.extend({tagName: function(){ return 'p'; }});
  211. ok(new View().$el.is('p'));
  212. });
  213. test("dispose", 0, function() {
  214. var View = Backbone.View.extend({
  215. events: {click: function(){ ok(false); }},
  216. initialize: function() {
  217. this.model.on('all x', function(){ ok(false); }, this);
  218. this.collection.on('all x', function(){ ok(false); }, this);
  219. }
  220. });
  221. var view = new View({
  222. model: new Backbone.Model,
  223. collection: new Backbone.Collection
  224. });
  225. view.dispose();
  226. view.model.trigger('x');
  227. view.collection.trigger('x');
  228. view.$el.click();
  229. });
  230. test("view#remove calls dispose.", 1, function() {
  231. var view = new Backbone.View();
  232. view.dispose = function() { ok(true); };
  233. view.remove();
  234. });
  235. });