From 9b227f65d3d71720828bdcc6b3e7d90a8bb49678 Mon Sep 17 00:00:00 2001 From: archmoj Date: Mon, 21 Jun 2021 21:54:59 -0400 Subject: [PATCH 1/2] make make_schema and validate tests easier to read for a front end dev --- tasks/test_mock.js | 17 +++++++++-------- tasks/test_plain_obj.js | 17 +++++++++-------- tasks/util/make_schema.js | 22 ++++++++++------------ 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/tasks/test_mock.js b/tasks/test_mock.js index 957b86b41ef..83af1c7f492 100644 --- a/tasks/test_mock.js +++ b/tasks/test_mock.js @@ -1,16 +1,17 @@ var minimist = require('minimist'); -var jsdom = require('jsdom'); var path = require('path'); var fs = require('fs'); +var JSDOM = require('jsdom').JSDOM; + +var window = new JSDOM('', {runScripts: 'dangerously'}).window; -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() {}; +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 scriptEl = window.document.createElement('script'); +scriptEl.textContent = fs.readFileSync('build/plotly.js', { encoding: 'utf-8' }); +window.document.body.appendChild(scriptEl); +var Plotly = window.Plotly; var pathToRoot = path.join(__dirname, '..'); var pathToMocks = path.join(pathToRoot, 'test', 'image', 'mocks'); @@ -39,7 +40,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); diff --git a/tasks/test_plain_obj.js b/tasks/test_plain_obj.js index 20904337e80..0601d0339d4 100644 --- a/tasks/test_plain_obj.js +++ b/tasks/test_plain_obj.js @@ -1,19 +1,20 @@ -var jsdom = require('jsdom'); var fs = require('fs'); +var JSDOM = require('jsdom').JSDOM; + +var window = new JSDOM('', {runScripts: 'dangerously'}).window; -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() {}; +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 scriptEl = window.document.createElement('script'); +scriptEl.textContent = fs.readFileSync('dist/plotly.js', { encoding: 'utf-8' }); +window.document.body.appendChild(scriptEl); +var Plotly = window.Plotly; 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:'; diff --git a/tasks/util/make_schema.js b/tasks/util/make_schema.js index f2e68036aad..5a1b4d07e32 100644 --- a/tasks/util/make_schema.js +++ b/tasks/util/make_schema.js @@ -2,21 +2,19 @@ var fs = require('fs'); var path = require('path'); var JSDOM = require('jsdom').JSDOM; -module.exports = function makeSchema(plotlyPath, schemaPath) { - return function() { - var plotlyjsCode = fs.readFileSync(plotlyPath, 'utf-8'); - - var w = new JSDOM('', {runScripts: 'dangerously'}).window; +var window = 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; }; +// Mock a few things that jsdom doesn't support out-of-the-box +window.URL.createObjectURL = function() {}; - w.eval(plotlyjsCode); +module.exports = function makeSchema(plotlyPath, schemaPath) { + return function() { + var scriptEl = window.document.createElement('script'); + scriptEl.textContent = fs.readFileSync(plotlyPath, { encoding: 'utf-8' }); + window.document.body.appendChild(scriptEl); + var Plotly = window.Plotly; - var plotSchema = w.Plotly.PlotSchema.get(); + var plotSchema = Plotly.PlotSchema.get(); var plotSchemaStr = JSON.stringify(plotSchema, null, 1); fs.writeFileSync(schemaPath, plotSchemaStr); From 40fb62eb7b3d919043f9fd77fda33a3ee85763c5 Mon Sep 17 00:00:00 2001 From: archmoj Date: Mon, 21 Jun 2021 22:26:29 -0400 Subject: [PATCH 2/2] reuse identical logic in 3 places --- tasks/test_mock.js | 12 ++---------- tasks/test_plain_obj.js | 14 ++------------ tasks/util/make_schema.js | 12 ++---------- tasks/util/plotly_node.js | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 32 deletions(-) create mode 100644 tasks/util/plotly_node.js diff --git a/tasks/test_mock.js b/tasks/test_mock.js index 83af1c7f492..0503ef8796b 100644 --- a/tasks/test_mock.js +++ b/tasks/test_mock.js @@ -1,17 +1,9 @@ var minimist = require('minimist'); var path = require('path'); var fs = require('fs'); -var JSDOM = require('jsdom').JSDOM; -var window = new JSDOM('', {runScripts: 'dangerously'}).window; - -// Mock a few things that jsdom doesn't support out-of-the-box -window.URL.createObjectURL = function() {}; - -var scriptEl = window.document.createElement('script'); -scriptEl.textContent = fs.readFileSync('build/plotly.js', { encoding: 'utf-8' }); -window.document.body.appendChild(scriptEl); -var Plotly = window.Plotly; +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'); diff --git a/tasks/test_plain_obj.js b/tasks/test_plain_obj.js index 0601d0339d4..007e20c05a2 100644 --- a/tasks/test_plain_obj.js +++ b/tasks/test_plain_obj.js @@ -1,15 +1,5 @@ -var fs = require('fs'); -var JSDOM = require('jsdom').JSDOM; - -var window = new JSDOM('', {runScripts: 'dangerously'}).window; - -// Mock a few things that jsdom doesn't support out-of-the-box -window.URL.createObjectURL = function() {}; - -var scriptEl = window.document.createElement('script'); -scriptEl.textContent = fs.readFileSync('dist/plotly.js', { encoding: 'utf-8' }); -window.document.body.appendChild(scriptEl); -var Plotly = window.Plotly; +var plotlyNode = require('./util/plotly_node'); +var Plotly = plotlyNode('build/plotly.js'); var assertValidate = function(fig, exp) { console.log(fig); diff --git a/tasks/util/make_schema.js b/tasks/util/make_schema.js index 5a1b4d07e32..47c1790afa4 100644 --- a/tasks/util/make_schema.js +++ b/tasks/util/make_schema.js @@ -1,18 +1,10 @@ var fs = require('fs'); var path = require('path'); -var JSDOM = require('jsdom').JSDOM; - -var window = new JSDOM('', {runScripts: 'dangerously'}).window; - -// Mock a few things that jsdom doesn't support out-of-the-box -window.URL.createObjectURL = function() {}; +var plotlyNode = require('./plotly_node'); module.exports = function makeSchema(plotlyPath, schemaPath) { return function() { - var scriptEl = window.document.createElement('script'); - scriptEl.textContent = fs.readFileSync(plotlyPath, { encoding: 'utf-8' }); - window.document.body.appendChild(scriptEl); - var Plotly = window.Plotly; + var Plotly = plotlyNode(plotlyPath); var plotSchema = Plotly.PlotSchema.get(); var plotSchemaStr = JSON.stringify(plotSchema, null, 1); diff --git a/tasks/util/plotly_node.js b/tasks/util/plotly_node.js new file mode 100644 index 00000000000..2bc34bce069 --- /dev/null +++ b/tasks/util/plotly_node.js @@ -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