Skip to content

Commit 40fb62e

Browse files
committed
reuse identical logic in 3 places
1 parent 9b227f6 commit 40fb62e

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

tasks/test_mock.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
var minimist = require('minimist');
22
var path = require('path');
33
var fs = require('fs');
4-
var JSDOM = require('jsdom').JSDOM;
54

6-
var window = new JSDOM('', {runScripts: 'dangerously'}).window;
7-
8-
// Mock a few things that jsdom doesn't support out-of-the-box
9-
window.URL.createObjectURL = function() {};
10-
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;
5+
var plotlyNode = require('./util/plotly_node');
6+
var Plotly = plotlyNode('build/plotly.js');
157

168
var pathToRoot = path.join(__dirname, '..');
179
var pathToMocks = path.join(pathToRoot, 'test', 'image', 'mocks');

tasks/test_plain_obj.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
var fs = require('fs');
2-
var JSDOM = require('jsdom').JSDOM;
3-
4-
var window = new JSDOM('', {runScripts: 'dangerously'}).window;
5-
6-
// Mock a few things that jsdom doesn't support out-of-the-box
7-
window.URL.createObjectURL = function() {};
8-
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;
1+
var plotlyNode = require('./util/plotly_node');
2+
var Plotly = plotlyNode('build/plotly.js');
133

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

tasks/util/make_schema.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
var fs = require('fs');
22
var path = require('path');
3-
var JSDOM = require('jsdom').JSDOM;
4-
5-
var window = new JSDOM('', {runScripts: 'dangerously'}).window;
6-
7-
// Mock a few things that jsdom doesn't support out-of-the-box
8-
window.URL.createObjectURL = function() {};
3+
var plotlyNode = require('./plotly_node');
94

105
module.exports = function makeSchema(plotlyPath, schemaPath) {
116
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;
7+
var Plotly = plotlyNode(plotlyPath);
168

179
var plotSchema = Plotly.PlotSchema.get();
1810
var plotSchemaStr = JSON.stringify(plotSchema, null, 1);

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)