Files
odoo_source/addons/web/static/tests/helpers/mock_env.js
T
Julien MougenotandAaron Bohy 5bcf16311a [IMP] web: View layout
This commit introduces a new component: the layout component.
Its purpose is to make abstraction of most generic components and
classes when writing a view template or an action.

To make use of this component, we also needed a new system to pass slot
from the parent of the layout to the control panel. This was done in a
hacky way and is meant to be replaced as soon as Owl provides a built-in
way to pass slots.

The control panel has also been slightly tweaked to default its props
from an environment's newly introduced key: the 'config'. This has been
done to reduce the amount of props passed to the layout and search
subcomponents.

X-original-commit: 3b29dd230acd961a72fb0cb971f9363958f65b60
Part-of: odoo/odoo#77463
Co-authored-by: Aaron Bohy <aab@odoo.com>
Co-authored-by: Julien Mougenot <jum@odoo.com>
2021-09-30 10:38:29 +00:00

118 lines
4.1 KiB
JavaScript

/** @odoo-module **/
import { registry } from "@web/core/registry";
import { makeEnv, startServices } from "@web/env";
import FormController from "web.FormController";
import { patch } from "../../src/core/utils/patch";
import { SERVICES_METADATA } from "../../src/env";
import { registerCleanup } from "./cleanup";
import { makeMockServer } from "./mock_server";
import { mocks } from "./mock_services";
import { patchWithCleanup } from "./utils";
export function clearRegistryWithCleanup(registry) {
const patch = {
content: {},
elements: null,
entries: null,
subRegistries: {},
// Preserve OnUpdate handlers
subscriptions: { UPDATE: [...registry.subscriptions.UPDATE] },
};
patchWithCleanup(registry, patch);
}
function cloneRegistryWithCleanup(registry) {
const patch = {
content: { ...registry.content },
elements: null,
entries: null,
subRegistries: {},
// Preserve OnUpdate handlers
subscriptions: { UPDATE: [...registry.subscriptions.UPDATE] },
};
patchWithCleanup(registry, patch);
}
export function clearServicesMetadataWithCleanup() {
const servicesMetadata = Object.assign({}, SERVICES_METADATA);
for (const key of Object.keys(SERVICES_METADATA)) {
delete SERVICES_METADATA[key];
}
registerCleanup(() => patch(SERVICES_METADATA, servicesMetadata));
}
export function prepareRegistriesWithCleanup() {
// Clone registries
cloneRegistryWithCleanup(registry.category("actions"));
cloneRegistryWithCleanup(registry.category("views"));
cloneRegistryWithCleanup(registry.category("error_handlers"));
cloneRegistryWithCleanup(registry.category("command_provider"));
cloneRegistryWithCleanup(registry.category("view_widgets"));
cloneRegistryWithCleanup(registry.category("fields"));
cloneRegistryWithCleanup(registry.category("main_components"));
// Clear registries
clearRegistryWithCleanup(registry.category("command_categories"));
clearRegistryWithCleanup(registry.category("debug"));
clearRegistryWithCleanup(registry.category("error_dialogs"));
clearRegistryWithCleanup(registry.category("favoriteMenu"));
clearRegistryWithCleanup(registry.category("ir.actions.report handlers"));
clearRegistryWithCleanup(registry.category("services"));
clearServicesMetadataWithCleanup();
clearRegistryWithCleanup(registry.category("systray"));
clearRegistryWithCleanup(registry.category("user_menuitems"));
clearRegistryWithCleanup(registry.category("__processed_archs__"));
// fun fact: at least one registry is missing... this shows that we need a
// better design for the way we clear these registries...
}
/**
* @typedef {import("@web/env").OdooEnv} OdooEnv
*/
/**
* Create a test environment
*
* @param {*} config
* @returns {Promise<OdooEnv>}
*/
export async function makeTestEnv(config = {}) {
// add all missing dependencies if necessary
const serviceRegistry = registry.category("services");
const servicesToProcess = serviceRegistry.getAll();
while (servicesToProcess.length) {
const service = servicesToProcess.pop();
if (service.dependencies) {
for (let depName of service.dependencies) {
if (depName in mocks && !serviceRegistry.contains(depName)) {
const dep = mocks[depName]();
serviceRegistry.add(depName, dep);
servicesToProcess.push(dep);
}
}
}
}
if (config.serverData || config.mockRPC || config.activateMockServer) {
makeMockServer(config.serverData, config.mockRPC);
}
// remove the multi-click delay for the quick edit in form views
// todo: move this elsewhere (setup?)
const initialQuickEditDelay = FormController.prototype.multiClickTime;
FormController.prototype.multiClickTime = 0;
registerCleanup(() => {
FormController.prototype.multiClickTime = initialQuickEditDelay;
});
const env = makeEnv();
env.config = config.config || {};
await startServices(env);
env.qweb.addTemplates(window.__ODOO_TEMPLATES__);
return env;
}