/
window.js
61 lines (47 loc) · 1.96 KB
/
window.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const jsdom = require('jsdom');
const gl = require('gl');
const sinon = require('sinon');
const util = require('./util');
function restore() {
// Remove previous window from module.exports
const previousWindow = module.exports;
if (previousWindow.close) previousWindow.close();
for (const key in previousWindow) if (previousWindow.hasOwnProperty(key)) delete previousWindow[key];
// Create new window and inject into module.exports
const window = jsdom.jsdom(undefined, {
// Send jsdom console output to the node console object.
virtualConsole: jsdom.createVirtualConsole().sendTo(console)
}).defaultView;
util.extend(module.exports, window);
window.devicePixelRatio = 1;
window.requestAnimationFrame = function(callback) {
return setImmediate(callback, 0);
};
window.cancelAnimationFrame = clearImmediate;
// Add webgl context with the supplied GL
const originalGetContext = window.HTMLCanvasElement.prototype.getContext;
window.HTMLCanvasElement.prototype.getContext = function (type, attributes) {
if (type === 'webgl') {
if (!this._webGLContext) {
this._webGLContext = gl(this.width, this.height, attributes);
}
return this._webGLContext;
}
// Fallback to existing HTMLCanvasElement getContext behaviour
return originalGetContext.call(this, type, attributes);
};
window.useFakeHTMLCanvasGetContext = function() {
window.HTMLCanvasElement.prototype.getContext = sinon.stub().returns('2d');
};
window.useFakeXMLHttpRequest = function() {
sinon.xhr.supportsCORS = true;
window.server = sinon.fakeServer.create();
window.XMLHttpRequest = window.server.xhr;
};
window.URL.revokeObjectURL = function () {};
window.restore = restore;
window.ImageData = window.ImageData || sinon.stub().returns(false);
return window;
}
module.exports = restore();