From 595a405b767eed34f90d48e5f04dff338cc34c2f Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 1 Dec 2017 11:12:20 +0100 Subject: [PATCH] [IMP] web: tests: patch _.debounce and _.throttle ... to allow them to be called several times without having to wait for the delays. This was already done for _.throttle, by default for the whole test suite, and it couldn't be parameterized in individual tests. This was enough for _.throttle, but the same solution couldn't be applied for _.debounce, because some tests actually test that some handlers are correctly debounced. Those function are now patched in addMockEnvironment, and two parameters can be set: 'debounce' (default: true) and 'throttle' (default: false). --- .../web/static/tests/helpers/qunit_config.js | 12 -------- addons/web/static/tests/helpers/test_utils.js | 29 +++++++++++++++++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/addons/web/static/tests/helpers/qunit_config.js b/addons/web/static/tests/helpers/qunit_config.js index 8d2b287ab88..641d3a6f09b 100644 --- a/addons/web/static/tests/helpers/qunit_config.js +++ b/addons/web/static/tests/helpers/qunit_config.js @@ -40,17 +40,6 @@ QUnit.config.hidepassed = (window.location.href.match(/[?&]testId=/) === null); var sortButtonAppended = false; -/** - * We override the _.throttle function to avoid the delay in order to be able to - * chain multiple actions using throttle in the same test. - */ -QUnit.begin(function () { - this._initialThrottle = _.throttle; - var self = this; - _.throttle = function (func, wait, options) { - return self._initialThrottle(func, 0, options); - }; -}); /** * This is the way the testing framework knows that tests passed or failed. It * only look in the phantomJS console and check if there is a ok or an error. @@ -67,7 +56,6 @@ QUnit.done(function(result) { if (!sortButtonAppended) { _addSortButton(); } - _.throttle = this._initialThrottle; }); /** diff --git a/addons/web/static/tests/helpers/test_utils.js b/addons/web/static/tests/helpers/test_utils.js index 0643b3e6a6b..37a98de1746 100644 --- a/addons/web/static/tests/helpers/test_utils.js +++ b/addons/web/static/tests/helpers/test_utils.js @@ -228,6 +228,11 @@ function createAsyncView(params) { * up in the init process of the view, because there are no other way to do it * after this method returns. Some events ('call_service', "load_views", * "get_session", "load_filters") have a special treatment beforehand. + * @param {boolean} [debounce=true] set to false to completely remove the + * debouncing, forcing the handler to be called directly (not on the next + * execution stack, like it does with delay=0). + * @param {boolean} [throttle=false] set to true to keep the throttling, which + * is completely removed by default. * * @returns {MockServer} the instance of the mock server, created by this * function. It is necessary for createAsyncView so that method can call some @@ -250,9 +255,9 @@ function addMockEnvironment(widget, params) { debug: params.debug, }); // make sure the debounce value for input fields is set to 0 - var initialDebounce = DebouncedField.prototype.DEBOUNCE; + var initialDebounceValue = DebouncedField.prototype.DEBOUNCE; DebouncedField.prototype.DEBOUNCE = params.fieldDebounce || 0; - var initialSession, initialConfig, initialParameters; + var initialSession, initialConfig, initialParameters, initialDebounce, initialThrottle; initialSession = _.extend({}, session); session.getTZOffset = function () { return 0; // by default, but may be overriden in specific tests @@ -274,6 +279,18 @@ function addMockEnvironment(widget, params) { initialParameters = _.extend({}, core._t.database.parameters); _.extend(core._t.database.parameters, params.translateParameters); } + if (params.debounce === false) { + initialDebounce = _.debounce; + _.debounce = function (func) { + return func; + }; + } + if (!('throttle' in params) || !params.throttle) { + initialThrottle = _.throttle; + _.throttle = function (func) { + return func; + }; + } var widgetDestroy = widget.destroy; widget.destroy = function () { @@ -281,7 +298,13 @@ function addMockEnvironment(widget, params) { // widget is destroyed, at the end of each test to avoid collisions core.bus.trigger('clear_cache'); - DebouncedField.prototype.DEBOUNCE = initialDebounce; + DebouncedField.prototype.DEBOUNCE = initialDebounceValue; + if (params.debounce === false) { + _.debounce = initialDebounce; + } + if (!('throttle' in params) || !params.throttle) { + _.throttle = initialThrottle; + } var key; if ('session' in params) {