Skip to content

Commit

Permalink
Merge pull request #921 from kenju/master
Browse files Browse the repository at this point in the history
[docs] Update "jsdom" guides for v10 and later
  • Loading branch information
nfcampos authored May 18, 2017
2 parents ca73244 + 8ba4246 commit f3bbfd3
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions docs/guides/jsdom.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,52 @@ gets run *before* React's code is run.

As a result, a standalone script like the one below is generally a good approach:

`jsdom v10~`:

```js
/* setup.js */

var jsdom = require('jsdom').jsdom;
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);
}

global.window = window;
global.document = window.document;
global.navigator = {
userAgent: 'node.js'
};
copyProps(window, global);
```

Here is the sample of [jsdom old API](https://github.com/tmpvar/jsdom/blob/master/lib/old-api.md) as well.

`jsdom ~<v10`:

```js
/* setup.js */

const jsdom = require('jsdom').jsdom;

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
global[property] = document.defaultView[property];
}
});

global.navigator = {
userAgent: 'node.js'
};

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);
}
copyProps(document.defaultView, global);
```


Expand Down

0 comments on commit f3bbfd3

Please sign in to comment.