forked from web-infra-dev/rstest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
38 lines (37 loc) · 1.36 KB
/
Copy pathsetup.ts
File metadata and controls
38 lines (37 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Mirrors the jsdom fixture's setup-spy.ts but for happy-dom. happy-dom
* exposes the same Element/HTMLElement/Node globals jsdom does, so the
* vendor-style focus monkey-patch + descriptor-history recording is
* portable. softResetEnv's protoSnapshot path handles both env names.
*/
if (typeof HTMLElement !== 'undefined') {
const preDescriptor = Object.getOwnPropertyDescriptor(
HTMLElement.prototype,
'focus',
);
(
globalThis as { __soft_focus_descriptor_history__?: Array<string> }
).__soft_focus_descriptor_history__ ??= [];
(
globalThis as { __soft_focus_descriptor_history__?: Array<string> }
).__soft_focus_descriptor_history__!.push(
!preDescriptor
? 'missing'
: 'value' in preDescriptor && typeof preDescriptor.value === 'function'
? 'value'
: preDescriptor.get && !preDescriptor.set
? 'get-only'
: 'other',
);
const originalFocus = HTMLElement.prototype.focus;
Object.defineProperty(HTMLElement.prototype, 'focus', {
configurable: true,
get: () =>
function patchedFocus(this: HTMLElement, options?: FocusOptions) {
(globalThis as { __soft_focus_calls__?: number }).__soft_focus_calls__ =
((globalThis as { __soft_focus_calls__?: number })
.__soft_focus_calls__ ?? 0) + 1;
return originalFocus.call(this, options);
},
});
}