From 3933dac0c7ff154e24f3068e8a1a55dd65f3b720 Mon Sep 17 00:00:00 2001 From: "Michael (mcm)" Date: Thu, 11 May 2023 14:38:34 +0000 Subject: [PATCH] [REF] web: remove top level await This commit removes two top level await in test files and reorganises a bit the tests setup as the module loading sequence has changed. The commit also converts the modules that contained these top level await to esm. The goal of removing async modules is to simplify the module loader in the future. task id: 3265979 Part-of: odoo/odoo#121185 --- addons/web/static/tests/helpers/legacy.js | 12 --- addons/web/static/tests/helpers/session.js | 2 +- .../static/tests/legacy/helpers/test_utils.js | 39 +++---- addons/web/static/tests/setup.js | 100 +++++++++--------- addons/web/static/tests/webclient/helpers.js | 4 - 5 files changed, 66 insertions(+), 91 deletions(-) delete mode 100644 addons/web/static/tests/helpers/legacy.js diff --git a/addons/web/static/tests/helpers/legacy.js b/addons/web/static/tests/helpers/legacy.js deleted file mode 100644 index ce5150644e2..00000000000 --- a/addons/web/static/tests/helpers/legacy.js +++ /dev/null @@ -1,12 +0,0 @@ -odoo.define( - "web.test_legacy", - ["web.session", "web.SessionOverrideForTests", "web.test_utils"], - async (require) => { - require("web.SessionOverrideForTests"); - require("web.test_utils"); - const session = require("web.session"); - await session.is_bound; // await for templates from server - - return { legacyProm: session.is_bound }; - } -); diff --git a/addons/web/static/tests/helpers/session.js b/addons/web/static/tests/helpers/session.js index 984a7b77026..b861f07e076 100644 --- a/addons/web/static/tests/helpers/session.js +++ b/addons/web/static/tests/helpers/session.js @@ -1,4 +1,4 @@ -/** @odoo-module alias=web.SessionOverrideForTests **/ +/** @odoo-module **/ // Override the Session.session_reload function // The wowl test infrastructure does set a correct odoo global value before each test diff --git a/addons/web/static/tests/legacy/helpers/test_utils.js b/addons/web/static/tests/legacy/helpers/test_utils.js index 5ae95570792..130615fe345 100644 --- a/addons/web/static/tests/legacy/helpers/test_utils.js +++ b/addons/web/static/tests/legacy/helpers/test_utils.js @@ -1,5 +1,4 @@ -odoo.define('web.test_utils', ["web.session", "web.test_utils_create", "web.test_utils_control_panel", "web.test_utils_dom", "web.test_utils_fields", "web.test_utils_file", "web.test_utils_form", "web.test_utils_graph", "web.test_utils_kanban", "web.test_utils_mock", "web.test_utils_modal", "web.test_utils_pivot", "web.tools"], async function (require) { - "use strict"; +/** @odoo-module alias=web.test_utils **/ /** * Test Utils @@ -10,19 +9,18 @@ odoo.define('web.test_utils', ["web.session", "web.test_utils_create", "web.test * instance of a view, appended in the dom, ready to be tested. */ - const session = require('web.session'); - const testUtilsCreate = require('web.test_utils_create'); - const testUtilsControlPanel = require('web.test_utils_control_panel'); - const testUtilsDom = require('web.test_utils_dom'); - const testUtilsFields = require('web.test_utils_fields'); - const testUtilsFile = require('web.test_utils_file'); - const testUtilsForm = require('web.test_utils_form'); - const testUtilsGraph = require('web.test_utils_graph'); - const testUtilsKanban = require('web.test_utils_kanban'); - const testUtilsMock = require('web.test_utils_mock'); - const testUtilsModal = require('web.test_utils_modal'); - const testUtilsPivot = require('web.test_utils_pivot'); - const tools = require('web.tools'); + import testUtilsCreate from "web.test_utils_create"; + import testUtilsControlPanel from "web.test_utils_control_panel"; + import testUtilsDom from "web.test_utils_dom"; + import testUtilsFields from "web.test_utils_fields"; + import testUtilsFile from "web.test_utils_file"; + import testUtilsForm from "web.test_utils_form"; + import testUtilsGraph from "web.test_utils_graph"; + import testUtilsKanban from "web.test_utils_kanban"; + import testUtilsMock from "web.test_utils_mock"; + import testUtilsModal from "web.test_utils_modal"; + import testUtilsPivot from "web.test_utils_pivot"; + import tools from "web.tools"; function deprecated(fn, type) { @@ -113,15 +111,7 @@ odoo.define('web.test_utils', ["web.session", "web.test_utils_create", "web.test return testUtilsDom.returnAfterNextAnimationFrame(); } - // Loading static files cannot be properly simulated when their real content is - // really needed. This is the case for static XML files so we load them here, - // before starting the qunit test suite. - // (session.js is in charge of loading the static xml bundle and we also have - // to load xml files that are normally lazy loaded by specific widgets). - // Assets can also contain static xml files. They are loaded when the session - // is launched. - await session.is_bound; - return { + export default { mock: { addMockEnvironment: testUtilsMock.addMockEnvironment, addMockEnvironmentOwl: testUtilsMock.addMockEnvironmentOwl, @@ -266,4 +256,3 @@ odoo.define('web.test_utils', ["web.session", "web.test_utils_create", "web.test triggerPositionalMouseEvent: deprecated(testUtilsDom.triggerPositionalMouseEvent, 'dom'), unpatch: deprecated(testUtilsMock.unpatch, 'mock'), }; -}); diff --git a/addons/web/static/tests/setup.js b/addons/web/static/tests/setup.js index bf7e7ef81b3..716b9f77755 100644 --- a/addons/web/static/tests/setup.js +++ b/addons/web/static/tests/setup.js @@ -7,7 +7,6 @@ import { assets, templates } from "@web/core/assets"; import { browser, makeRAMLocalStorage } from "@web/core/browser/browser"; import { nextTick, patchTimeZone, patchWithCleanup } from "@web/../tests/helpers/utils"; import { memoize } from "@web/core/utils/functions"; -import { legacyProm } from "web.test_legacy"; import { registerCleanup } from "./helpers/cleanup"; import { utils } from "./helpers/mock_env"; import { session as sessionInfo } from "@web/session"; @@ -19,6 +18,7 @@ transitionConfig.disabled = true; import { patch } from "@web/core/utils/patch"; import { App, whenReady } from "@odoo/owl"; import { currencies } from "@web/core/currency"; +import "./helpers/session"; const { prepareRegistriesWithCleanup } = utils; @@ -300,59 +300,52 @@ function removeUnwantedAttrsFromTemplates(attrs) { } } -// alt attribute causes issues with scroll tests. Indeed, alt is -// displayed between the time we scroll programatically and the time -// we assert for the scroll position. The src attribute is removed -// as well to make sure images won't trigger a GET request on the -// server. - -// Clean up templates that have already been added. -removeUnwantedAttrsFromTemplates(["alt", "src"]); - -const { loadXML, getBundle, loadJS, loadCSS } = assets; -patch(assets, "TestAssetsLoadXML", { - loadXML: function (templates) { - console.log("%c[assets] fetch XML ressource", "color: #66e; font-weight: bold;"); - // Clean up new templates that might be added later. - loadXML(templates); - removeUnwantedAttrsFromTemplates(["alt", "src"]); - }, - getBundle: memoize(async function (xmlID) { - console.log( - "%c[assets] fetch libs from xmlID: " + xmlID, - "color: #66e; font-weight: bold;" - ); - return getBundle(xmlID); - }), - loadJS: memoize(async function (ressource) { - if (ressource.match(/\/static(\/\S+\/|\/)libs?/)) { +function patchAssets() { + const { loadXML, getBundle, loadJS, loadCSS } = assets; + patch(assets, "TestAssetsLoadXML", { + loadXML: function (templates) { + console.log("%c[assets] fetch XML ressource", "color: #66e; font-weight: bold;"); + // Clean up new templates that might be added later. + loadXML(templates); + removeUnwantedAttrsFromTemplates(["alt", "src"]); + }, + getBundle: memoize(async function (xmlID) { console.log( - "%c[assets] fetch (mock) JS ressource: " + ressource, + "%c[assets] fetch libs from xmlID: " + xmlID, "color: #66e; font-weight: bold;" ); - return nextTick(); - } - console.log( - "%c[assets] fetch JS ressource: " + ressource, - "color: #66e; font-weight: bold;" - ); - return loadJS(ressource); - }), - loadCSS: memoize(async function (ressource) { - if (ressource.match(/\/static(\/\S+\/|\/)libs?/)) { + return getBundle(xmlID); + }), + loadJS: memoize(async function (ressource) { + if (ressource.match(/\/static(\/\S+\/|\/)libs?/)) { + console.log( + "%c[assets] fetch (mock) JS ressource: " + ressource, + "color: #66e; font-weight: bold;" + ); + return nextTick(); + } console.log( - "%c[assets] fetch (mock) CSS ressource: " + ressource, + "%c[assets] fetch JS ressource: " + ressource, "color: #66e; font-weight: bold;" ); - return nextTick(); - } - console.log( - "%c[assets] fetch CSS ressource: " + ressource, - "color: #66e; font-weight: bold;" - ); - return loadCSS(ressource); - }), -}); + return loadJS(ressource); + }), + loadCSS: memoize(async function (ressource) { + if (ressource.match(/\/static(\/\S+\/|\/)libs?/)) { + console.log( + "%c[assets] fetch (mock) CSS ressource: " + ressource, + "color: #66e; font-weight: bold;" + ); + return nextTick(); + } + console.log( + "%c[assets] fetch CSS ressource: " + ressource, + "color: #66e; font-weight: bold;" + ); + return loadCSS(ressource); + }), + }); +} export async function setupTests() { // uncomment to debug memory leaks in qunit suite @@ -389,7 +382,16 @@ export async function setupTests() { patchOwlApp(); }); - await Promise.all([whenReady(), legacyProm]); + await Promise.all([whenReady(), session.is_bound]); + + // alt attribute causes issues with scroll tests. Indeed, alt is + // displayed between the time we scroll programatically and the time + // we assert for the scroll position. The src attribute is removed + // as well to make sure images won't trigger a GET request on the + // server. + // Clean up templates that have already been added. + removeUnwantedAttrsFromTemplates(["alt", "src"]); + patchAssets(); // make sure images do not trigger a GET on the server new MutationObserver((mutations) => { diff --git a/addons/web/static/tests/webclient/helpers.js b/addons/web/static/tests/webclient/helpers.js index 36139719f6f..6095b6d13fd 100644 --- a/addons/web/static/tests/webclient/helpers.js +++ b/addons/web/static/tests/webclient/helpers.js @@ -19,10 +19,6 @@ import { effectService } from "@web/core/effects/effect_service"; import { hotkeyService } from "@web/core/hotkeys/hotkey_service"; import { menuService } from "@web/webclient/menus/menu_service"; import { WebClient } from "@web/webclient/webclient"; -// This import is needed because of it's sideeffects, for exemple : -// web.test_utils easyload xml templates at line : 124:130. -// Also it set the autocomplete delay time for the field Many2One at 0 for the tests at line : 132:137 -import "web.test_legacy"; import AbstractService from "web.AbstractService"; import ActionMenus from "web.ActionMenus"; import basicFields from "web.basic_fields";