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.

318 lines
9.0 KiB

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