You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
9.1 KiB

  1. /* global QUnit */
  2. /* Copyright 2016 LasLabs Inc.
  3. * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */
  4. odoo.define('web_responsive.test', function(require) {
  5. "use strict";
  6. var core = require('web.core');
  7. var responsive = require('web_responsive');
  8. QUnit.module('web_responsive', {
  9. beforeEach: function() {
  10. var $el = $(
  11. '<div class="drawer drawer--left">' +
  12. '<header role="banner">' +
  13. '<button class="drawer-toggle">' +
  14. '<span class="drawer-hamburger-icon"/>' +
  15. '</button>' +
  16. '<nav class="drawer-nav">' +
  17. '<ul class="drawer-menu">' +
  18. '<li class="drawer-menu-item"/>' +
  19. '</ul>' +
  20. '</nav>' +
  21. '<div class="panel-title" id="appDrawerAppPanelHead"></div>' +
  22. '</header>' +
  23. '<main role="main"></main>' +
  24. '<a class="oe_menu_leaf"/>' +
  25. '<div>' +
  26. '<div class="o_sub_menu_content"></div>' +
  27. '</div>' +
  28. '<div class="dropdown-scrollable"></div>' +
  29. '</div>'
  30. );
  31. this.$clickZone = $el.find('a.oe_menu_leaf');
  32. this.$secondaryMenu = $el.find('div.o_sub_menu_content').parent();
  33. this.$dropdown = $el.find('div.dropdown-scrollable');
  34. this.document = $("#qunit-fixture");
  35. this.document.append($el);
  36. this.drawer = new responsive.AppDrawer();
  37. },
  38. linkGrid: function() {
  39. for(var i = 0; i < 3; i++){
  40. this.drawer.$el.append(
  41. $('<div class="row">').append(
  42. $('<a class="col-md-6" id="a_' + i + '"><span class="app-drawer-icon-app /></a>' +
  43. '<a class="col-md-6" id="b_' + i + '"><span class="app-drawer-icon-app /></a>'
  44. )
  45. )
  46. );
  47. this.drawer.$appLinks = this.drawer.$el.find('a.col-md-6');
  48. }
  49. }
  50. });
  51. QUnit.test('It should set initialized after success init',
  52. function(assert) {
  53. assert.expect(1);
  54. assert.ok(this.drawer.initialized);
  55. }
  56. );
  57. QUnit.test('It should close drawer after click on clickZone',
  58. function(assert) {
  59. assert.expect(1);
  60. this.$clickZone.click();
  61. var self = this;
  62. var d = $.Deferred();
  63. setTimeout(function() {
  64. assert.ok(self.drawer.$el.hasClass('drawer-close'));
  65. d.resolve();
  66. }, 100);
  67. return d;
  68. }
  69. );
  70. QUnit.test('It should collapse open secondary menus during handleClickZones',
  71. function(assert) {
  72. assert.expect(1);
  73. this.$clickZone.click();
  74. var self = this;
  75. var d = $.Deferred();
  76. setTimeout(function() {
  77. assert.equal(self.$secondaryMenu.attr('aria-expanded'), 'false');
  78. d.resolve();
  79. }, 200);
  80. return d;
  81. }
  82. );
  83. QUnit.test('It should update max-height on scrollable dropdowns',
  84. function(assert) {
  85. assert.expect(1);
  86. this.drawer.handleWindowResize();
  87. var height = $(window).height() * this.drawer.dropdownHeightFactor;
  88. var actual = parseFloat(this.$dropdown.css('max-height'));
  89. var pass = Math.abs(actual - height) < 0.001;
  90. assert.pushResult({
  91. result: pass,
  92. actual: actual,
  93. expect: height,
  94. message: ''
  95. });
  96. }
  97. );
  98. QUnit.test('It should return keybuffer + new key',
  99. function(assert) {
  100. assert.expect(1);
  101. this.drawer.keyBuffer = 'TES';
  102. var res = this.drawer.handleKeyBuffer(84);
  103. assert.equal(res, 'TEST');
  104. }
  105. );
  106. QUnit.test('It should clear keybuffer after timeout',
  107. function(assert) {
  108. assert.expect(1);
  109. this.drawer.keyBuffer = 'TES';
  110. this.drawer.keyBufferTime = 10;
  111. this.drawer.handleKeyBuffer(84);
  112. var self = this;
  113. var d = $.Deferred();
  114. setTimeout(function() {
  115. assert.equal(self.drawer.keyBuffer, "");
  116. d.resolve();
  117. }, 100);
  118. return d;
  119. }
  120. );
  121. QUnit.test('It should trigger core bus event for drawer close',
  122. function(assert) {
  123. assert.expect(1);
  124. this.drawer.onDrawerOpen();
  125. var d = $.Deferred();
  126. core.bus.on('drawer.closed', this, function() {
  127. assert.ok(true);
  128. d.resolve();
  129. });
  130. this.drawer.$el.trigger({type: 'drawer.closed'});
  131. return d;
  132. }
  133. );
  134. QUnit.test('It should set isOpen to false when closing',
  135. function(assert) {
  136. assert.expect(1);
  137. this.drawer.onDrawerOpen();
  138. var self = this;
  139. var d = $.Deferred();
  140. setTimeout(function() {
  141. assert.equal(self.drawer.isOpen, false);
  142. d.resolve();
  143. }, 100);
  144. this.drawer.$el.trigger({type: 'drawer.closed'});
  145. return d;
  146. }
  147. );
  148. QUnit.test('It should set isOpen to true when opening',
  149. function(assert) {
  150. assert.expect(1);
  151. this.drawer.$el.trigger({type: 'drawer.opened'});
  152. var self = this;
  153. var d = $.Deferred();
  154. setTimeout(function() {
  155. assert.ok(self.drawer.isOpen);
  156. d.resolve();
  157. }, 100);
  158. return d;
  159. }
  160. );
  161. QUnit.test('It should trigger core bus event for drawer open',
  162. function(assert) {
  163. assert.expect(1);
  164. this.drawer.onDrawerOpen();
  165. var d = $.Deferred();
  166. core.bus.on('drawer.opened', this, function() {
  167. assert.ok(true);
  168. d.resolve();
  169. });
  170. this.drawer.$el.trigger({type: 'drawer.opened'});
  171. return d;
  172. }
  173. );
  174. QUnit.test('It should choose link to right',
  175. function(assert) {
  176. assert.expect(1);
  177. this.linkGrid();
  178. var $appLink = $('#a_1'),
  179. $expect = $('#a_2'),
  180. $res = this.drawer.findAdjacentAppLink(
  181. $appLink, this.drawer.RIGHT
  182. );
  183. assert.equal($res[0].id, $expect[0].id);
  184. }
  185. );
  186. QUnit.test('It should choose link to left',
  187. function(assert) {
  188. assert.expect(1);
  189. this.linkGrid();
  190. var $appLink = $('#a_2'),
  191. $expect = $('#a_1'),
  192. $res = this.drawer.findAdjacentAppLink(
  193. $appLink, this.drawer.LEFT
  194. );
  195. assert.equal($res[0].id, $expect[0].id);
  196. }
  197. );
  198. QUnit.test('It should choose link above',
  199. function(assert) {
  200. assert.expect(1);
  201. this.linkGrid();
  202. var $appLink = $('#a_1'),
  203. $expect = $('#a_0'),
  204. $res = this.drawer.findAdjacentAppLink(
  205. $appLink, this.drawer.UP
  206. );
  207. assert.equal($res[0].id, $expect[0].id);
  208. }
  209. );
  210. QUnit.test('It should choose link below',
  211. function(assert) {
  212. assert.expect(1);
  213. this.linkGrid();
  214. var $appLink = $('#a_1'),
  215. $expect = $('#a_2'),
  216. $res = this.drawer.findAdjacentAppLink(
  217. $appLink, this.drawer.DOWN
  218. );
  219. assert.equal($res[0].id, $expect[0].id);
  220. }
  221. );
  222. QUnit.test('It should choose first link if next on last',
  223. function(assert) {
  224. assert.expect(1);
  225. this.linkGrid();
  226. var $appLink = $('#b_2'),
  227. $expect = $('#a_0'),
  228. $res = this.drawer.findAdjacentAppLink(
  229. $appLink, this.drawer.RIGHT
  230. );
  231. assert.equal($res[0].id, $expect[0].id);
  232. }
  233. );
  234. QUnit.test('It should choose bottom link if up on top',
  235. function(assert) {
  236. assert.expect(1);
  237. this.linkGrid();
  238. var $appLink = $('#a_0'),
  239. $expect = $('#a_2'),
  240. $res = this.drawer.findAdjacentAppLink(
  241. $appLink, this.drawer.UP
  242. );
  243. assert.equal($res[0].id, $expect[0].id);
  244. }
  245. );
  246. QUnit.test('It should choose top link if down on bottom',
  247. function(assert) {
  248. assert.expect(1);
  249. this.linkGrid();
  250. var $appLink = $('#a_2'),
  251. $expect = $('#a_0'),
  252. $res = this.drawer.findAdjacentAppLink(
  253. $appLink, this.drawer.DOWN
  254. );
  255. assert.equal($res[0].id, $expect[0].id);
  256. }
  257. );
  258. });