Skip to content

Commit 642f0ff

Browse files
committed
Merge pull request #206 from plotly/bundle-tests
Add testing framework to test custom bundles
2 parents 54bfe7a + 90a2538 commit 642f0ff

12 files changed

+228
-10
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ build/*
66
!build/ploticon.js
77
!build/README.md
88

9-
npm-debug.log
9+
npm-debug.log*

circle.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ test:
2121
override:
2222
- sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' mytestbed)" -- bash -c "cd /var/www/streambed/image_server/plotly.js && node test/image/compare_pixels_test.js"
2323
- npm run citest-jasmine
24+
- npm run test-bundle
2425
- npm run test-syntax

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
"preprocess": "node tasks/preprocess.js",
2525
"bundle": "node tasks/bundle.js",
2626
"header": "node tasks/header.js",
27-
"build": "npm run preprocess && npm run bundle && npm run header",
27+
"build": "npm dedupe && npm run preprocess && npm run bundle && npm run header",
2828
"cibuild": "node tasks/cibundle.js",
2929
"watch": "node tasks/watch_plotly.js",
3030
"lint": "eslint . || true",
3131
"test-jasmine": "karma start test/jasmine/karma.conf.js",
3232
"citest-jasmine": "karma start test/jasmine/karma.ciconf.js",
3333
"test-image": "./tasks/test_image.sh",
3434
"test-syntax": "node test/syntax_test.js",
35-
"test": "npm run test-jasmine && npm test-image && npm test-syntax",
35+
"test-bundle": "node tasks/test_bundle.js",
36+
"test": "npm run citest-jasmine && npm run test-image && npm run test-syntax && npm run test-bundle",
3637
"start-test_dashboard": "node devtools/test_dashboard/server.js",
3738
"start-image_viewer": "node devtools/image_viewer/server.js",
3839
"baseline": "./tasks/baseline.sh",

tasks/test_bundle.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var path = require('path');
2+
var exec = require('child_process').exec;
3+
4+
var glob = require('glob');
5+
6+
var constants = require('./util/constants');
7+
var pathToJasmineBundleTests = path.join(constants.pathToJasmineBundleTests);
8+
9+
10+
glob(pathToJasmineBundleTests + '/*.js', function(err, files) {
11+
files.forEach(function(file) {
12+
var baseName = path.basename(file);
13+
var cmd = 'npm run citest-jasmine -- bundle_tests/' + baseName;
14+
15+
exec(cmd, function(err, stdout) {
16+
console.log(stdout);
17+
18+
if(err) throw err;
19+
});
20+
});
21+
});

tasks/util/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
pathToTestImagesDiffList: path.join(pathToBuild, 'list_of_incorrect_images.txt'),
4949

5050
pathToJasmineTests: path.join(pathToRoot, 'test/jasmine/tests'),
51+
pathToJasmineBundleTests: path.join(pathToRoot, 'test/jasmine/bundle_tests'),
5152

5253
uglifyOptions: {
5354
fromString: true,

test/jasmine/bundle_tests/bar_test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@lib/core');
4+
var PlotlyBar = require('@lib/bar');
5+
6+
var createGraphDiv = require('../assets/create_graph_div');
7+
var destroyGraphDiv = require('../assets/destroy_graph_div');
8+
9+
10+
describe('Bundle with bar', function() {
11+
'use strict';
12+
13+
Plotly.register(PlotlyBar);
14+
15+
var mock = require('@mocks/bar_line.json');
16+
17+
beforeEach(function(done) {
18+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
19+
});
20+
21+
afterEach(destroyGraphDiv);
22+
23+
it('should graph scatter traces', function() {
24+
var nodes = d3.selectAll('g.trace.scatter');
25+
26+
expect(nodes.size()).toEqual(1);
27+
});
28+
29+
it('should graph bar traces', function() {
30+
var nodes = d3.selectAll('g.trace.bars');
31+
32+
expect(nodes.size()).toEqual(1);
33+
});
34+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@lib/core');
4+
var PlotlyChoropleth = require('@lib/choropleth');
5+
6+
var createGraphDiv = require('../assets/create_graph_div');
7+
var destroyGraphDiv = require('../assets/destroy_graph_div');
8+
9+
10+
describe('Bundle with choropleth', function() {
11+
'use strict';
12+
13+
Plotly.register(PlotlyChoropleth);
14+
15+
var mock = require('@mocks/geo_multiple-usa-choropleths.json');
16+
17+
beforeEach(function(done) {
18+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
19+
});
20+
21+
afterEach(destroyGraphDiv);
22+
23+
it('should graph choropleth traces', function() {
24+
var nodes = d3.selectAll('g.trace.choropleth');
25+
26+
expect(nodes.size()).toEqual(4);
27+
});
28+
});
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@lib/core');
4+
var PlotlyContour = require('@lib/contour');
5+
6+
var createGraphDiv = require('../assets/create_graph_div');
7+
var destroyGraphDiv = require('../assets/destroy_graph_div');
8+
9+
10+
describe('Bundle with contour', function() {
11+
'use strict';
12+
13+
Plotly.register(PlotlyContour);
14+
15+
var mock = require('@mocks/contour_scatter.json');
16+
17+
beforeEach(function(done) {
18+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
19+
});
20+
21+
afterEach(destroyGraphDiv);
22+
23+
it('should graph scatter traces', function() {
24+
var nodes = d3.selectAll('g.trace.scatter');
25+
26+
expect(nodes.size()).toEqual(1);
27+
});
28+
29+
it('should graph contour traces', function() {
30+
var nodes = d3.selectAll('g.contour');
31+
32+
expect(nodes.size()).toEqual(1);
33+
});
34+
});
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@lib/core');
4+
5+
var createGraphDiv = require('../assets/create_graph_div');
6+
var destroyGraphDiv = require('../assets/destroy_graph_div');
7+
8+
9+
describe('Bundle with core only', function() {
10+
'use strict';
11+
12+
var mock = require('@mocks/bar_line.json');
13+
14+
beforeEach(function(done) {
15+
spyOn(console, 'warn');
16+
17+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
18+
});
19+
20+
afterEach(destroyGraphDiv);
21+
22+
it('should graph scatter traces', function() {
23+
var nodes = d3.selectAll('g.trace.scatter');
24+
25+
expect(nodes.size()).toEqual(mock.data.length);
26+
});
27+
28+
it('should not graph bar traces', function() {
29+
var nodes = d3.selectAll('g.trace.bars');
30+
31+
expect(nodes.size()).toEqual(0);
32+
});
33+
34+
it('should warn users about unregistered bar trace type', function() {
35+
expect(console.warn).toHaveBeenCalled();
36+
});
37+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var d3 = require('d3');
2+
3+
var Plotly = require('@lib/core');
4+
var PlotlyHistogram2dContour = require('@lib/histogram2dcontour');
5+
var PlotlyHistogram = require('@lib/histogram');
6+
7+
var createGraphDiv = require('../assets/create_graph_div');
8+
var destroyGraphDiv = require('../assets/destroy_graph_div');
9+
10+
11+
describe('Bundle with histogram2dcontour and histogram', function() {
12+
'use strict';
13+
14+
Plotly.register([PlotlyHistogram2dContour, PlotlyHistogram]);
15+
16+
var mock = require('@mocks/2dhistogram_contour_subplots.json');
17+
18+
beforeEach(function(done) {
19+
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
20+
});
21+
22+
afterEach(destroyGraphDiv);
23+
24+
it('should graph scatter traces', function() {
25+
var nodes = d3.selectAll('g.trace.scatter');
26+
27+
expect(nodes.size()).toEqual(1);
28+
});
29+
30+
it('should graph contour traces', function() {
31+
var nodes = d3.selectAll('g.contour');
32+
33+
expect(nodes.size()).toEqual(1);
34+
});
35+
36+
it('should graph histogram traces', function() {
37+
var nodes = d3.selectAll('g.bars');
38+
39+
expect(nodes.size()).toEqual(2);
40+
});
41+
});

test/jasmine/karma.conf.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
/*eslint-env node*/
2+
13
// Karma configuration
24

