Skip to content

Commit 2a7738e

Browse files
committed
api: improve register transform logging and tests
1 parent 3e74c80 commit 2a7738e

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/plot_api/register.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,22 @@ function registerTransformModule(newModule) {
6969
throw new Error(prefix + ' is missing a *transform* or *calcTransform* method.');
7070
}
7171

72+
// For more info, see:
73+
// https://github.com/plotly/plotly.js/pull/978#pullrequestreview-2403353
74+
if(hasTransform && hasCalcTransform) {
75+
Lib.log([
76+
prefix + ' has both a *transform* and *calcTransform* methods.',
77+
'Please note that all *transform* methods are executed',
78+
'before all *calcTransform* methods.'
79+
].join(' '));
7280
}
81+
7382
if(!Lib.isPlainObject(newModule.attributes)) {
7483
Lib.log(prefix + ' registered without an *attributes* object.');
7584
}
85+
7686
if(typeof newModule.supplyDefaults !== 'function') {
77-
Lib.log(prefix + ' registered without a *supplyDefaults* function.');
87+
Lib.log(prefix + ' registered without a *supplyDefaults* method.');
7888
}
7989

8090
Registry.transformsRegistry[newModule.name] = newModule;

test/jasmine/tests/register_test.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,11 @@ describe('the register function', function() {
206206
expect(function() {
207207
Plotly.register([invalidTrace]);
208208
}).toThrowError(Error, 'Invalid module was attempted to be registered!');
209+
210+
expect(Registry.transformsRegistry['mah-transform']).toBeUndefined();
209211
});
210212

211-
it('should throw when if transform module is invalid', function() {
213+
it('should throw when if transform module is invalid (1)', function() {
212214
var missingTransformName = {
213215
moduleType: 'transform'
214216
};
@@ -217,15 +219,23 @@ describe('the register function', function() {
217219
Plotly.register(missingTransformName);
218220
}).toThrowError(Error, 'Transform module *name* must be a string.');
219221

222+
expect(Registry.transformsRegistry['mah-transform']).toBeUndefined();
223+
});
224+
225+
it('should throw when if transform module is invalid (2)', function() {
220226
var missingTransformFunc = {
221227
moduleType: 'transform',
222228
name: 'mah-transform'
223229
};
224230

225231
expect(function() {
226232
Plotly.register(missingTransformFunc);
227-
}).toThrowError(Error, 'Transform module mah-transform is missing a *transform* function.');
233+
}).toThrowError(Error, 'Transform module mah-transform is missing a *transform* or *calcTransform* method.');
234+
235+
expect(Registry.transformsRegistry['mah-transform']).toBeUndefined();
236+
});
228237

238+
it('should not throw when transform module is valid (1)', function() {
229239
var transformModule = {
230240
moduleType: 'transform',
231241
name: 'mah-transform',
@@ -238,4 +248,33 @@ describe('the register function', function() {
238248

239249
expect(Registry.transformsRegistry['mah-transform']).toBeDefined();
240250
});
251+
252+
it('should not throw when transform module is valid (2)', function() {
253+
var transformModule = {
254+
moduleType: 'transform',
255+
name: 'mah-transform',
256+
calcTransform: function() {}
257+
};
258+
259+
expect(function() {
260+
Plotly.register(transformModule);
261+
}).not.toThrow();
262+
263+
expect(Registry.transformsRegistry['mah-transform']).toBeDefined();
264+
});
265+
266+
it('should not throw when transform module is valid (3)', function() {
267+
var transformModule = {
268+
moduleType: 'transform',
269+
name: 'mah-transform',
270+
transform: function() {},
271+
calcTransform: function() {}
272+
};
273+
274+
expect(function() {
275+
Plotly.register(transformModule);
276+
}).not.toThrow();
277+
278+
expect(Registry.transformsRegistry['mah-transform']).toBeDefined();
279+
});
241280
});

0 commit comments

Comments
 (0)