Skip to content

Commit b3086fa

Browse files
committed
test: add test for plotly.min.js + require.js
- which basically mimics how plotly.js is injected into Jupyter NB by plotly.py
1 parent d736910 commit b3086fa

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"open": "0.0.5",
110110
"prepend-file": "^1.3.0",
111111
"prettysize": "0.0.3",
112+
"requirejs": "^2.3.1",
112113
"through2": "^2.0.0",
113114
"uglify-js": "^2.6.1",
114115
"watchify": "^3.7.0",

tasks/pretest.js

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var common = require('./util/common');
77
makeCredentialsFile();
88
makeSetPlotConfigFile();
99
makeTestImageFolders();
10+
makeRequireJSFixture();
1011

1112
// Create a credentials json file,
1213
// to be required in jasmine test suites and test dashboard
@@ -52,6 +53,16 @@ function makeTestImageFolders() {
5253
makeOne(constants.pathToTestImagesDiff, 'test image diff folder');
5354
}
5455

56+
// Make script file that define plotly in a RequireJS context
57+
function makeRequireJSFixture() {
58+
var bundle = fs.readFileSync(constants.pathToPlotlyDistMin, 'utf-8'),
59+
template = 'define(\'plotly\', function(require, exports, module) { {{bundle}} });',
60+
index = template.replace('{{bundle}}', bundle);
61+
62+
common.writeFile(constants.pathToRequireJSFixture, index);
63+
logger('make build/requirejs_fixture.js');
64+
}
65+
5566
function logger(task) {
5667
console.log('ok ' + task);
5768
}

tasks/util/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ module.exports = {
6666

6767
pathToJasmineTests: path.join(pathToRoot, 'test/jasmine/tests'),
6868
pathToJasmineBundleTests: path.join(pathToRoot, 'test/jasmine/bundle_tests'),
69+
pathToRequireJS: path.join(pathToRoot, 'node_modules', 'requirejs', 'require.js'),
70+
pathToRequireJSFixture: path.join(pathToBuild, 'requirejs_fixture.js'),
6971

7072
// this mapbox access token is 'public', no need to hide it
7173
// more info: https://www.mapbox.com/help/define-access-token/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe('plotly.js + require.js', function() {
2+
'use strict';
3+
4+
it('should preserve require.js globals', function() {
5+
expect(window.requirejs).toBeDefined();
6+
expect(window.define).toBeDefined();
7+
expect(window.require).toBeDefined();
8+
});
9+
10+
it('should be able to import plotly.min.js', function(done) {
11+
require(['plotly'], function(Plotly) {
12+
expect(Plotly).toBeDefined();
13+
done();
14+
});
15+
});
16+
});

test/jasmine/karma.conf.js

+10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
*
1414
*/
1515

16+
var constants = require('../../tasks/util/constants');
17+
1618
var arg = process.argv[4];
1719

1820
var testFileGlob = arg ? arg : 'tests/*_test.js';
1921
var isSingleSuiteRun = (arg && arg.indexOf('bundle_tests/') === -1);
22+
var isRequireJSTest = (arg && arg.indexOf('bundle_tests/requirejs') !== -1);
2023

2124
var pathToMain = '../../lib/index.js';
2225
var pathToJQuery = 'assets/jquery-1.8.3.min.js';
@@ -113,6 +116,13 @@ if(isSingleSuiteRun) {
113116
func.defaultConfig.preprocessors[pathToMain] = ['browserify'];
114117
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
115118
}
119+
else if(isRequireJSTest) {
120+
func.defaultConfig.files = [
121+
constants.pathToRequireJS,
122+
constants.pathToRequireJSFixture,
123+
testFileGlob
124+
];
125+
}
116126
else {
117127
func.defaultConfig.files = [
118128
pathToJQuery,

0 commit comments

Comments
 (0)