PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/view.js

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