Skip to content

Commit d3549d1

Browse files
authored
Merge pull request #5755 from plotly/centralize-jsdom-plotly-node
Rewrite and make a centralized jsdom utility to get Plotly object in node.js test scripts and generating the schema
2 parents 0fe16c2 + 40fb62e commit d3549d1

File tree

4 files changed

+27
-35
lines changed

4 files changed

+27
-35
lines changed

tasks/test_mock.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
var minimist = require('minimist');
2-
var jsdom = require('jsdom');
32
var path = require('path');
43
var fs = require('fs');
54

6-
var plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'});
7-
// Mock a few things that jsdom doesn't support out-of-the-box
8-
plotlyServerDom.window.URL.createObjectURL = function() {};
9-
10-
// Run Plotly inside jsdom
11-
var plotlyJsPath = require.resolve('../build/plotly.js');
12-
var plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8');
13-
plotlyServerDom.window.eval(plotlyJsSource);
5+
var plotlyNode = require('./util/plotly_node');
6+
var Plotly = plotlyNode('build/plotly.js');
147

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

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

4437
fail = false;
4538
assert(name, out);

tasks/test_plain_obj.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
var jsdom = require('jsdom');
2-
var fs = require('fs');
3-
4-
var plotlyServerDom = new jsdom.JSDOM('', { runScripts: 'dangerously'});
5-
// Mock a few things that jsdom doesn't support out-of-the-box
6-
plotlyServerDom.window.URL.createObjectURL = function() {};
7-
8-
// Run Plotly inside jsdom
9-
var plotlyJsPath = require.resolve('../build/plotly.js');
10-
var plotlyJsSource = fs.readFileSync(plotlyJsPath, 'utf-8');
11-
plotlyServerDom.window.eval(plotlyJsSource);
1+
var plotlyNode = require('./util/plotly_node');
2+
var Plotly = plotlyNode('build/plotly.js');
123

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

16-
var errorList = plotlyServerDom.window.Plotly.validate(fig.data, fig.layout);
7+
var errorList = Plotly.validate(fig.data, fig.layout);
178

189
if(exp) {
1910
if(errorList !== undefined) throw 'should be valid:';

tasks/util/make_schema.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
var fs = require('fs');
22
var path = require('path');
3-
var JSDOM = require('jsdom').JSDOM;
3+
var plotlyNode = require('./plotly_node');
44

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

9-
var w = new JSDOM('', {runScripts: 'dangerously'}).window;
10-
11-
// jsdom by itself doesn't support getContext, and adding the npm canvas
12-
// package is annoying and platform-dependent.
13-
// see https://github.com/tmpvar/jsdom/issues/1782
14-
w.HTMLCanvasElement.prototype.getContext = function() { return null; };
15-
w.URL.createObjectURL = function() { return null; };
16-
17-
w.eval(plotlyjsCode);
18-
19-
var plotSchema = w.Plotly.PlotSchema.get();
9+
var plotSchema = Plotly.PlotSchema.get();
2010
var plotSchemaStr = JSON.stringify(plotSchema, null, 1);
2111
fs.writeFileSync(schemaPath, plotSchemaStr);
2212

tasks/util/plotly_node.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var fs = require('fs');
2+
var JSDOM = require('jsdom').JSDOM;
3+
4+
var window = new JSDOM('', {
5+
runScripts: 'dangerously'
6+
}).window;
7+
8+
// Mock things that jsdom doesn't support out-of-the-box
9+
window.URL.createObjectURL = function() {};
10+
11+
module.exports = function plotlyNode(plotlyPath) {
12+
// Execute source code by inserting a <script> tag containing it
13+
var scriptEl = window.document.createElement('script');
14+
scriptEl.textContent = fs.readFileSync(plotlyPath, { encoding: 'utf-8' });
15+
window.document.body.appendChild(scriptEl);
16+
17+
return window.Plotly;
18+
};

0 commit comments

Comments
 (0)