From 6eb223829499142379ef52082f664b3ef25296fe Mon Sep 17 00:00:00 2001 From: Lucas Perais Date: Mon, 25 Jul 2022 10:05:19 +0000 Subject: [PATCH] [FIX] tests: views: helpers: the view's env is not the global env Before this commit in tests, when creating a view with makeView, the same env was shared for services, view and main_components_container. This is rather wrong as bugs in prod could not be reproduced in tests. For example: env.config was present in the env for every services and components, this is wrong as only View must receive it in its env. After this commit, makeView separates the env for services and top-level components from the one dedicated for View. Part-of: odoo/odoo#96618 --- addons/web/static/tests/helpers/mock_env.js | 12 +++++++----- addons/web/static/tests/search/helpers.js | 5 +++-- addons/web/static/tests/search/with_search_tests.js | 1 + addons/web/static/tests/views/helpers.js | 5 +++-- addons/web/static/tests/views/layout_tests.js | 5 +++-- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/addons/web/static/tests/helpers/mock_env.js b/addons/web/static/tests/helpers/mock_env.js index 7433fe0e098..00696b41476 100644 --- a/addons/web/static/tests/helpers/mock_env.js +++ b/addons/web/static/tests/helpers/mock_env.js @@ -53,7 +53,7 @@ function prepareRegistriesWithCleanup() { cloneRegistryWithCleanup(registry.category("command_setup")); cloneRegistryWithCleanup(registry.category("view_widgets")); cloneRegistryWithCleanup(registry.category("fields")); - cloneRegistryWithCleanup(registry.category('wowlToLegacyServiceMappers')); + cloneRegistryWithCleanup(registry.category("wowlToLegacyServiceMappers")); cloneRegistryWithCleanup(registry.category("main_components")); cloneRegistryWithCleanup(registry.category("fields")); @@ -64,7 +64,7 @@ function prepareRegistriesWithCleanup() { clearRegistryWithCleanup(registry.category("error_dialogs")); clearRegistryWithCleanup(registry.category("favoriteMenu")); clearRegistryWithCleanup(registry.category("ir.actions.report handlers")); - clearRegistryWithCleanup(registry.category('wowlToLegacyServiceMappers')); + clearRegistryWithCleanup(registry.category("wowlToLegacyServiceMappers")); clearRegistryWithCleanup(registry.category("services")); clearServicesMetadataWithCleanup(); @@ -122,9 +122,11 @@ export async function makeTestEnv(config = {}) { FormController.prototype.multiClickTime = initialQuickEditDelay; }); - const env = makeEnv(); - owl.Component.env = env; - env.config = config.config || {}; + let env = makeEnv(); await startServices(env); + owl.Component.env = env; + if ("config" in config) { + env = Object.assign(Object.create(env), { config: config.config }); + } return env; } diff --git a/addons/web/static/tests/search/helpers.js b/addons/web/static/tests/search/helpers.js index ee1faf90e5b..3c282ef9040 100644 --- a/addons/web/static/tests/search/helpers.js +++ b/addons/web/static/tests/search/helpers.js @@ -78,8 +78,9 @@ export async function makeWithSearch(params) { `; Parent.components = { Component: params.Component, WithSearch }; - const env = await makeTestEnv({ serverData, mockRPC, config }); - const parent = await mount(Parent, getFixture(), { env, props }); + const env = await makeTestEnv({ serverData, mockRPC }); + const searchEnv = Object.assign(Object.create(env), { config }); + const parent = await mount(Parent, getFixture(), { env: searchEnv, props }); const parentNode = parent.__owl__; const withSearchNode = getUniqueChild(parentNode); const componentNode = getUniqueChild(withSearchNode); diff --git a/addons/web/static/tests/search/with_search_tests.js b/addons/web/static/tests/search/with_search_tests.js index 2cb8f966ce9..afd7a1d12aa 100644 --- a/addons/web/static/tests/search/with_search_tests.js +++ b/addons/web/static/tests/search/with_search_tests.js @@ -322,6 +322,7 @@ QUnit.module("Search", (hooks) => { class Parent extends Component { setup() { + owl.useSubEnv({ config: {} }); this.searchState = useState({ resModel: "animal", domain: [["type", "=", "carnivorous"]], diff --git a/addons/web/static/tests/views/helpers.js b/addons/web/static/tests/views/helpers.js index 0b9495260f5..a78f7b1d8b0 100644 --- a/addons/web/static/tests/views/helpers.js +++ b/addons/web/static/tests/views/helpers.js @@ -61,7 +61,7 @@ export async function makeView(params) { delete props.searchViewArch; } - const env = await makeTestEnv({ serverData, mockRPC, config }); + const env = await makeTestEnv({ serverData, mockRPC }); Object.assign(env, createDebugContext(env)); // This is needed if the views are in debug mode /** Legacy Environment, for compatibility sakes @@ -81,7 +81,8 @@ export async function makeView(params) { await addLegacyMockEnvironment(env, legacyParams); const target = getFixture(); - const view = await mount(View, target, { env, props }); + const viewEnv = Object.assign(Object.create(env), { config }); + const view = await mount(View, target, { env: viewEnv, props }); await mount(MainComponentsContainer, target, { env, props }); const viewNode = view.__owl__; diff --git a/addons/web/static/tests/views/layout_tests.js b/addons/web/static/tests/views/layout_tests.js index dcc5a303533..3578b21279c 100644 --- a/addons/web/static/tests/views/layout_tests.js +++ b/addons/web/static/tests/views/layout_tests.js @@ -58,8 +58,9 @@ QUnit.module("Views", (hooks) => { `; ToyComponent.components = { Layout }; - const env = await makeTestEnv({ config: {} }); - await mount(ToyComponent, getFixture(), { env }); + const env = await makeTestEnv(); + const toyEnv = Object.assign(Object.create(env), { config: {} }); + await mount(ToyComponent, getFixture(), { env: toyEnv }); assert.containsOnce(target, ".o_view_sample_data"); assert.containsNone(target, ".o_control_panel");