Skip to content

Commit

Permalink
feat: log warning for unintentional registry use (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilzona authored Apr 9, 2019
1 parent 26e036f commit 3e9621e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 13 additions & 1 deletion packages/picasso.js/src/core/utils/__tests__/registry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import registry from '../registry';

describe('Registry', () => {
let reg;
let logger;

beforeEach(() => {
reg = registry();
logger = {
warn: sinon.spy()
};
reg = registry('', 'myRegistry', logger);
});

describe('add', () => {
Expand All @@ -30,5 +35,12 @@ describe('Registry', () => {
const registered = reg.register('a', () => {});
expect(registered).to.equal(false);
});

it('should warn if key does not exist', () => {
reg.register('spelledCorrect', () => {});
const attempt = reg('spelledWrong');
expect(attempt).to.equal(undefined);
expect(logger.warn).to.have.been.calledOnce;
});
});
});
8 changes: 6 additions & 2 deletions packages/picasso.js/src/core/utils/registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function registryFactory(parentRegistry) {
export default function registryFactory(parentRegistry, registerName = 'unspecified', logger) {
let defaultValue;
const reg = {};
const parent = parentRegistry || {
Expand Down Expand Up @@ -72,7 +72,11 @@ export default function registryFactory(parentRegistry) {
if (typeof value !== 'undefined') {
return add(key, value);
}
return get(key || defaultValue);
const ret = get(key);
if (logger && typeof ret === 'undefined') {
logger.warn(`${key} does not exist in ${registerName} registry`);
}
return ret || get(defaultValue);
}

registry.add = add;
Expand Down
14 changes: 7 additions & 7 deletions packages/picasso.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,38 @@ function pic(config = {}, registries = {}) {
* Component registry
* @type {registry}
*/
component: registry(registries.component),
component: registry(registries.component, 'component', logger),
/**
* Data registry
* @type {registry}
*/
data: registry(registries.data),
data: registry(registries.data, 'data', logger),
/**
* Formatter registry
* @type {registry}
*/
formatter: registry(registries.formatter),
formatter: registry(registries.formatter, 'formatter', logger),
/**
* Interaction registry
* @type {registry}
*/
interaction: registry(registries.interaction),
interaction: registry(registries.interaction, 'interaction', logger),
/**
* Renderer registry
* @type {registry}
*/
renderer: renderer(registries.renderer),
renderer: renderer(registries.renderer, 'renderer', logger),
/**
* Scale registry
* @type {registry}
*/
scale: registry(registries.scale),
scale: registry(registries.scale, 'scale', logger),
/**
* Symbol registry
* @type {registry}
* @private
*/
symbol: registry(registries.symbol),
symbol: registry(registries.symbol, 'symbol', logger),
// -- misc --
/**
* log some some stuff
Expand Down

0 comments on commit 3e9621e

Please sign in to comment.