| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { EventBus } from "@odoo/owl"; |
| 4 |
import { getCurrentDimensions, getDocument, getWindow } from "@web/../lib/hoot-dom/helpers/dom"; |
| 5 |
import { |
| 6 |
mockedCancelAnimationFrame, |
| 7 |
mockedClearInterval, |
| 8 |
mockedClearTimeout, |
| 9 |
mockedRequestAnimationFrame, |
| 10 |
mockedSetInterval, |
| 11 |
mockedSetTimeout, |
| 12 |
} from "@web/../lib/hoot-dom/helpers/time"; |
| 13 |
import { interactor } from "../../hoot-dom/hoot_dom_utils"; |
| 14 |
import { MockEventTarget, strictEqual, waitForDocument } from "../hoot_utils"; |
| 15 |
import { ensureTest, getRunner } from "../main_runner"; |
| 16 |
import { |
| 17 |
MockAnimation, |
| 18 |
mockedAnimate, |
| 19 |
mockedScroll, |
| 20 |
mockedScrollBy, |
| 21 |
mockedScrollIntoView, |
| 22 |
mockedScrollTo, |
| 23 |
mockedWindowScroll, |
| 24 |
mockedWindowScrollBy, |
| 25 |
mockedWindowScrollTo, |
| 26 |
} from "./animation"; |
| 27 |
import { MockConsole } from "./console"; |
| 28 |
import { mockCrypto } from "./crypto"; |
| 29 |
import { MockDate, MockIntl } from "./date"; |
| 30 |
import { MockClipboardItem, mockNavigator } from "./navigator"; |
| 31 |
import { |
| 32 |
MockBlob, |
| 33 |
MockBroadcastChannel, |
| 34 |
MockMessageChannel, |
| 35 |
MockMessagePort, |
| 36 |
MockRequest, |
| 37 |
MockResponse, |
| 38 |
MockSharedWorker, |
| 39 |
MockURL, |
| 40 |
MockWebSocket, |
| 41 |
MockWorker, |
| 42 |
MockXMLHttpRequest, |
| 43 |
MockXMLHttpRequestUpload, |
| 44 |
mockCookie, |
| 45 |
mockHistory, |
| 46 |
mockLocation, |
| 47 |
mockedFetch, |
| 48 |
} from "./network"; |
| 49 |
import { MockNotification } from "./notification"; |
| 50 |
import { MockStorage } from "./storage"; |
| 51 |
|
| 52 |
//----------------------------------------------------------------------------- |
| 53 |
// Global |
| 54 |
//----------------------------------------------------------------------------- |
| 55 |
|
| 56 |
const { |
| 57 |
EventTarget, |
| 58 |
HTMLAnchorElement, |
| 59 |
MutationObserver, |
| 60 |
Number: { isNaN: $isNaN, parseFloat: $parseFloat }, |
| 61 |
Object: { |
| 62 |
assign: $assign, |
| 63 |
defineProperties: $defineProperties, |
| 64 |
defineProperty: $defineProperty, |
| 65 |
entries: $entries, |
| 66 |
getOwnPropertyDescriptor: $getOwnPropertyDescriptor, |
| 67 |
getPrototypeOf: $getPrototypeOf, |
| 68 |
keys: $keys, |
| 69 |
hasOwn: $hasOwn, |
| 70 |
}, |
| 71 |
Reflect: { ownKeys: $ownKeys }, |
| 72 |
Set, |
| 73 |
WeakMap, |
| 74 |
WeakSet, |
| 75 |
} = globalThis; |
| 76 |
|
| 77 |
const { addEventListener, removeEventListener } = EventTarget.prototype; |
| 78 |
const { preventDefault } = Event.prototype; |
| 79 |
|
| 80 |
//----------------------------------------------------------------------------- |
| 81 |
// Internal |
| 82 |
//----------------------------------------------------------------------------- |
| 83 |
|
| 84 |
/** |
| 85 |
* @param {unknown} target |
| 86 |
* @param {Record<string, PropertyDescriptor>} descriptors |
| 87 |
*/ |
| 88 |
function applyPropertyDescriptors(target, descriptors) { |
| 89 |
if (!originalDescriptors.has(target)) { |
| 90 |
originalDescriptors.set(target, {}); |
| 91 |
} |
| 92 |
const targetDescriptors = originalDescriptors.get(target); |
| 93 |
const ownerDecriptors = new Map(); |
| 94 |
for (const [property, rawDescriptor] of $entries(descriptors)) { |
| 95 |
const owner = findPropertyOwner(target, property); |
| 96 |
targetDescriptors[property] = $getOwnPropertyDescriptor(owner, property); |
| 97 |
const descriptor = { ...rawDescriptor }; |
| 98 |
if ("value" in descriptor) { |
| 99 |
descriptor.writable = false; |
| 100 |
} |
| 101 |
if (!ownerDecriptors.has(owner)) { |
| 102 |
ownerDecriptors.set(owner, {}); |
| 103 |
} |
| 104 |
const nextDescriptors = ownerDecriptors.get(owner); |
| 105 |
nextDescriptors[property] = descriptor; |
| 106 |
} |
| 107 |
for (const [owner, nextDescriptors] of ownerDecriptors) { |
| 108 |
$defineProperties(owner, nextDescriptors); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param {string[]} [changedKeys] |
| 114 |
*/ |
| 115 |
function callMediaQueryChanges(changedKeys) { |
| 116 |
for (const mediaQueryList of mediaQueryLists) { |
| 117 |
if (!changedKeys || changedKeys.some((key) => mediaQueryList.media.includes(key))) { |
| 118 |
const event = new MediaQueryListEvent("change", { |
| 119 |
matches: mediaQueryList.matches, |
| 120 |
media: mediaQueryList.media, |
| 121 |
}); |
| 122 |
mediaQueryList.dispatchEvent(event); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @template T |
| 129 |
* @param {T} target |
| 130 |
* @param {keyof T} property |
| 131 |
*/ |
| 132 |
function findOriginalDescriptor(target, property) { |
| 133 |
if (originalDescriptors.has(target)) { |
| 134 |
const descriptors = originalDescriptors.get(target); |
| 135 |
if (descriptors && property in descriptors) { |
| 136 |
return descriptors[property]; |
| 137 |
} |
| 138 |
} |
| 139 |
return null; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @param {unknown} object |
| 144 |
* @param {string} property |
| 145 |
* @returns {unknown} |
| 146 |
*/ |
| 147 |
function findPropertyOwner(object, property) { |
| 148 |
if ($hasOwn(object, property)) { |
| 149 |
return object; |
| 150 |
} |
| 151 |
const prototype = $getPrototypeOf(object); |
| 152 |
if (prototype) { |
| 153 |
return findPropertyOwner(prototype, property); |
| 154 |
} |
| 155 |
return object; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param {unknown} object |
| 160 |
*/ |
| 161 |
function getTouchDescriptors(object) { |
| 162 |
const descriptors = {}; |
| 163 |
const toDelete = []; |
| 164 |
for (const eventName of TOUCH_EVENTS) { |
| 165 |
const fnName = `on${eventName}`; |
| 166 |
if (fnName in object) { |
| 167 |
const owner = findPropertyOwner(object, fnName); |
| 168 |
descriptors[fnName] = $getOwnPropertyDescriptor(owner, fnName); |
| 169 |
} else { |
| 170 |
toDelete.push(fnName); |
| 171 |
} |
| 172 |
} |
| 173 |
/** @type {({ descriptors?: Record<string, PropertyDescriptor>; toDelete?: string[]})} */ |
| 174 |
const result = {}; |
| 175 |
if ($keys(descriptors).length) { |
| 176 |
result.descriptors = descriptors; |
| 177 |
} |
| 178 |
if (toDelete.length) { |
| 179 |
result.toDelete = toDelete; |
| 180 |
} |
| 181 |
return result; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @param {typeof globalThis} view |
| 186 |
*/ |
| 187 |
function getTouchTargets(view) { |
| 188 |
return [view, view.Document.prototype]; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @param {typeof globalThis} view |
| 193 |
*/ |
| 194 |
function getWatchedEventTargets(view) { |
| 195 |
return [ |
| 196 |
view, |
| 197 |
view.document, |
| 198 |
// Permanent DOM elements |
| 199 |
view.HTMLDocument.prototype, |
| 200 |
view.HTMLBodyElement.prototype, |
| 201 |
view.HTMLHeadElement.prototype, |
| 202 |
view.HTMLHtmlElement.prototype, |
| 203 |
// Other event targets |
| 204 |
EventBus.prototype, |
| 205 |
MockEventTarget.prototype, |
| 206 |
]; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* @param {string} type |
| 211 |
* @returns {PropertyDescriptor} |
| 212 |
*/ |
| 213 |
function makeEventDescriptor(type) { |
| 214 |
let callback = null; |
| 215 |
return { |
| 216 |
enumerable: true, |
| 217 |
configurable: true, |
| 218 |
get() { |
| 219 |
return callback; |
| 220 |
}, |
| 221 |
set(value) { |
| 222 |
if (callback === value) { |
| 223 |
return; |
| 224 |
} |
| 225 |
if (typeof callback === "function") { |
| 226 |
this.removeEventListener(type, callback); |
| 227 |
} |
| 228 |
callback = value; |
| 229 |
if (typeof callback === "function") { |
| 230 |
this.addEventListener(type, callback); |
| 231 |
} |
| 232 |
}, |
| 233 |
}; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @param {string} mediaQueryString |
| 238 |
*/ |
| 239 |
function matchesQueryPart(mediaQueryString) { |
| 240 |
const [, key, value] = mediaQueryString.match(R_MEDIA_QUERY_PROPERTY) || []; |
| 241 |
let match = false; |
| 242 |
if (mockMediaValues[key]) { |
| 243 |
match = strictEqual(value, mockMediaValues[key]); |
| 244 |
} else if (key) { |
| 245 |
switch (key) { |
| 246 |
case "max-height": { |
| 247 |
match = getCurrentDimensions().height <= $parseFloat(value); |
| 248 |
break; |
| 249 |
} |
| 250 |
case "max-width": { |
| 251 |
match = getCurrentDimensions().width <= $parseFloat(value); |
| 252 |
break; |
| 253 |
} |
| 254 |
case "min-height": { |
| 255 |
match = getCurrentDimensions().height >= $parseFloat(value); |
| 256 |
break; |
| 257 |
} |
| 258 |
case "min-width": { |
| 259 |
match = getCurrentDimensions().width >= $parseFloat(value); |
| 260 |
break; |
| 261 |
} |
| 262 |
case "orientation": { |
| 263 |
const { width, height } = getCurrentDimensions(); |
| 264 |
match = value === "landscape" ? width > height : width < height; |
| 265 |
break; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
return mediaQueryString.startsWith("not") ? !match : match; |
| 270 |
} |
| 271 |
|
| 272 |
/** @type {addEventListener} */ |
| 273 |
function mockedAddEventListener(...args) { |
| 274 |
const runner = getRunner(); |
| 275 |
if (runner.dry || !runner.suiteStack.length) { |
| 276 |
// Ignore listeners during dry run or outside of a test suite |
| 277 |
return; |
| 278 |
} |
| 279 |
if (!R_OWL_SYNTHETIC_LISTENER.test(String(args[1]))) { |
| 280 |
// Ignore cleanup for Owl synthetic listeners |
| 281 |
runner.after(removeEventListener.bind(this, ...args)); |
| 282 |
} |
| 283 |
return addEventListener.call(this, ...args); |
| 284 |
} |
| 285 |
|
| 286 |
/** @type {Document["elementFromPoint"]} */ |
| 287 |
function mockedElementFromPoint(...args) { |
| 288 |
return mockedElementsFromPoint.call(this, ...args)[0]; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Mocked version of {@link document.elementsFromPoint} to: |
| 293 |
* - remove "HOOT-..." elements from the result |
| 294 |
* - put the <body> & <html> elements at the end of the list, as they may be ordered |
| 295 |
* incorrectly due to the fixture being behind the body. |
| 296 |
* @type {Document["elementsFromPoint"]} |
| 297 |
*/ |
| 298 |
function mockedElementsFromPoint(...args) { |
| 299 |
const { value: elementsFromPoint } = findOriginalDescriptor(this, "elementsFromPoint"); |
| 300 |
const result = []; |
| 301 |
let hasDocumentElement = false; |
| 302 |
let hasBody = false; |
| 303 |
for (const element of elementsFromPoint.call(this, ...args)) { |
| 304 |
if (element.tagName.startsWith("HOOT")) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
if (element === this.body) { |
| 308 |
hasBody = true; |
| 309 |
} else if (element === this.documentElement) { |
| 310 |
hasDocumentElement = true; |
| 311 |
} else { |
| 312 |
result.push(element); |
| 313 |
} |
| 314 |
} |
| 315 |
if (hasBody) { |
| 316 |
result.push(this.body); |
| 317 |
} |
| 318 |
if (hasDocumentElement) { |
| 319 |
result.push(this.documentElement); |
| 320 |
} |
| 321 |
return result; |
| 322 |
} |
| 323 |
|
| 324 |
function mockedHref() { |
| 325 |
return this.hasAttribute("href") ? new MockURL(this.getAttribute("href")).href : ""; |
| 326 |
} |
| 327 |
|
| 328 |
/** @type {typeof matchMedia} */ |
| 329 |
function mockedMatchMedia(mediaQueryString) { |
| 330 |
return new MockMediaQueryList(mediaQueryString); |
| 331 |
} |
| 332 |
|
| 333 |
function mockedPreventDefault() { |
| 334 |
preventedEvents.add(this); |
| 335 |
return preventDefault.call(this, ...arguments); |
| 336 |
} |
| 337 |
|
| 338 |
/** @type {typeof removeEventListener} */ |
| 339 |
function mockedRemoveEventListener(...args) { |
| 340 |
if (getRunner().dry) { |
| 341 |
// Ignore listeners during dry run |
| 342 |
return; |
| 343 |
} |
| 344 |
return removeEventListener.call(this, ...args); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* @param {MutationRecord[]} mutations |
| 349 |
*/ |
| 350 |
function observeAddedNodes(mutations) { |
| 351 |
const runner = getRunner(); |
| 352 |
for (const mutation of mutations) { |
| 353 |
for (const node of mutation.addedNodes) { |
| 354 |
if (runner.dry) { |
| 355 |
node.remove(); |
| 356 |
} else { |
| 357 |
runner.after(node.remove.bind(node)); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* @param {PointerEvent} ev |
| 365 |
*/ |
| 366 |
function onAnchorHrefClick(ev) { |
| 367 |
if (ev.defaultPrevented) { |
| 368 |
return; |
| 369 |
} |
| 370 |
const href = ev.target.closest("a[href]")?.href; |
| 371 |
if (!href) { |
| 372 |
return; |
| 373 |
} |
| 374 |
|
| 375 |
ev.preventDefault(); |
| 376 |
|
| 377 |
// Assign href to mock location instead of actual location |
| 378 |
mockLocation.href = href; |
| 379 |
|
| 380 |
const [, hash] = href.split("#"); |
| 381 |
if (hash) { |
| 382 |
// Scroll to the target element if the href is/has a hash |
| 383 |
getDocument().getElementById(hash)?.scrollIntoView(); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
function onWindowResize() { |
| 388 |
callMediaQueryChanges(); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* @param {typeof globalThis} view |
| 393 |
*/ |
| 394 |
function restoreTouch(view) { |
| 395 |
const touchObjects = getTouchTargets(view); |
| 396 |
for (let i = 0; i < touchObjects.length; i++) { |
| 397 |
const object = touchObjects[i]; |
| 398 |
const { descriptors, toDelete } = originalTouchFunctions[i]; |
| 399 |
if (descriptors) { |
| 400 |
$defineProperties(object, descriptors); |
| 401 |
} |
| 402 |
if (toDelete) { |
| 403 |
for (const fnName of toDelete) { |
| 404 |
delete object[fnName]; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
class MockMediaQueryList extends MockEventTarget { |
| 411 |
static publicListeners = ["change"]; |
| 412 |
|
| 413 |
get matches() { |
| 414 |
return this.media |
| 415 |
.split(R_COMMA) |
| 416 |
.some((orPart) => orPart.split(R_AND).every(matchesQueryPart)); |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* @param {string} mediaQueryString |
| 421 |
*/ |
| 422 |
constructor(mediaQueryString) { |
| 423 |
super(...arguments); |
| 424 |
|
| 425 |
this.media = mediaQueryString.trim().toLowerCase(); |
| 426 |
|
| 427 |
mediaQueryLists.add(this); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
const DEFAULT_MEDIA_VALUES = { |
| 432 |
"display-mode": "browser", |
| 433 |
pointer: "fine", |
| 434 |
"prefers-color-scheme": "light", |
| 435 |
"prefers-reduced-motion": "reduce", |
| 436 |
}; |
| 437 |
|
| 438 |
const TOUCH_EVENTS = ["touchcancel", "touchend", "touchmove", "touchstart"]; |
| 439 |
|
| 440 |
const R_AND = /\s*\band\b\s*/; |
| 441 |
const R_COMMA = /\s*,\s*/; |
| 442 |
const R_MEDIA_QUERY_PROPERTY = /\(\s*([\w-]+)\s*:\s*(.+)\s*\)/; |
| 443 |
const R_OWL_SYNTHETIC_LISTENER = /\bnativeToSyntheticEvent\b/; |
| 444 |
|
| 445 |
/** @type {WeakMap<unknown, Record<string, PropertyDescriptor>>} */ |
| 446 |
const originalDescriptors = new WeakMap(); |
| 447 |
const originalTouchFunctions = getTouchTargets(globalThis).map(getTouchDescriptors); |
| 448 |
|
| 449 |
/** @type {Set<MockMediaQueryList>} */ |
| 450 |
const mediaQueryLists = new Set(); |
| 451 |
const mockConsole = new MockConsole(); |
| 452 |
const mockLocalStorage = new MockStorage(); |
| 453 |
const mockMediaValues = { ...DEFAULT_MEDIA_VALUES }; |
| 454 |
const mockSessionStorage = new MockStorage(); |
| 455 |
/** @type {WeakSet<Event>} */ |
| 456 |
const preventedEvents = new WeakSet(); |
| 457 |
let mockTitle = ""; |
| 458 |
|
| 459 |
// Mock descriptors |
| 460 |
const ANCHOR_MOCK_DESCRIPTORS = { |
| 461 |
href: { |
| 462 |
...$getOwnPropertyDescriptor(HTMLAnchorElement.prototype, "href"), |
| 463 |
get: mockedHref, |
| 464 |
}, |
| 465 |
}; |
| 466 |
const DOCUMENT_MOCK_DESCRIPTORS = { |
| 467 |
cookie: { |
| 468 |
get: () => mockCookie.get(), |
| 469 |
set: (value) => mockCookie.set(value), |
| 470 |
}, |
| 471 |
elementFromPoint: { value: mockedElementFromPoint }, |
| 472 |
elementsFromPoint: { value: mockedElementsFromPoint }, |
| 473 |
title: { |
| 474 |
get: () => mockTitle, |
| 475 |
set: (value) => (mockTitle = value), |
| 476 |
}, |
| 477 |
}; |
| 478 |
const ELEMENT_MOCK_DESCRIPTORS = { |
| 479 |
animate: { value: mockedAnimate }, |
| 480 |
scroll: { value: mockedScroll }, |
| 481 |
scrollBy: { value: mockedScrollBy }, |
| 482 |
scrollIntoView: { value: mockedScrollIntoView }, |
| 483 |
scrollTo: { value: mockedScrollTo }, |
| 484 |
}; |
| 485 |
const WINDOW_MOCK_DESCRIPTORS = { |
| 486 |
Animation: { value: MockAnimation }, |
| 487 |
Blob: { value: MockBlob }, |
| 488 |
BroadcastChannel: { value: MockBroadcastChannel }, |
| 489 |
cancelAnimationFrame: { value: mockedCancelAnimationFrame, writable: false }, |
| 490 |
clearInterval: { value: mockedClearInterval, writable: false }, |
| 491 |
clearTimeout: { value: mockedClearTimeout, writable: false }, |
| 492 |
ClipboardItem: { value: MockClipboardItem }, |
| 493 |
console: { value: mockConsole, writable: false }, |
| 494 |
crypto: { value: mockCrypto, writable: false }, |
| 495 |
Date: { value: MockDate, writable: false }, |
| 496 |
fetch: { value: interactor("server", mockedFetch).as("fetch"), writable: false }, |
| 497 |
history: { value: mockHistory }, |
| 498 |
innerHeight: { get: () => getCurrentDimensions().height }, |
| 499 |
innerWidth: { get: () => getCurrentDimensions().width }, |
| 500 |
isSecureContext: { value: true, writable: false }, |
| 501 |
Intl: { value: MockIntl }, |
| 502 |
localStorage: { value: mockLocalStorage, writable: false }, |
| 503 |
matchMedia: { value: mockedMatchMedia }, |
| 504 |
MessageChannel: { value: MockMessageChannel }, |
| 505 |
MessagePort: { value: MockMessagePort }, |
| 506 |
navigator: { value: mockNavigator }, |
| 507 |
Notification: { value: MockNotification }, |
| 508 |
outerHeight: { get: () => getCurrentDimensions().height }, |
| 509 |
outerWidth: { get: () => getCurrentDimensions().width }, |
| 510 |
Request: { value: MockRequest, writable: false }, |
| 511 |
requestAnimationFrame: { value: mockedRequestAnimationFrame, writable: false }, |
| 512 |
Response: { value: MockResponse, writable: false }, |
| 513 |
scroll: { value: mockedWindowScroll }, |
| 514 |
scrollBy: { value: mockedWindowScrollBy }, |
| 515 |
scrollTo: { value: mockedWindowScrollTo }, |
| 516 |
sessionStorage: { value: mockSessionStorage, writable: false }, |
| 517 |
setInterval: { value: mockedSetInterval, writable: false }, |
| 518 |
setTimeout: { value: mockedSetTimeout, writable: false }, |
| 519 |
SharedWorker: { value: MockSharedWorker }, |
| 520 |
URL: { value: MockURL }, |
| 521 |
WebSocket: { value: MockWebSocket }, |
| 522 |
Worker: { value: MockWorker }, |
| 523 |
XMLHttpRequest: { value: MockXMLHttpRequest }, |
| 524 |
XMLHttpRequestUpload: { value: MockXMLHttpRequestUpload }, |
| 525 |
}; |
| 526 |
|
| 527 |
//----------------------------------------------------------------------------- |
| 528 |
// Exports |
| 529 |
//----------------------------------------------------------------------------- |
| 530 |
|
| 531 |
export function cleanupWindow() { |
| 532 |
const view = getWindow(); |
| 533 |
|
| 534 |
// Storages |
| 535 |
mockLocalStorage.clear(); |
| 536 |
mockSessionStorage.clear(); |
| 537 |
|
| 538 |
// Media |
| 539 |
mediaQueryLists.clear(); |
| 540 |
$assign(mockMediaValues, DEFAULT_MEDIA_VALUES); |
| 541 |
|
| 542 |
// Title |
| 543 |
mockTitle = ""; |
| 544 |
|
| 545 |
// Listeners |
| 546 |
view.removeEventListener("click", onAnchorHrefClick); |
| 547 |
view.removeEventListener("resize", onWindowResize); |
| 548 |
|
| 549 |
// Head & body attributes |
| 550 |
const { head, body } = view.document; |
| 551 |
for (const { name } of head.attributes) { |
| 552 |
head.removeAttribute(name); |
| 553 |
} |
| 554 |
for (const { name } of body.attributes) { |
| 555 |
body.removeAttribute(name); |
| 556 |
} |
| 557 |
|
| 558 |
// Touch |
| 559 |
restoreTouch(view); |
| 560 |
} |
| 561 |
|
| 562 |
export function getTitle() { |
| 563 |
const doc = getDocument(); |
| 564 |
const titleDescriptor = findOriginalDescriptor(doc, "title"); |
| 565 |
if (titleDescriptor) { |
| 566 |
return titleDescriptor.get.call(doc); |
| 567 |
} else { |
| 568 |
return doc.title; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
export function getViewPortHeight() { |
| 573 |
const view = getWindow(); |
| 574 |
const heightDescriptor = findOriginalDescriptor(view, "innerHeight"); |
| 575 |
if (heightDescriptor) { |
| 576 |
return heightDescriptor.get.call(view); |
| 577 |
} else { |
| 578 |
return view.innerHeight; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
export function getViewPortWidth() { |
| 583 |
const view = getWindow(); |
| 584 |
const titleDescriptor = findOriginalDescriptor(view, "innerWidth"); |
| 585 |
if (titleDescriptor) { |
| 586 |
return titleDescriptor.get.call(view); |
| 587 |
} else { |
| 588 |
return view.innerWidth; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* @param {Event} event |
| 594 |
*/ |
| 595 |
export function isPrevented(event) { |
| 596 |
return event.defaultPrevented || preventedEvents.has(event); |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* @param {Record<string, string>} name |
| 601 |
*/ |
| 602 |
export function mockMatchMedia(values) { |
| 603 |
ensureTest("mockMatchMedia"); |
| 604 |
$assign(mockMediaValues, values); |
| 605 |
|
| 606 |
callMediaQueryChanges($keys(values)); |
| 607 |
} |
| 608 |
|
| 609 |
/** |
| 610 |
* @param {Event} event |
| 611 |
*/ |
| 612 |
export function mockPreventDefault(event) { |
| 613 |
$defineProperty(event, "preventDefault", { |
| 614 |
value: mockedPreventDefault, |
| 615 |
}); |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* @param {boolean} setTouch |
| 620 |
*/ |
| 621 |
export function mockTouch(setTouch) { |
| 622 |
ensureTest("mockTouch"); |
| 623 |
const objects = getTouchTargets(getWindow()); |
| 624 |
if (setTouch) { |
| 625 |
for (const object of objects) { |
| 626 |
const descriptors = {}; |
| 627 |
for (const eventName of TOUCH_EVENTS) { |
| 628 |
const fnName = `on${eventName}`; |
| 629 |
if (!$hasOwn(object, fnName)) { |
| 630 |
descriptors[fnName] = makeEventDescriptor(eventName); |
| 631 |
} |
| 632 |
} |
| 633 |
$defineProperties(object, descriptors); |
| 634 |
} |
| 635 |
mockMatchMedia({ pointer: "coarse" }); |
| 636 |
} else { |
| 637 |
for (const object of objects) { |
| 638 |
for (const eventName of TOUCH_EVENTS) { |
| 639 |
delete object[`on${eventName}`]; |
| 640 |
} |
| 641 |
} |
| 642 |
mockMatchMedia({ pointer: "fine" }); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* @param {typeof globalThis} [view=getWindow()] |
| 648 |
*/ |
| 649 |
export function patchWindow(view = getWindow()) { |
| 650 |
// Window (doesn't need to be ready) |
| 651 |
applyPropertyDescriptors(view, WINDOW_MOCK_DESCRIPTORS); |
| 652 |
|
| 653 |
waitForDocument(view.document).then(() => { |
| 654 |
// Document |
| 655 |
applyPropertyDescriptors(view.document, DOCUMENT_MOCK_DESCRIPTORS); |
| 656 |
|
| 657 |
// Element prototypes |
| 658 |
applyPropertyDescriptors(view.Element.prototype, ELEMENT_MOCK_DESCRIPTORS); |
| 659 |
applyPropertyDescriptors(view.HTMLAnchorElement.prototype, ANCHOR_MOCK_DESCRIPTORS); |
| 660 |
}); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* @param {string} value |
| 665 |
*/ |
| 666 |
export function setTitle(value) { |
| 667 |
const doc = getDocument(); |
| 668 |
const titleDescriptor = findOriginalDescriptor(doc, "title"); |
| 669 |
if (titleDescriptor) { |
| 670 |
titleDescriptor.set.call(doc, value); |
| 671 |
} else { |
| 672 |
doc.title = value; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
export function setupWindow() { |
| 677 |
const view = getWindow(); |
| 678 |
|
| 679 |
// Listeners |
| 680 |
view.addEventListener("click", onAnchorHrefClick); |
| 681 |
view.addEventListener("resize", onWindowResize); |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
* @param {typeof globalThis} [view=getWindow()] |
| 686 |
*/ |
| 687 |
export function watchAddedNodes(view = getWindow()) { |
| 688 |
const observer = new MutationObserver(observeAddedNodes); |
| 689 |
observer.observe(view.document.head, { childList: true }); |
| 690 |
|
| 691 |
return function unwatchAddedNodes() { |
| 692 |
observer.disconnect(); |
| 693 |
}; |
| 694 |
} |
| 695 |
|
| 696 |
/** |
| 697 |
* @param {typeof globalThis} [view=getWindow()] |
| 698 |
*/ |
| 699 |
export function watchListeners(view = getWindow()) { |
| 700 |
const targets = getWatchedEventTargets(view); |
| 701 |
for (const target of targets) { |
| 702 |
target.addEventListener = mockedAddEventListener; |
| 703 |
target.removeEventListener = mockedRemoveEventListener; |
| 704 |
} |
| 705 |
|
| 706 |
return function unwatchAllListeners() { |
| 707 |
for (const target of targets) { |
| 708 |
target.addEventListener = addEventListener; |
| 709 |
target.removeEventListener = removeEventListener; |
| 710 |
} |
| 711 |
}; |
| 712 |
} |
| 713 |
|
| 714 |
/** |
| 715 |
* Returns a function checking that the given target does not contain any unexpected |
| 716 |
* key. The list of accepted keys is the initial list of keys of the target, along |
| 717 |
* with an optional `whiteList` argument. |
| 718 |
* |
| 719 |
* @template T |
| 720 |
* @param {T} target |
| 721 |
* @param {string[]} [whiteList] |
| 722 |
* @example |
| 723 |
* afterEach(watchKeys(window, ["odoo"])); |
| 724 |
*/ |
| 725 |
export function watchKeys(target, whiteList) { |
| 726 |
const acceptedKeys = new Set([...$ownKeys(target), ...(whiteList || [])]); |
| 727 |
|
| 728 |
return function checkKeys() { |
| 729 |
const keysDiff = $ownKeys(target).filter( |
| 730 |
(key) => $isNaN($parseFloat(key)) && !acceptedKeys.has(key) |
| 731 |
); |
| 732 |
for (const key of keysDiff) { |
| 733 |
const descriptor = $getOwnPropertyDescriptor(target, key); |
| 734 |
if (descriptor.configurable) { |
| 735 |
delete target[key]; |
| 736 |
} else if (descriptor.writable) { |
| 737 |
target[key] = undefined; |
| 738 |
} |
| 739 |
} |
| 740 |
}; |
| 741 |
} |