framework/js-packages/jest-config/setup-env.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-09-28 22:47:45 +08:00
import mixin from '@flarum/core/src/common/utils/mixin';
import ExportRegistry from '@flarum/core/src/common/ExportRegistry';
import jquery from 'jquery';
import m from 'mithril';
2024-09-28 22:47:45 +08:00
import dayjs from 'dayjs';
import './test-matchers';
2024-09-28 22:47:45 +08:00
import relativeTime from 'dayjs/plugin/relativeTime';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import jsdom from 'jsdom';
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
const dom = new jsdom.JSDOM('', {
pretendToBeVisual: false,
});
// Fill in the globals Mithril.js needs to operate. Also, the first two are often
// useful to have just in tests.
global.window = dom.window;
global.document = dom.window.document;
global.requestAnimationFrame = (callback) => callback();
// Some other needed pollyfills.
window.$ = jquery;
2024-09-28 23:11:50 +08:00
window.testing = true;
2024-09-28 22:47:45 +08:00
window.m = m;
window.$.fn.tooltip = () => {};
window.matchMedia = () => ({
addListener: () => {},
removeListener: () => {},
});
window.scrollTo = () => {};
// Flarum specific globals.
global.flarum = {
extensions: {},
reg: new (mixin(ExportRegistry, {
checkModule: () => true,
}))(),
};
// Prepare basic dom structure.
document.body.innerHTML = `
<div id="app">
<main class="App-content">
<div id="notices"></div>
<div id="content"></div>
</main>
</div>
`;
beforeEach(() => {
flarum.reg.clear();
});
afterAll(() => {
dom.window.close();
});