5+
/*
6+
* Test file globs can be passed with an argument.
7+
*
8+
* Example:
9+
*
10+
* $ npm run test-jasmine -- tests/axes_test.js
11+
*
12+
* will only run the tests in axes_test.js
13+
*
14+
*/
15+
var testFileGlob = process.argv[4] ? process.argv[4] : 'tests/*_test.js';
16+
17+
318
function func(config) {
419

520
// level of logging
@@ -21,7 +36,7 @@ func.defaultConfig = {
2136
// list of files / patterns to load in the browser
2237
files: [
2338
'assets/jquery-1.8.3.min.js',
24-
'tests/*_test.js'
39+
testFileGlob
2540
],
2641

2742
// list of files to exclude
@@ -30,9 +45,9 @@ func.defaultConfig = {
3045

3146
// preprocess matching files before serving them to the browser
3247
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
33-
preprocessors: {
34-
'tests/*_test.js': ['browserify']
35-
},
48+
//
49+
// N.B. this field is filled below
50+
preprocessors: {},
3651

3752
// test results reporter to use
3853
// possible values: 'dots', 'progress'
@@ -64,5 +79,7 @@ func.defaultConfig = {
6479
}
6580
};
6681

82+
// browserify the test files
83+
func.defaultConfig.preprocessors[testFileGlob] = ['browserify'];
6784

6885
module.exports = func;

test/syntax_test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ var glob = require('glob');
66

77
var constants = require('../tasks/util/constants');
88

9-
var focusGlobals = ['fdescribe', 'fit', 'xdescribe', 'xit'];
9+
var BLACK_LIST = ['fdescribe', 'fit', 'xdescribe', 'xit'];
1010
var logs = [];
1111

12+
var testGlob = path.join(constants.pathToJasmineTests, '**/*.js');
13+
var bundleTestGlob = path.join(constants.pathToJasmineBundleTests, '**/*.js');
1214

13-
glob(path.join(constants.pathToJasmineTests, '**/*.js'), function(err, files) {
15+
16+
glob('{' + testGlob + ',' + bundleTestGlob + '}', function(err, files) {
1417
files.forEach(function(file) {
1518
var code = fs.readFileSync(file, 'utf-8');
1619

1720
falafel(code, {locations: true}, function(node) {
18-
if(node.type === 'Identifier' && focusGlobals.indexOf(node.name) !== -1) {
21+
if(node.type === 'Identifier' && BLACK_LIST.indexOf(node.name) !== -1) {
1922
logs.push([
2023
path.basename(file),
2124
'[line ' + node.loc.start.line + '] :',

0 commit comments

Comments
 (0)