Created
February 7, 2020 14:29
-
-
Save eoikonomou/cd2166933707bb35be2b082e4c605ab2 to your computer and use it in GitHub Desktop.
Mocking canvas for tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Mock Canvas / Context2D calls | |
// | |
function mockCanvas (window) { | |
window.HTMLCanvasElement.prototype.getContext = function () { | |
return { | |
fillRect: function() {}, | |
clearRect: function(){}, | |
getImageData: function(x, y, w, h) { | |
return { | |
data: new Array(w*h*4) | |
}; | |
}, | |
putImageData: function() {}, | |
createImageData: function(){ return []}, | |
setTransform: function(){}, | |
drawImage: function(){}, | |
save: function(){}, | |
fillText: function(){}, | |
restore: function(){}, | |
beginPath: function(){}, | |
moveTo: function(){}, | |
lineTo: function(){}, | |
closePath: function(){}, | |
stroke: function(){}, | |
translate: function(){}, | |
scale: function(){}, | |
rotate: function(){}, | |
arc: function(){}, | |
fill: function(){}, | |
measureText: function(){ | |
return { width: 0 }; | |
}, | |
transform: function(){}, | |
rect: function(){}, | |
clip: function(){}, | |
}; | |
} | |
window.HTMLCanvasElement.prototype.toDataURL = function () { | |
return ""; | |
} | |
} | |
const document = jsdom.jsdom(undefined, { | |
virtualConsole: jsdom.createVirtualConsole().sendTo(console) | |
}); | |
const window = document.defaultView; | |
mockCanvas(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment