Choices/config/jsdom.js
Konstantin Vyatkin 15d54c7d34 Remove classnames and use createElement (#687)
* enable restricted syntax in tests

* add Options to global

* crearte native elements

* remove unused helper

* fix/improve typings

* add JSDoc typings

* remove if/else
2019-10-25 15:43:28 +01:00

61 lines
1.5 KiB
JavaScript

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;
function copyProps(src, target) {
const props = Object.getOwnPropertyNames(src)
.filter(prop => typeof target[prop] === 'undefined')
.map(prop => Object.getOwnPropertyDescriptor(src, prop));
Object.defineProperties(target, props);
}
function ignoreExtensions(extensions = [], returnValue = {}) {
function noop() {
return returnValue;
}
extensions.forEach(ext => {
require.extensions[ext] = noop;
});
}
function mockRAF(global) {
let callbacksQueue = [];
global.setInterval(() => {
for (let i = 0; i < callbacksQueue.length; i++) {
if (callbacksQueue[i] !== false) {
callbacksQueue[i].call(null);
}
}
callbacksQueue = [];
}, 1000 / 60);
global.requestAnimationFrame = callback => callbacksQueue.push(callback) - 1;
global.cancelAnimationFrame = id => {
callbacksQueue[id] = false;
};
}
global.window = window;
global.document = window.document;
global.navigator = {
userAgent: 'node.js',
};
global.CustomEvent = window.CustomEvent;
global.Element = window.Element;
global.HTMLElement = window.HTMLElement;
global.Option = window.Option;
global.HTMLOptionElement = window.HTMLOptionElement;
global.HTMLOptGroupElement = window.HTMLOptGroupElement;
global.DocumentFragment = window.DocumentFragment;
copyProps(window, global);
mockRAF(global);
ignoreExtensions(['.scss', '.css']);
ignoreExtensions(['.jpg', '.png', '.svg'], '');