[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).
This commit is contained in:
Aaron Bohy
2017-12-04 10:01:22 +01:00
parent 5fd72753c6
commit 595a405b76
2 changed files with 26 additions and 15 deletions
@@ -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;
});
/**
+26 -3
View File
@@ -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) {