Skip to content

Commit

Permalink
fix(tests): reset defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jun 6, 2019
1 parent 560d322 commit de5f3d1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/mock-doc/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ const sessionStorageMap = new WeakMap<MockWindow, MockStorage>();
const eventClassMap = new WeakMap<MockWindow, any>();
const customEventClassMap = new WeakMap<MockWindow, any>();
const keyboardEventClassMap = new WeakMap<MockWindow, any>();

const nativeClearInterval = clearInterval;
const nativeClearTimeout = clearTimeout;
const nativeSetInterval = setInterval;
const nativeSetTimeout = setTimeout;
const nativeURL = URL;

export class MockWindow {
__clearInterval: typeof nativeClearInterval;
__clearTimeout: typeof nativeClearTimeout;
__setInterval: typeof nativeSetInterval;
__setTimeout: typeof nativeSetTimeout;
__maxTimeout: number;
__allowInterval: boolean;
URL: typeof URL;

console: Console;
customElements: CustomElementRegistry;
document: Document;
Expand Down Expand Up @@ -50,6 +62,7 @@ export class MockWindow {
this.performance = new MockPerformance();
this.customElements = new MockCustomElementRegistry(this as any);
this.console = createConsole();
resetWindowDefaults(this);
resetWindowDimensions(this);
}

Expand Down Expand Up @@ -337,19 +350,17 @@ export class MockWindow {
get window() {
return this;
}

URL = URL;

// used so the native setTimeout can actually be used
// but allows us to monkey patch the window.setTimeout
__clearInterval = clearInterval;
__clearTimeout = clearTimeout;
__setInterval = setInterval;
__setTimeout = setTimeout;
__maxTimeout = 30000;
__allowInterval = true;
}

function resetWindowDefaults(win: any) {
win.__clearInterval = nativeClearInterval;
win.__clearTimeout = nativeClearTimeout;
win.__setInterval = nativeSetInterval;
win.__setTimeout = nativeSetTimeout;
win.__maxTimeout = 30000;
win.__allowInterval = true;
win.URL = nativeURL;
}

export function createWindow(html: string | boolean = null): Window {
return new MockWindow(html) as any;
Expand Down Expand Up @@ -403,7 +414,7 @@ export function resetWindow(win: Window) {
delete (win as any)[key];
}
}

resetWindowDefaults(win);
resetWindowDimensions(win);

resetEventListeners(win);
Expand Down
31 changes: 31 additions & 0 deletions src/runtime/test/globals.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component } from "@stencil/core";
import { newSpecPage } from "@stencil/core/testing";


describe('globals', () => {

@Component({
tag: 'cmp-a'
})
class CmpA {}

it('should mock json fetch, no input', async () => {
const page = await newSpecPage({
components: [CmpA],
html: `<cmp-a></cmp-a>`,
autoApplyChanges: true
});
await new Promise((resolve) => {
requestAnimationFrame(() => {
page.win.requestAnimationFrame(() => {
setTimeout(() => {
page.win.setTimeout(() => {
resolve();
}, 10);
}, 10);
});
});
});
});

});

0 comments on commit de5f3d1

Please sign in to comment.