summaryrefslogtreecommitdiff
path: root/addons/mail/static/tests/helpers/test_utils.js
diff options
context:
space:
mode:
authorXavierDo <xdo@odoo.com>2019-02-06 12:10:36 +0000
committerXavierDo <xdo@odoo.com>2019-02-07 12:53:14 +0000
commit5c3aaacd2e5748b3f104f5ee033b442582d44c07 (patch)
treef66a0af24d0bb6ce135a85738f087cb6af915d20 /addons/mail/static/tests/helpers/test_utils.js
parent2d45df1081c568c3b306fae18b9284878756c85b (diff)
[IMP] mail: correctly patch mail status manager timers
We want to avoid that a `setTimeout` started in one test has an impact on other tests. This is especially true with the status update loop. Since mail_service is used in multiple tests, the safest solution is to patch timeouts everytime we make a call to getMailServices. There is no need for an unpatch, the patch become the default behaviour until the page is refresh at the end of tests. It is possible to manually add the patch after calling `getMailServices` ``` this.services = mailTestUtils.getMailServices(this); this.timeoutMock = mailTestUtils.patchMailTimeouts(); ``` This can be usefull if we want to get the timeoutMock to simulate time changes. Task: 1856205
Diffstat (limited to 'addons/mail/static/tests/helpers/test_utils.js')
-rw-r--r--addons/mail/static/tests/helpers/test_utils.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/addons/mail/static/tests/helpers/test_utils.js b/addons/mail/static/tests/helpers/test_utils.js
index cf63c281c58..063e3a9eb03 100644
--- a/addons/mail/static/tests/helpers/test_utils.js
+++ b/addons/mail/static/tests/helpers/test_utils.js
@@ -5,6 +5,7 @@ var BusService = require('bus.BusService');
var Discuss = require('mail.Discuss');
var MailService = require('mail.Service');
+var mailUtils = require('mail.utils');
var AbstractStorageService = require('web.AbstractStorageService');
var Class = require('web.Class');
@@ -80,6 +81,111 @@ var MockMailService = Class.extend({
});
/**
+ * Patch all the mailUtils.clearTimeout and mailUtils.setTimeout.
+ *
+ * @return {Object} helper functions, including unpatch and time management tools.
+ */
+var patchMailTimeouts = function () {
+ var currentTime = 0;
+ var timeouts = {};
+ var countTimeout = 0;
+
+ mailUtils.clearTimeout = function (id) {
+ delete timeouts[id];
+ };
+
+ mailUtils.setTimeout = function (func, duration) {
+ duration = duration || 0;
+ var executeTime = currentTime + duration;
+ countTimeout++;
+ timeouts[countTimeout] = {
+ executeTime: executeTime,
+ func: func
+ };
+ return countTimeout;
+ };
+ /**
+ * @return {integer|boolean} id of the next timeout in queue, false if queue is empty
+ */
+ function getNextTimeoutId() {
+ var minKey = false;
+ _.each(timeouts, function (value, key) {
+ if (minKey === false) {
+ minKey = Number(key);
+ return;
+ }
+ var minTime = timeouts[minKey].executeTime;
+ if (value.executeTime < minTime || (value.executeTime === minTime && key < minKey)) {
+ minKey = Number(key);
+ }
+ });
+ return minKey;
+ }
+
+ /**
+ * @return {integer|boolean} delay (time interval) before the next timeout in queue is executed.
+ * Useful to know how much time to advance to execute next timer.
+ */
+ function getNextTimeoutDelay() {
+ var next = getNextTimeoutId();
+ if (next === false) {
+ return false;
+ }
+ return timeouts[next].executeTime - currentTime;
+ }
+
+ /**
+ * Set the current time to given time
+ *
+ * @param {integer} time
+ */
+ function setTime(time) {
+ var next = getNextTimeoutId();
+ if (next !== false && timeouts[next].executeTime <= time) {
+ currentTime = timeouts[next].executeTime;
+ var func = timeouts[next].func;
+ // watch out setTimeout inside setTimeout (recursive)
+ delete timeouts[next];
+ func();
+ setTime(time);
+ }
+ else {
+ currentTime = time;
+ }
+ }
+
+ /**
+ * Add the given time to current time
+ *
+ * @param {integer} time
+ */
+ function addTime(time) {
+ setTime(currentTime + time);
+ }
+
+ /**
+ * Set time to the max time in queue and execute all timeouts before this time.
+ */
+ function runPendingTimeouts() {
+ var maxTimeInQueue = 0;
+ _.each(timeouts, function (value, key) {
+ if (value.executeTime > maxTimeInQueue) {
+ maxTimeInQueue = value.executeTime;
+ }
+ });
+ setTime(maxTimeInQueue);
+ }
+
+ return {
+ addTime: addTime,
+ getNextTimeoutDelay:getNextTimeoutDelay,
+ runPendingTimeouts: runPendingTimeouts,
+ setTime: setTime,
+ };
+};
+
+
+/**
* Returns the list of mail services required by the mail components: a
* mail_service, and its two dependencies bus_service and local_storage.
*
@@ -87,6 +193,7 @@ var MockMailService = Class.extend({
* and local_storage, in that order
*/
function getMailServices() {
+ patchMailTimeouts();
return new MockMailService().getServices();
}
@@ -94,6 +201,7 @@ return {
MockMailService: MockMailService,
createDiscuss: createDiscuss,
getMailServices: getMailServices,
+ patchMailTimeouts: patchMailTimeouts,
};
});