|
@ -3,6 +3,8 @@ |
|
|
* |
|
|
* |
|
|
* Adapted from: https://github.com/Cristy94/canvas-mock
|
|
|
* Adapted from: https://github.com/Cristy94/canvas-mock
|
|
|
*/ |
|
|
*/ |
|
|
|
|
|
var jsdom = require('jsdom'); |
|
|
|
|
|
var jsdom_global = require('jsdom-global'); |
|
|
|
|
|
|
|
|
var canvasMock; // Use one canvas instance for all calls to createElement('canvas');
|
|
|
var canvasMock; // Use one canvas instance for all calls to createElement('canvas');
|
|
|
|
|
|
|
|
@ -59,10 +61,11 @@ function replaceCanvasContext (el) { |
|
|
* The override is only done if there is no 2D context already present. |
|
|
* The override is only done if there is no 2D context already present. |
|
|
* This allows for normal running in a browser, and for node.js the usage of 'canvas'. |
|
|
* This allows for normal running in a browser, and for node.js the usage of 'canvas'. |
|
|
* |
|
|
* |
|
|
* @param {object} the current global window object. This can possible come from module 'jsdom', |
|
|
|
|
|
|
|
|
* @param {object} the current global window object. This can possibly come from module 'jsdom', |
|
|
* when running under node.js. |
|
|
* when running under node.js. |
|
|
|
|
|
* @private |
|
|
*/ |
|
|
*/ |
|
|
function mockify(window) { |
|
|
|
|
|
|
|
|
function overrideCreateElement(window) { |
|
|
var d = window.document; |
|
|
var d = window.document; |
|
|
var f = window.document.createElement; |
|
|
var f = window.document.createElement; |
|
|
|
|
|
|
|
@ -87,4 +90,48 @@ function mockify(window) { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Initialize the mock, jsdom and jsdom_global for unit test usage. |
|
|
|
|
|
* |
|
|
|
|
|
* Suppresses a warning from `jsdom` on usage of `getContext()`. A mock definition is added for |
|
|
|
|
|
* it, so the message is not relevant. |
|
|
|
|
|
* |
|
|
|
|
|
* @param {string} [html=''] html definitions which should be added to the jsdom definition |
|
|
|
|
|
* @returns {function} function to call in after(), to clean up for `jsdom_global` |
|
|
|
|
|
*/ |
|
|
|
|
|
function mockify(html = '') { |
|
|
|
|
|
// Start of message that we want to suppress.
|
|
|
|
|
|
let msg = 'Error: Not implemented: HTMLCanvasElement.prototype.getContext' |
|
|
|
|
|
+ ' (without installing the canvas npm package)'; |
|
|
|
|
|
|
|
|
|
|
|
// Override default virtual console of jsdom
|
|
|
|
|
|
const virtualConsole = new jsdom.VirtualConsole(); |
|
|
|
|
|
|
|
|
|
|
|
// Set up a simple 'mock' console output. Only 'error' needs to be overridden
|
|
|
|
|
|
let myConsole = { |
|
|
|
|
|
error: (msg) => { |
|
|
|
|
|
if (msg.indexOf(msg) === 0) { |
|
|
|
|
|
//console.error('all is well');
|
|
|
|
|
|
} else { |
|
|
|
|
|
// All other messages pass through
|
|
|
|
|
|
console.error(msg); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// Using the global catch instead of specific event handler, because I couldn't get them to work
|
|
|
|
|
|
virtualConsole.sendTo(myConsole); |
|
|
|
|
|
|
|
|
|
|
|
let cleanupFunction = jsdom_global( |
|
|
|
|
|
html, |
|
|
|
|
|
{ skipWindowCheck: true, virtualConsole: virtualConsole} |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
overrideCreateElement(window); // The actual initialization of canvas-mock
|
|
|
|
|
|
|
|
|
|
|
|
return cleanupFunction; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = mockify; |
|
|
module.exports = mockify; |