Debian Codegraph @

addons/web/static/lib/hoot/core/fixture.js

raw
1 /** @odoo-module */
2
3 import { animationFrame } from "@odoo/hoot-dom";
4 import { App } from "@odoo/owl";
5 import { getActiveElement, getCurrentDimensions } from "@web/../lib/hoot-dom/helpers/dom";
6 import { setupEventActions } from "@web/../lib/hoot-dom/helpers/events";
7 import { isInstanceOf } from "@web/../lib/hoot-dom/hoot_dom_utils";
8 import { HootError } from "../hoot_utils";
9 import { subscribeToTransitionChange } from "../mock/animation";
10 import { getViewPortHeight, getViewPortWidth } from "../mock/window";
11
12 /**
13 * @typedef {Parameters<typeof import("@odoo/owl").mount>[2] & {
14 * className: string | string[];
15 * target?: import("@odoo/hoot-dom").Target;
16 * }} MountOnFixtureOptions
17 *
18 * @typedef {{
19 * component: import("@odoo/owl").ComponentConstructor;
20 * props: unknown;
21 * }} TestRootProps
22 */
23
24 //-----------------------------------------------------------------------------
25 // Global
26 //-----------------------------------------------------------------------------
27
28 const { customElements, document, getSelection, HTMLElement, Promise, WeakSet } = globalThis;
29
30 //-----------------------------------------------------------------------------
31 // Internal
32 //-----------------------------------------------------------------------------
33
34 /**
35 * @param {HTMLIFrameElement} iframe
36 */
37 function waitForIframe(iframe) {
38 return new Promise((resolve) => iframe.addEventListener("load", resolve));
39 }
40
41 const destroyed = new WeakSet();
42 let allowFixture = false;
43 /** @type {HootFixtureElement | null} */
44 let currentFixture = null;
45 let shouldPrepareNextFixture = true; // Prepare setup for first test
46
47 //-----------------------------------------------------------------------------
48 // Exports
49 //-----------------------------------------------------------------------------
50
51 /**
52 * @param {App | import("@odoo/owl").Component} target
53 */
54 export function destroy(target) {
55 const app = isInstanceOf(target, App) ? target : target.__owl__.app;
56 if (destroyed.has(app)) {
57 return;
58 }
59 destroyed.add(app);
60 app.destroy();
61 }
62
63 /**
64 * @param {import("./runner").Runner} runner
65 */
66 export function makeFixtureManager(runner) {
67 function cleanup() {
68 allowFixture = false;
69
70 if (currentFixture) {
71 shouldPrepareNextFixture = true;
72 currentFixture.remove();
73 currentFixture = null;
74 }
75 }
76
77 function getFixture() {
78 if (!allowFixture) {
79 throw new HootError(`cannot access fixture outside of a test.`);
80 }
81 if (!currentFixture) {
82 // Prepare fixture once to not force layouts/reflows
83 currentFixture = document.createElement(HootFixtureElement.TAG_NAME);
84 if (runner.debug || runner.headless) {
85 currentFixture.show();
86 }
87
88 const { width, height } = getCurrentDimensions();
89 if (width !== getViewPortWidth()) {
90 currentFixture.style.width = `${width}px`;
91 }
92 if (height !== getViewPortHeight()) {
93 currentFixture.style.height = `${height}px`;
94 }
95
96 document.body.appendChild(currentFixture);
97 }
98 return currentFixture;
99 }
100
101 async function setup() {
102 allowFixture = true;
103
104 if (shouldPrepareNextFixture) {
105 shouldPrepareNextFixture = false;
106
107 // Reset focus & selection
108 getActiveElement().blur();
109 getSelection().removeAllRanges();
110 // Wait for selectionchange events to expire before any actual testing.
111 await animationFrame();
112 }
113 }
114
115 return {
116 cleanup,
117 setup,
118 get: getFixture,
119 };
120 }
121
122 export class HootFixtureElement extends HTMLElement {
123 static CLASSES = {
124 transitions: "allow-transitions",
125 show: "show-fixture",
126 };
127 static TAG_NAME = "hoot-fixture";
128
129 static styleElement = document.createElement("style");
130
131 static {
132 customElements.define(this.TAG_NAME, this);
133
134 this.styleElement.id = "hoot-fixture-style";
135 this.styleElement.textContent = /* css */ `
136 ${this.TAG_NAME} {
137 position: fixed !important;
138 height: 100vh;
139 width: 100vw;
140 left: 50%;
141 top: 50%;
142 transform: translate(-50%, -50%);
143 opacity: 0;
144 z-index: -1;
145 }
146
147 ${this.TAG_NAME}.${this.CLASSES.show} {
148 background-color: inherit;
149 color: inherit;
150 opacity: 1;
151 z-index: 3;
152 }
153
154 ${this.TAG_NAME}:not(.${this.CLASSES.transitions}) * {
155 animation: none !important;
156 transition: none !important;
157 }
158 `;
159 }
160
161 get hasIframes() {
162 return this._iframes.size > 0;
163 }
164
165 /** @private */
166 _observer = new MutationObserver(this._onFixtureMutation.bind(this));
167 /**
168 * @private
169 * @type {Map<HTMLIFrameElement, Promise<void>>}
170 */
171 _iframes = new Map();
172
173 connectedCallback() {
174 setupEventActions(this);
175 subscribeToTransitionChange((allowTransitions) =>
176 this.classList.toggle(this.constructor.CLASSES.transitions, allowTransitions)
177 );
178
179 this._observer.observe(this, { childList: true, subtree: true });
180 this._lookForIframes();
181 }
182
183 disconnectedCallback() {
184 this._iframes.clear();
185 this._observer.disconnect();
186 }
187
188 hide() {
189 this.classList.remove(this.constructor.CLASSES.show);
190 }
191
192 async waitForIframes() {
193 await Promise.all(this._iframes.values());
194 }
195
196 show() {
197 this.classList.add(this.constructor.CLASSES.show);
198 }
199
200 /**
201 * @private
202 */
203 _lookForIframes() {
204 const toRemove = new Set(this._iframes.keys());
205 for (const iframe of this.getElementsByTagName("iframe")) {
206 if (toRemove.delete(iframe)) {
207 continue;
208 }
209 this._iframes.set(iframe, waitForIframe(iframe));
210 setupEventActions(iframe.contentWindow);
211 }
212 for (const iframe of toRemove) {
213 this._iframes.delete(iframe);
214 }
215 }
216
217 /**
218 * @private
219 * @type {MutationCallback}
220 */
221 _onFixtureMutation(mutations) {
222 if (mutations.some((mutation) => mutation.addedNodes)) {
223 this._lookForIframes();
224 }
225 }
226 }