This repository has been archived by the owner on Jul 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Use mock canvas object replacing canvas
#3518
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Set up mock 2D context, for usage in unit tests. | ||
* | ||
* Adapted from: https://github.com/Cristy94/canvas-mock | ||
*/ | ||
|
||
var canvasMock; // Use one canvas instance for all calls to createElement('canvas'); | ||
|
||
|
||
function replaceCanvasContext (el) { | ||
el.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(){}, | ||
|
||
// | ||
// Following added for vis.js unit tests | ||
// | ||
|
||
measureText: function(text) { | ||
return { | ||
width: 12*text.length, | ||
height: 14 | ||
}; | ||
}, | ||
}; | ||
} | ||
}; | ||
|
||
|
||
/** | ||
* Overrides document.createElement(), in order to supply a custom canvas element. | ||
* | ||
* In the canvas element, getContext() is overridden in order to supply a simple | ||
* mock object for the 2D context. For all other elements, the call functions unchanged. | ||
* | ||
* 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'. | ||
* | ||
* @param {object} the current global window object. This can possible come from module 'jsdom', | ||
* when running under node.js. | ||
*/ | ||
function mockify(window) { | ||
var d = window.document; | ||
var f = window.document.createElement; | ||
|
||
// Check if 2D context already present. That happens either when running in a browser, | ||
// or this is node.js with 'canvas' installed. | ||
var ctx = d.createElement('canvas').getContext('2d'); | ||
if (ctx !== null && ctx !== undefined) { | ||
//console.log('2D context is present, no need to override'); | ||
return; | ||
} | ||
|
||
window.document.createElement = function(param) { | ||
if (param === 'canvas') { | ||
if (canvasMock === undefined) { | ||
canvasMock = f.call(d, 'canvas'); | ||
replaceCanvasContext(canvasMock); | ||
} | ||
return canvasMock; | ||
} else { | ||
return f.call(d, param); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = mockify; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spacing looks off
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, this code is a module which I took from here and adapted it to own needs when I realized it wasn't completely fit for the purpose.
If think you mean this spacing?
Otherwise, I don't know what you mean. I have a habit of aligning related stuff, but indeed it's not needed here with only two lines.
Ah wait, there's tabs there. That's probably what you mean. Ah, right, that's from the original. I'll change it to spaces.