Skip to content

Commit

Permalink
Updated withDom to support jsdom@9.x.x and jsdom@10.x.x and greater.
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmonkey authored and indexzero committed Aug 21, 2017
1 parent da91407 commit 23aeea7
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions packages/enzyme/withDom.js
@@ -1,11 +1,9 @@
require('raf/polyfill');

if (!global.document) {
try {
const jsdom = require('jsdom').jsdom; // could throw

global.document = jsdom('');
global.window = document.defaultView;
function initGlobal(document, window) {
global.document = document;
global.window = window;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
global[property] = document.defaultView[property];
Expand All @@ -15,6 +13,28 @@ if (!global.document) {
global.navigator = {
userAgent: 'node.js',
};
}

function createV9jsdom(jsdom) {
const document = jsdom('');
initGlobal(document, document.defaultView);
}

function createV10jsdom(JSDOM) {
const { window: { document } } = new JSDOM('');
initGlobal(document, window);
}

try {
const jsdomPackage = require('jsdom');
const jsdom = jsdomPackage.jsdom;
const JSDOM = jsdomPackage.JSDOM;

if(jsdom && typeof jsdom === 'function'){
createV9jsdom(jsdom);
} else if (JSDOM && typeof JSDOM === 'function') {
createV10jsdom(JSDOM);
}
} catch (e) {
// jsdom is not supported...
if (e.message === "Cannot find module 'jsdom'") {
Expand Down

0 comments on commit 23aeea7

Please sign in to comment.