2026-04-28 21:39:13 -07:00
|
|
|
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;
|
2026-04-29 15:48:36 -07:00
|
|
|
(global as any).TextEncoder = _window.TextEncoder || require('util').TextEncoder;
|
|
|
|
|
(global as any).TextDecoder = _window.TextDecoder || require('util').TextDecoder;
|
2026-04-29 09:02:38 -07:00
|
|
|
|
|
|
|
|
const { TextEncoder, TextDecoder } = require('util');
|
|
|
|
|
_window.TextEncoder = TextEncoder;
|
|
|
|
|
_window.TextDecoder = TextDecoder;
|
2026-04-28 21:39:13 -07:00
|
|
|
|
|
|
|
|
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 {
|
2026-04-29 15:48:36 -07:00
|
|
|
const browserWindow = getWindow();
|
|
|
|
|
const lib = browserWindow.ribbit;
|
|
|
|
|
lib.window = browserWindow;
|
|
|
|
|
return lib;
|
2026-04-28 21:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function resetDOM(content = 'test'): void {
|
|
|
|
|
getWindow().document.body.innerHTML = `<article id="ribbit">${content}</article>`;
|
|
|
|
|
}
|