31 lines
920 B
TypeScript
31 lines
920 B
TypeScript
|
|
import { Window } from 'happy-dom';
|
||
|
|
import * as fs from 'fs';
|
||
|
|
import * as path from 'path';
|
||
|
|
|
||
|
|
let _window: any;
|
||
|
|
|
||
|
|
export function getWindow(): any {
|
||
|
|
if (!_window) {
|
||
|
|
_window = new Window({ url: 'http://localhost' });
|
||
|
|
(global as any).window = _window;
|
||
|
|
(global as any).document = _window.document;
|
||
|
|
(global as any).HTMLElement = _window.HTMLElement;
|
||
|
|
(global as any).Node = _window.Node;
|
||
|
|
(global as any).NodeFilter = _window.NodeFilter;
|
||
|
|
|
||
|
|
const bundle = fs.readFileSync(
|
||
|
|
path.join(__dirname, '..', 'dist', 'ribbit', 'ribbit.js'), 'utf8'
|
||
|
|
);
|
||
|
|
_window.eval(bundle.replace('var ribbit =', 'window.ribbit ='));
|
||
|
|
}
|
||
|
|
return _window;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function ribbit(): any {
|
||
|
|
return getWindow().ribbit;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function resetDOM(content = 'test'): void {
|
||
|
|
getWindow().document.body.innerHTML = `<article id="ribbit">${content}</article>`;
|
||
|
|
}
|