-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Use mock canvas object replacing canvas
#3518
Conversation
Fixes almende#3515. A mock canvas object is added to the unit tests, which makes usage of module `canvas` voluntary. The issue with `canvas` is that it requires an external dependency to `cairo`. This complicates setting up a develop environment for `vis.js` - Removed `canvas` from `package.json` - Added section to README.md with instructions on how to install `canvas` instead.
package.json
Outdated
@@ -33,30 +33,30 @@ | |||
"watch-dev": "gulp watch --bundle" | |||
}, | |||
"dependencies": { | |||
"canvas": "^1.6.7", |
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.
"Canvas" is not needed als dependency because it is only used during development. Should be removed here.
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.
Right. I must have placed it there again during testing.
README.md
Outdated
@@ -322,6 +322,31 @@ module: { | |||
|
|||
There is also an [demo-project](https://github.com/mojoaxel/vis-webpack-demo) showing the integration of vis.js using webpack. | |||
|
|||
|
|||
## Using module `canvas` |
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.
I think this section should be better a sub-part of "Installation".
README.md
Outdated
The unit tests use a mock object for canvas, which is severely limited in its functionality. It might be preferable | ||
to use module `canvas` supplies a canvas object for usage with `node.js`. This has a functional implementation of the 2D context. | ||
|
||
Reasons to use `canvas: |
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.
I don't think we need this "Reasons to use canvas" section at all.
README.md
Outdated
The unit tests will only use a mock object for canvas, if there is no `canvas` element already supplied. So, if the `canvas` module is installed, that will take precedence. | ||
|
||
|
||
The issue with `canvas` is that it has an external dependency to `cairo`. This needs to be installed outside of the regular install as done by `npm`. |
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.
👍
README.md
Outdated
|
||
The issue with `canvas` is that it has an external dependency to `cairo`. This needs to be installed outside of the regular install as done by `npm`. | ||
|
||
- In order to fully install `canvas` on Windows, follow [these instructions](https://github.com/Automattic/node-canvas/wiki/Installation---Windows). |
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.
Why not just link to the canvas-wiki and remove this whole section.
test/Network.test.js
Outdated
var {allOptions, configureOptions} = require('./../lib/network/options.js'); | ||
//var {printStyle} = require('./../lib/shared/Validator'); | ||
|
||
|
||
|
||
|
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.
Extra blank lines.
Typically, I use 2 blank lines to separate top level class/function declarations, single blank line to separate import groups. Two blank lines to separate imports from constants. Single blank line between nested functions.
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.
I have my own guidelines which broadly match yours. But this was indeed a good observation.
test/Network.test.js
Outdated
@@ -353,6 +360,9 @@ describe('Network', function () { | |||
|
|||
|
|||
describe('Node', function () { | |||
before(function() { | |||
// if (!this.canvasPresent) this.skip(); | |||
}); |
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.
These before
functions should all be removed, imo
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.
Yes, they should. Artifacts of a previous attempt at a fix.
test/canvas-mock.js
Outdated
* Adapted from: https://github.com/Cristy94/canvas-mock | ||
*/ | ||
|
||
var myHackedInCanvas; // Use one global canvas instance for all calls to createElement('canvas'); |
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.
Why global?
If that's a good reason, can we call this globalCanvasMock?
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.
OK, the comment might be a bit off. It's strictly speaking not a global because it doesn't reside in the global namespace. It's a static within the module. I'll adjust the comment.
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.
And the name is silly; changing it to canvasMock
.
// Following added for vis.js unit tests | ||
// | ||
|
||
measureText: function(text) { |
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?
measureText: function(text) {
return {
width:>> <<12*text.length,
height: 14
};
},
};
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.
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.
Looks good
@mbroad Thanks. It's admittedly a hack, but it only runs within the unit tests so I think we're OK. |
@mojoaxel can you please review again/accept changes so we can get this in? |
@wimrijnders - Seems like an appropriate use of a mock to me. |
@mojoaxel I've handled your review points. Can you please have a look again? |
We should release this quickly as a Patch-Release. |
@mojoaxel Another release will occur in exactly 2 weeks |
* Use mock canvas object replacing `canvas` Fixes almende#3515. A mock canvas object is added to the unit tests, which makes usage of module `canvas` voluntary. The issue with `canvas` is that it requires an external dependency to `cairo`. This complicates setting up a develop environment for `vis.js` - Removed `canvas` from `package.json` - Added section to README.md with instructions on how to install `canvas` instead. * Removed debugger statements * Updates for review * Fixes for review
Fixes #3515.
A mock canvas object is added to the unit tests, which makes usage of module
canvas
voluntary.The issue with
canvas
is that it requires an external dependency tocairo
. This complicates setting up a develop environment forvis.js
canvas
frompackage.json
canvas
instead.