ribbit/test/setup.ts

40 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

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;
(global as any).TextEncoder = _window.TextEncoder || require('util').TextEncoder;
(global as any).TextDecoder = _window.TextDecoder || require('util').TextDecoder;
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 {
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>`;
}