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.

290 lines
8.3 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 trigger core bus event for drawer close',
  99. function(assert) {
  100. assert.expect(1);
  101. this.drawer.onDrawerOpen();
  102. var d = $.Deferred();
  103. core.bus.on('drawer.closed', this, function() {
  104. assert.ok(true);
  105. d.resolve();
  106. });
  107. this.drawer.$el.trigger({type: 'drawer.closed'});
  108. return d;
  109. }
  110. );
  111. QUnit.test('It should set isOpen to false when closing',
  112. function(assert) {
  113. assert.expect(1);
  114. this.drawer.onDrawerOpen();
  115. var self = this;
  116. var d = $.Deferred();
  117. setTimeout(function() {
  118. assert.equal(self.drawer.isOpen, false);
  119. d.resolve();
  120. }, 100);
  121. this.drawer.$el.trigger({type: 'drawer.closed'});
  122. return d;
  123. }
  124. );
  125. QUnit.test('It should set isOpen to true when opening',
  126. function(assert) {
  127. assert.expect(1);
  128. this.drawer.$el.trigger({type: 'drawer.opened'});
  129. var self = this;
  130. var d = $.Deferred();
  131. setTimeout(function() {
  132. assert.ok(self.drawer.isOpen);
  133. d.resolve();
  134. }, 100);
  135. return d;
  136. }
  137. );
  138. QUnit.test('It should trigger core bus event for drawer open',
  139. function(assert) {
  140. assert.expect(1);
  141. this.drawer.onDrawerOpen();
  142. var d = $.Deferred();
  143. core.bus.on('drawer.opened', this, function() {
  144. assert.ok(true);
  145. d.resolve();
  146. });
  147. this.drawer.$el.trigger({type: 'drawer.opened'});
  148. return d;
  149. }
  150. );
  151. QUnit.test('It should choose link to right',
  152. function(assert) {
  153. assert.expect(1);
  154. this.linkGrid();
  155. var $appLink = $('#a_1'),
  156. $expect = $('#a_2'),
  157. $res = this.drawer.findAdjacentLink(
  158. $appLink, this.drawer.RIGHT
  159. );
  160. assert.equal($res[0].id, $expect[0].id);
  161. }
  162. );
  163. QUnit.test('It should choose link to left',
  164. function(assert) {
  165. assert.expect(1);
  166. this.linkGrid();
  167. var $appLink = $('#a_2'),
  168. $expect = $('#a_1'),
  169. $res = this.drawer.findAdjacentLink(
  170. $appLink, this.drawer.LEFT
  171. );
  172. assert.equal($res[0].id, $expect[0].id);
  173. }
  174. );
  175. QUnit.test('It should choose link above',
  176. function(assert) {
  177. assert.expect(1);
  178. this.linkGrid();
  179. var $appLink = $('#a_1'),
  180. $expect = $('#a_0'),
  181. $res = this.drawer.findAdjacentLink(
  182. $appLink, this.drawer.UP
  183. );
  184. assert.equal($res[0].id, $expect[0].id);
  185. }
  186. );
  187. QUnit.test('It should choose link below',
  188. function(assert) {
  189. assert.expect(1);
  190. this.linkGrid();
  191. var $appLink = $('#a_1'),
  192. $expect = $('#a_2'),
  193. $res = this.drawer.findAdjacentLink(
  194. $appLink, this.drawer.DOWN
  195. );
  196. assert.equal($res[0].id, $expect[0].id);
  197. }
  198. );
  199. QUnit.test('It should choose first link if next on last',
  200. function(assert) {
  201. assert.expect(1);
  202. this.linkGrid();
  203. var $appLink = $('#b_2'),
  204. $expect = $('#a_0'),
  205. $res = this.drawer.findAdjacentLink(
  206. $appLink, this.drawer.RIGHT
  207. );
  208. assert.equal($res[0].id, $expect[0].id);
  209. }
  210. );
  211. QUnit.test('It should choose bottom link if up on top',
  212. function(assert) {
  213. assert.expect(1);
  214. this.linkGrid();
  215. var $appLink = $('#a_0'),
  216. $expect = $('#a_2'),
  217. $res = this.drawer.findAdjacentLink(
  218. $appLink, this.drawer.UP
  219. );
  220. assert.equal($res[0].id, $expect[0].id);
  221. }
  222. );
  223. QUnit.test('It should choose top link if down on bottom',
  224. function(assert) {
  225. assert.expect(1);
  226. this.linkGrid();
  227. var $appLink = $('#a_2'),
  228. $expect = $('#a_0'),
  229. $res = this.drawer.findAdjacentLink(
  230. $appLink, this.drawer.DOWN
  231. );
  232. assert.equal($res[0].id, $expect[0].id);
  233. }
  234. );
  235. });