Skip to content

Commit 9b227f6

Browse files
committed
make make_schema and validate tests easier to read for a front end dev
1 parent 0ce560c commit 9b227f6

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

tasks/test_mock.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
var minimist = require('minimist');
2-
var jsdom = require('jsdom');
32
var path = require('path');
43
var fs = require('fs');
4+
var JSDOM = require('jsdom').JSDOM;
5+
6+
var window = new JSDOM('', {runScripts: 'dangerously'}).window;
57

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

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);
11+
var scriptEl = window.document.createElement('script');
12+
scriptEl.textContent = fs.readFileSync('build/plotly.js', { encoding: 'utf-8' });
13+
window.document.body.appendChild(scriptEl);
14+
var Plotly = window.Plotly;
1415

1516
var pathToRoot = path.join(__dirname, '..');
1617
var pathToMocks = path.join(pathToRoot, 'test', 'image', 'mocks');
@@ -39,7 +40,7 @@ for(var i = 0; i < list.length; i++) {
3940

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

4445
fail = false;
4546
assert(name, out);

tasks/test_plain_obj.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
var jsdom = require('jsdom');
21
var fs = require('fs');
2+
var JSDOM = require('jsdom').JSDOM;
3+
4+
var window = new JSDOM('', {runScripts: 'dangerously'}).window;
35

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

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);
9+
var scriptEl = window.document.createElement('script');
10+
scriptEl.textContent = fs.readFileSync('dist/plotly.js', { encoding: 'utf-8' });
11+
window.document.body.appendChild(scriptEl);
12+
var Plotly = window.Plotly;
1213

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

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

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

tasks/util/make_schema.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ var fs = require('fs');
22
var path = require('path');
33
var JSDOM = require('jsdom').JSDOM;
44

5-
module.exports = function makeSchema(plotlyPath, schemaPath) {
6-
return function() {
7-
var plotlyjsCode = fs.readFileSync(plotlyPath, 'utf-8');
8-
9-
var w = new JSDOM('', {runScripts: 'dangerously'}).window;
5+
var window = new JSDOM('', {runScripts: 'dangerously'}).window;
106

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; };
7+
// Mock a few things that jsdom doesn't support out-of-the-box
8+
window.URL.createObjectURL = function() {};
169

17-
w.eval(plotlyjsCode);
10+
module.exports = function makeSchema(plotlyPath, schemaPath) {
11+
return function() {
12+
var scriptEl = window.document.createElement('script');
13+
scriptEl.textContent = fs.readFileSync(plotlyPath, { encoding: 'utf-8' });
14+
window.document.body.appendChild(scriptEl);
15+
var Plotly = window.Plotly;
1816

19-
var plotSchema = w.Plotly.PlotSchema.get();
17+
var plotSchema = Plotly.PlotSchema.get();
2018
var plotSchemaStr = JSON.stringify(plotSchema, null, 1);
2119
fs.writeFileSync(schemaPath, plotSchemaStr);
2220

0 commit comments

Comments
 (0)