-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.js
34 lines (27 loc) · 964 Bytes
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const jsdom = require('jsdom');
// setup the simplest document possible
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
// get the window object out of the document
const win = doc.defaultView;
// set globals for mocha that make access to document and window feel
// natural in the test environment
global.document = doc;
global.window = win;
//JSDOM doesn't support localStrage by default, so lets just fake it..
if (!global.window.localStorage) {
global.window.localStorage = {
getItem() { return '{}'; },
setItem() {}
};
}
// take all properties of the window object and also attach it to the
// mocha global object
propagateToGlobal(win);
// from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal (window) {
for (let key in window) {
if (!window.hasOwnProperty(key)) continue;
if (key in global) continue;
global[key] = window[key];
}
}