Skip to content

Rewrite and make a centralized jsdom utility to get Plotly object in node.js test scripts and generating the schema #5755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions tasks/test_mock.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
var minimist = require('minimist');
var jsdom = require('jsdom');
var path = require('path');
var fs = require('fs');

var plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'});
// Mock a few things that jsdom doesn't support out-of-the-box
plotlyServerDom.window.URL.createObjectURL = function() {};

// Run Plotly inside jsdom
var plotlyJsPath = require.resolve('../build/plotly.js');
var plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8');
plotlyServerDom.window.eval(plotlyJsSource);
var plotlyNode = require('./util/plotly_node');
var Plotly = plotlyNode('build/plotly.js');

var pathToRoot = path.join(__dirname, '..');
var pathToMocks = path.join(pathToRoot, 'test', 'image', 'mocks');
Expand Down Expand Up @@ -39,7 +32,7 @@ for(var i = 0; i < list.length; i++) {

var filename = path.join(pathToMocks, name + '.json');
var fig = JSON.parse(fs.readFileSync(filename));
var out = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout);
var out = Plotly.validate(fig.data, fig.layout);

fail = false;
assert(name, out);
Expand Down
15 changes: 3 additions & 12 deletions tasks/test_plain_obj.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
var jsdom = require('jsdom');
var fs = require('fs');

var plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'});
// Mock a few things that jsdom doesn't support out-of-the-box
plotlyServerDom.window.URL.createObjectURL = function() {};

// Run Plotly inside jsdom
var plotlyJsPath = require.resolve('../build/plotly.js');
var plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8');
plotlyServerDom.window.eval(plotlyJsSource);
var plotlyNode = require('./util/plotly_node');
var Plotly = plotlyNode('build/plotly.js');

var assertValidate = function(fig, exp) {
console.log(fig);

var errorList = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout);
var errorList = Plotly.validate(fig.data, fig.layout);

if(exp) {
if(errorList !== undefined) throw 'should be valid:';
Expand Down
16 changes: 3 additions & 13 deletions tasks/util/make_schema.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
var fs = require('fs');
var path = require('path');
var JSDOM = require('jsdom').JSDOM;
var plotlyNode = require('./plotly_node');

module.exports = function makeSchema(plotlyPath, schemaPath) {
return function() {
var plotlyjsCode = fs.readFileSync(plotlyPath, 'utf-8');
var Plotly = plotlyNode(plotlyPath);

var w = new JSDOM('', {runScripts: 'dangerously'}).window;

// jsdom by itself doesn't support getContext, and adding the npm canvas
// package is annoying and platform-dependent.
// see https://github.com/tmpvar/jsdom/issues/1782
w.HTMLCanvasElement.prototype.getContext = function() { return null; };
w.URL.createObjectURL = function() { return null; };

w.eval(plotlyjsCode);

var plotSchema = w.Plotly.PlotSchema.get();
var plotSchema = Plotly.PlotSchema.get();
var plotSchemaStr = JSON.stringify(plotSchema, null, 1);
fs.writeFileSync(schemaPath, plotSchemaStr);

Expand Down
18 changes: 18 additions & 0 deletions tasks/util/plotly_node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var fs = require('fs');
var JSDOM = require('jsdom').JSDOM;

var window = new JSDOM('', {
runScripts: 'dangerously'
}).window;

// Mock things that jsdom doesn't support out-of-the-box
window.URL.createObjectURL = function() {};

module.exports = function plotlyNode(plotlyPath) {
// Execute source code by inserting a <script> tag containing it
var scriptEl = window.document.createElement('script');
scriptEl.textContent = fs.readFileSync(plotlyPath, { encoding: 'utf-8' });
window.document.body.appendChild(scriptEl);

return window.Plotly;
};