Skip to content

Commit de5f3d1

Browse files
committed
fix(tests): reset defaults
1 parent 560d322 commit de5f3d1

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

src/mock-doc/window.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@ const sessionStorageMap = new WeakMap<MockWindow, MockStorage>();
2020
const eventClassMap = new WeakMap<MockWindow, any>();
2121
const customEventClassMap = new WeakMap<MockWindow, any>();
2222
const keyboardEventClassMap = new WeakMap<MockWindow, any>();
23-
23+
const nativeClearInterval = clearInterval;
24+
const nativeClearTimeout = clearTimeout;
25+
const nativeSetInterval = setInterval;
26+
const nativeSetTimeout = setTimeout;
27+
const nativeURL = URL;
2428

2529
export class MockWindow {
30+
__clearInterval: typeof nativeClearInterval;
31+
__clearTimeout: typeof nativeClearTimeout;
32+
__setInterval: typeof nativeSetInterval;
33+
__setTimeout: typeof nativeSetTimeout;
34+
__maxTimeout: number;
35+
__allowInterval: boolean;
36+
URL: typeof URL;
37+
2638
console: Console;
2739
customElements: CustomElementRegistry;
2840
document: Document;
@@ -50,6 +62,7 @@ export class MockWindow {
5062
this.performance = new MockPerformance();
5163
this.customElements = new MockCustomElementRegistry(this as any);
5264
this.console = createConsole();
65+
resetWindowDefaults(this);
5366
resetWindowDimensions(this);
5467
}
5568

@@ -337,19 +350,17 @@ export class MockWindow {
337350
get window() {
338351
return this;
339352
}
340-
341-
URL = URL;
342-
343-
// used so the native setTimeout can actually be used
344-
// but allows us to monkey patch the window.setTimeout
345-
__clearInterval = clearInterval;
346-
__clearTimeout = clearTimeout;
347-
__setInterval = setInterval;
348-
__setTimeout = setTimeout;
349-
__maxTimeout = 30000;
350-
__allowInterval = true;
351353
}
352354

355+
function resetWindowDefaults(win: any) {
356+
win.__clearInterval = nativeClearInterval;
357+
win.__clearTimeout = nativeClearTimeout;
358+
win.__setInterval = nativeSetInterval;
359+
win.__setTimeout = nativeSetTimeout;
360+
win.__maxTimeout = 30000;
361+
win.__allowInterval = true;
362+
win.URL = nativeURL;
363+
}
353364

354365
export function createWindow(html: string | boolean = null): Window {
355366
return new MockWindow(html) as any;
@@ -403,7 +414,7 @@ export function resetWindow(win: Window) {
403414
delete (win as any)[key];
404415
}
405416
}
406-
417+
resetWindowDefaults(win);
407418
resetWindowDimensions(win);
408419

409420
resetEventListeners(win);

src/runtime/test/globals.spec.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Component } from "@stencil/core";
2+
import { newSpecPage } from "@stencil/core/testing";
3+
4+
5+
describe('globals', () => {
6+
7+
@Component({
8+
tag: 'cmp-a'
9+
})
10+
class CmpA {}
11+
12+
it('should mock json fetch, no input', async () => {
13+
const page = await newSpecPage({
14+
components: [CmpA],
15+
html: `<cmp-a></cmp-a>`,
16+
autoApplyChanges: true
17+
});
18+
await new Promise((resolve) => {
19+
requestAnimationFrame(() => {
20+
page.win.requestAnimationFrame(() => {
21+
setTimeout(() => {
22+
page.win.setTimeout(() => {
23+
resolve();
24+
}, 10);
25+
}, 10);
26+
});
27+
});
28+
});
29+
});
30+
31+
});

0 commit comments

Comments
 (0)