Skip to content

Commit a693bb2

Browse files
committed
Change register function and add test
1 parent 5f26169 commit a693bb2

File tree

3 files changed

+81
-29
lines changed

3 files changed

+81
-29
lines changed

src/index.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,19 @@ exports.Queue = Plotly.Queue;
4646
// export d3 used in the bundle
4747
exports.d3 = require('d3');
4848

49-
Plotly.register({
50-
traces: {
51-
bar: require('./traces/bar'),
52-
box: require('./traces/box'),
53-
heatmap: require('./traces/heatmap'),
54-
histogram: require('./traces/histogram'),
55-
histogram2d: require('./traces/histogram2d'),
56-
histogram2dcontour: require('./traces/histogram2dcontour'),
57-
pie: require('./traces/pie'),
58-
contour: require('./traces/contour'),
59-
scatter3d: require('./traces/scatter3d'),
60-
surface: require('./traces/surface'),
61-
mesh3d: require('./traces/mesh3d'),
62-
scattergeo: require('./traces/scattergeo'),
63-
choropleth: require('./traces/choropleth'),
64-
scattergl: require('./traces/scattergl')
65-
}
66-
});
49+
Plotly.register([
50+
require('./traces/bar'),
51+
require('./traces/box'),
52+
require('./traces/heatmap'),
53+
require('./traces/histogram'),
54+
require('./traces/histogram2d'),
55+
require('./traces/histogram2dcontour'),
56+
require('./traces/pie'),
57+
require('./traces/contour'),
58+
require('./traces/scatter3d'),
59+
require('./traces/surface'),
60+
require('./traces/mesh3d'),
61+
require('./traces/scattergeo'),
62+
require('./traces/choropleth'),
63+
require('./traces/scattergl')
64+
]);

src/plotly.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ var Gl3d = require('./plots/gl3d');
4646
Plots.registerSubplot(Gl3d);
4747

4848
var Gl2d = require('./plots/gl2d');
49-
5049
Plots.registerSubplot(Gl2d);
50+
5151
exports.Axes = require('./plots/cartesian/axes');
5252
exports.Fx = require('./plots/cartesian/graph_interact');
5353
exports.micropolar = require('./plots/polar/micropolar');
@@ -65,21 +65,27 @@ exports.Titles = require('./components/titles');
6565
exports.Legend = require('./components/legend');
6666
exports.ModeBar = require('./components/modebar');
6767

68-
// Traces are registered in index.js
69-
exports.register = function register(options) {
70-
if(!options || options === {}){
71-
throw new Error('You must pass a config object to Plotly.register.');
68+
exports.register = function register(_modules) {
69+
if(!_modules){
70+
throw new Error('No argument passed to Plotly.register.');
71+
} else if(_modules && !Array.isArray(_modules)){
72+
_modules = [_modules];
7273
}
7374

74-
for(var trace in options.traces){
75-
var newTrace = options.traces[trace];
76-
Plots.register(newTrace, newTrace._type, newTrace._categories, newTrace._meta);
75+
76+
for(var i = 0; i < _modules.length; i++){
77+
var newModule = _modules[i];
78+
79+
if(newModule && newModule.moduleType !== 'trace'){
80+
throw new Error('Invalid module was attempted to be registered!');
81+
} else {
82+
Plots.register(newModule, newModule.name, newModule.categories, newModule.meta);
83+
}
7784
}
7885
};
7986

80-
exports.register({
81-
traces: [require('./traces/scatter')]
82-
});
87+
88+
exports.register(require('./traces/scatter'));
8389

8490
// Scatter is the only trace included by default
8591
exports.Scatter = Plots.getModule('scatter');

test/jasmine/tests/register_test.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var Plotly = require('@src/plotly');
2+
3+
describe('the register function', function() {
4+
5+
it('should throw an error when no argument is given', function() {
6+
expect(function() {
7+
Plotly.register();
8+
}).toThrowError(Error, 'No argument passed to Plotly.register.');
9+
});
10+
11+
it('should work with a single module', function() {
12+
var mockTrace1 = {
13+
moduleType: 'trace',
14+
name: 'mockTrace1',
15+
meta: 'Meta string',
16+
categories: ['categories', 'array']
17+
};
18+
19+
expect(function() {
20+
Plotly.register(mockTrace1);
21+
}).not.toThrow();
22+
23+
expect(Plotly.Plots.getModule('mockTrace1')).toBe(mockTrace1);
24+
});
25+
26+
it('should work with an array of modules', function() {
27+
var mockTrace2 = {
28+
moduleType: 'trace',
29+
name: 'mockTrace2',
30+
meta: 'Meta string',
31+
categories: ['categories', 'array']
32+
};
33+
34+
expect(function() {
35+
Plotly.register([mockTrace2]);
36+
}).not.toThrow();
37+
38+
expect(Plotly.Plots.getModule('mockTrace2')).toBe(mockTrace2);
39+
});
40+
41+
it('should throw an error when an invalid module is given', function() {
42+
var invalidTrace = { moduleType: 'invalid' };
43+
44+
expect(function() {
45+
Plotly.register([invalidTrace]);
46+
}).toThrowError(Error, 'Invalid module was attempted to be registered!');
47+
});
48+
});

0 commit comments

Comments
 (0)