Debian Codegraph @

addons/web/static/tests/legacy/helpers/mock_env.js

raw symbols SCIP
1 /** @odoo-module alias=@web/../tests/helpers/mock_env default=false */
2
3 import { SERVICES_METADATA } from "@web/core/utils/hooks";
4 import { registry } from "@web/core/registry";
5 import { makeEnv, startServices } from "@web/env";
6 import { registerCleanup } from "./cleanup";
7 import { makeMockServer } from "./mock_server";
8 import { mocks } from "./mock_services";
9 import { patchWithCleanup } from "./utils";
10 import { Component } from "@odoo/owl";
11 import { startRouter } from "@web/core/browser/router";
12
13 function prepareRegistry(registry, keepContent = false) {
14 const _addEventListener = registry.addEventListener.bind(registry);
15 const _removeEventListener = registry.removeEventListener.bind(registry);
16 const patch = {
17 content: keepContent ? { ...registry.content } : {},
18 elements: null,
19 entries: null,
20 subRegistries: {},
21 addEventListener(type, callback) {
22 _addEventListener(type, callback);
23 registerCleanup(() => {
24 _removeEventListener(type, callback);
25 });
26 },
27 };
28 patchWithCleanup(registry, patch);
29 }
30
31 export function clearRegistryWithCleanup(registry) {
32 prepareRegistry(registry);
33 }
34
35 function cloneRegistryWithCleanup(registry) {
36 prepareRegistry(registry, true);
37 }
38
39 export function clearServicesMetadataWithCleanup() {
40 const servicesMetadata = Object.assign({}, SERVICES_METADATA);
41 for (const key of Object.keys(SERVICES_METADATA)) {
42 delete SERVICES_METADATA[key];
43 }
44 registerCleanup(() => {
45 for (const key of Object.keys(SERVICES_METADATA)) {
46 delete SERVICES_METADATA[key];
47 }
48 Object.assign(SERVICES_METADATA, servicesMetadata);
49 });
50 }
51
52 export const registryNamesToCloneWithCleanup = [
53 "actions",
54 "command_provider",
55 "command_setup",
56 "error_handlers",
57 "fields",
58 "fields",
59 "main_components",
60 "view_widgets",
61 "views",
62 ];
63
64 export const utils = {
65 prepareRegistriesWithCleanup() {
66 // Clone registries
67 registryNamesToCloneWithCleanup.forEach((registryName) =>
68 cloneRegistryWithCleanup(registry.category(registryName))
69 );
70
71 // Clear registries
72 clearRegistryWithCleanup(registry.category("command_categories"));
73 clearRegistryWithCleanup(registry.category("debug"));
74 clearRegistryWithCleanup(registry.category("error_dialogs"));
75 clearRegistryWithCleanup(registry.category("favoriteMenu"));
76 clearRegistryWithCleanup(registry.category("ir.actions.report handlers"));
77 clearRegistryWithCleanup(registry.category("main_components"));
78
79 clearRegistryWithCleanup(registry.category("services"));
80 clearServicesMetadataWithCleanup();
81
82 clearRegistryWithCleanup(registry.category("systray"));
83 clearRegistryWithCleanup(registry.category("user_menuitems"));
84 clearRegistryWithCleanup(registry.category("kanban_examples"));
85 // fun fact: at least one registry is missing... this shows that we need a
86 // better design for the way we clear these registries...
87 },
88 };
89
90 // This is exported in a utils object to allow for patching
91 export function prepareRegistriesWithCleanup() {
92 return utils.prepareRegistriesWithCleanup();
93 }
94
95 /**
96 * @typedef {import("@web/env").OdooEnv} OdooEnv
97 */
98
99 /**
100 * Create a test environment
101 *
102 * @param {*} config
103 * @returns {Promise<OdooEnv>}
104 */
105 export async function makeTestEnv(config = {}) {
106 startRouter();
107 // add all missing dependencies if necessary
108 const serviceRegistry = registry.category("services");
109 const servicesToProcess = serviceRegistry.getAll();
110 while (servicesToProcess.length) {
111 const service = servicesToProcess.pop();
112 if (service.dependencies) {
113 for (const depName of service.dependencies) {
114 if (depName in mocks && !serviceRegistry.contains(depName)) {
115 const dep = mocks[depName]();
116 serviceRegistry.add(depName, dep);
117 servicesToProcess.push(dep);
118 }
119 }
120 }
121 }
122
123 if (config.serverData || config.mockRPC || config.activateMockServer) {
124 await makeMockServer(config.serverData, config.mockRPC);
125 }
126
127 let env = makeEnv();
128 await startServices(env);
129 Component.env = env;
130 if ("config" in config) {
131 env = Object.assign(Object.create(env), { config: config.config });
132 }
133 return env;
134 }
135
136 /**
137 * Create a test environment for dialog tests
138 *
139 * @param {*} config
140 * @returns {Promise<OdooEnv>}
141 */
142 export async function makeDialogTestEnv(config = {}) {
143 const env = await makeTestEnv(config);
144 env.dialogData = {
145 isActive: true,
146 close() {},
147 };
148 return env;
149 }