Debian Codegraph @

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

raw symbols SCIP
1 /** @odoo-module alias=@web/../tests/helpers/mount_in_fixture default=false**/
2
3 import { App, Component, xml } from "@odoo/owl";
4 import { registerCleanup } from "@web/../tests/helpers/cleanup";
5 import { makeTestEnv } from "@web/../tests/helpers/mock_env";
6 import { mocks } from "@web/../tests/helpers/mock_services";
7 import { MainComponentsContainer } from "@web/core/main_components_container";
8 import { registry } from "@web/core/registry";
9 import { getTemplate } from "@web/core/templates";
10
11 class TestComponent extends Component {
12 static props = {
13 components: { type: Array },
14 };
15
16 static template = xml`
17 <t t-foreach="props.components" t-as="comp" t-key="comp.component.name">
18 <t t-component="comp.component" t-props="comp.props"/>
19 </t>
20 `;
21
22 /**
23 * Returns the instance of the first component.
24 * @returns {Component}
25 */
26 get defaultComponent() {
27 return this.__owl__.bdom.children[0].child.component;
28 }
29 }
30
31 function getApp(env, props) {
32 const appConfig = {
33 env,
34 getTemplate,
35 test: true,
36 props: props,
37 };
38 if (env.services && "localization" in env.services) {
39 appConfig.translateFn = env._t;
40 }
41 const app = new App(TestComponent, appConfig);
42 registerCleanup(() => app.destroy());
43 return app;
44 }
45
46 /**
47 * @typedef {Object} Config
48 * @property {Object} env
49 * @property {Object} props
50 * @property {string[]} templates
51 */
52
53 /**
54 * This functions will mount the given component and
55 * will add a MainComponentsContainer if the overlay
56 * service is loaded.
57 *
58 * @template T
59 * @param {new (...args: any[]) => T} Comp
60 * @param {HTMLElement} target
61 * @param {Config} config
62 * @returns {Promise<T>} Instance of Comp
63 */
64 export async function mountInFixture(Comp, target, config = {}) {
65 const serviceRegistry = registry.category("services");
66
67 let env = config.env || {};
68 const isEnvInitialized = env && env.services;
69
70 function isServiceRegistered(serviceName) {
71 return isEnvInitialized
72 ? serviceName in env.services
73 : serviceRegistry.contains(serviceName);
74 }
75
76 async function addService(serviceName, service) {
77 if (isServiceRegistered(serviceName)) {
78 return;
79 }
80
81 service = typeof service === "function" ? service() : service;
82 if (isEnvInitialized) {
83 env.services[serviceName] = await service.start(env);
84 } else {
85 serviceRegistry.add(serviceName, service);
86 }
87 }
88
89 const components = [{ component: Comp, props: config.props || {} }];
90 if (isServiceRegistered("overlay")) {
91 await addService("localization", mocks.localization);
92 components.push({ component: MainComponentsContainer, props: {} });
93 }
94
95 if (!isEnvInitialized) {
96 env = await makeTestEnv(env);
97 }
98
99 const app = getApp(env, { components });
100
101 if (config.templates) {
102 app.addTemplates(config.templates);
103 }
104
105 const testComp = await app.mount(target);
106 return testComp.defaultComponent;
107 }