From 653b03055356a343f5f8f29d8d9701898c4d517a Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Tue, 26 Apr 2022 10:02:31 +0000 Subject: [PATCH] [MOV] mail: move advanceTime function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR prepares the ground for the new env in discuss. The addTimeControlToEnv function has been move in its own file, it will not rely on env anymore. task-2582313 closes odoo/odoo#89673 Signed-off-by: Sébastien Theys (seb) --- addons/mail/static/src/env/test_env.js | 65 ------------------ .../mail/static/tests/helpers/test_utils.js | 6 +- .../mail/static/tests/helpers/time_control.js | 66 +++++++++++++++++++ 3 files changed, 68 insertions(+), 69 deletions(-) create mode 100644 addons/mail/static/tests/helpers/time_control.js diff --git a/addons/mail/static/src/env/test_env.js b/addons/mail/static/src/env/test_env.js index 8a50bbfd60f..9b83ffabf31 100644 --- a/addons/mail/static/src/env/test_env.js +++ b/addons/mail/static/src/env/test_env.js @@ -1,7 +1,5 @@ /** @odoo-module **/ -import { nextTick } from '@mail/utils/utils'; - /** * @param {Object} [providedEnv={}] * @returns {Object} @@ -25,66 +23,3 @@ export function addMessagingToEnv(providedEnv = {}) { }); return env; } - -/** - * @param {Object} [providedEnv={}] - * @returns {Object} - */ -export function addTimeControlToEnv(providedEnv = {}) { - const env = { ...providedEnv }; - if (!env.browser) { - env.browser = {}; - } - // list of timeout ids that have timed out. - let timedOutIds = []; - // key: timeoutId, value: func + remaining duration - const timeouts = new Map(); - Object.assign(env.browser, { - clearTimeout: id => { - timeouts.delete(id); - timedOutIds = timedOutIds.filter(i => i !== id); - }, - setTimeout: (func, duration) => { - const timeoutId = _.uniqueId('timeout_'); - const timeout = { - id: timeoutId, - isTimedOut: false, - func, - duration, - }; - timeouts.set(timeoutId, timeout); - if (duration === 0) { - timedOutIds.push(timeoutId); - timeout.isTimedOut = true; - } - return timeoutId; - }, - }); - if (!env.testUtils) { - env.testUtils = {}; - } - Object.assign(env.testUtils, { - advanceTime: async duration => { - await nextTick(); - for (const id of timeouts.keys()) { - const timeout = timeouts.get(id); - if (timeout.isTimedOut) { - continue; - } - timeout.duration = Math.max(timeout.duration - duration, 0); - if (timeout.duration === 0) { - timedOutIds.push(id); - } - } - while (timedOutIds.length > 0) { - const id = timedOutIds.shift(); - const timeout = timeouts.get(id); - timeouts.delete(id); - timeout.func(); - await nextTick(); - } - await nextTick(); - }, - }); - return env; -} diff --git a/addons/mail/static/tests/helpers/test_utils.js b/addons/mail/static/tests/helpers/test_utils.js index a024a17f732..6cfcb9b944c 100644 --- a/addons/mail/static/tests/helpers/test_utils.js +++ b/addons/mail/static/tests/helpers/test_utils.js @@ -3,10 +3,7 @@ import BusService from 'bus.BusService'; import { MessagingMenuContainer } from '@mail/components/messaging_menu_container/messaging_menu_container'; -import { - addMessagingToEnv, - addTimeControlToEnv, -} from '@mail/env/test_env'; +import { addMessagingToEnv } from '@mail/env/test_env'; import { insertAndReplace, replace } from '@mail/model/model_field_command'; import { ChatWindowService } from '@mail/services/chat_window_service/chat_window_service'; import { MessagingService } from '@mail/services/messaging/messaging'; @@ -15,6 +12,7 @@ import { DialogService } from '@mail/services/dialog_service/dialog_service'; import { getMessagingComponent } from '@mail/utils/messaging_component'; import { nextTick } from '@mail/utils/utils'; import { DiscussWidget } from '@mail/widgets/discuss/discuss'; +import { addTimeControlToEnv } from '@mail/../tests/helpers/time_control'; import core from 'web.core'; import AbstractStorageService from 'web.AbstractStorageService'; diff --git a/addons/mail/static/tests/helpers/time_control.js b/addons/mail/static/tests/helpers/time_control.js new file mode 100644 index 00000000000..bcd50809cef --- /dev/null +++ b/addons/mail/static/tests/helpers/time_control.js @@ -0,0 +1,66 @@ +/** @odoo-module **/ + +import { nextTick } from '@mail/utils/utils'; + +/** + * @param {Object} [providedEnv={}] + * @returns {Object} + */ + export function addTimeControlToEnv(providedEnv = {}) { + const env = { ...providedEnv }; + if (!env.browser) { + env.browser = {}; + } + // list of timeout ids that have timed out. + let timedOutIds = []; + // key: timeoutId, value: func + remaining duration + const timeouts = new Map(); + Object.assign(env.browser, { + clearTimeout: id => { + timeouts.delete(id); + timedOutIds = timedOutIds.filter(i => i !== id); + }, + setTimeout: (func, duration) => { + const timeoutId = _.uniqueId('timeout_'); + const timeout = { + id: timeoutId, + isTimedOut: false, + func, + duration, + }; + timeouts.set(timeoutId, timeout); + if (duration === 0) { + timedOutIds.push(timeoutId); + timeout.isTimedOut = true; + } + return timeoutId; + }, + }); + if (!env.testUtils) { + env.testUtils = {}; + } + Object.assign(env.testUtils, { + advanceTime: async duration => { + await nextTick(); + for (const id of timeouts.keys()) { + const timeout = timeouts.get(id); + if (timeout.isTimedOut) { + continue; + } + timeout.duration = Math.max(timeout.duration - duration, 0); + if (timeout.duration === 0) { + timedOutIds.push(id); + } + } + while (timedOutIds.length > 0) { + const id = timedOutIds.shift(); + const timeout = timeouts.get(id); + timeouts.delete(id); + timeout.func(); + await nextTick(); + } + await nextTick(); + }, + }); + return env; +}