Skip to content

Commit

Permalink
Allow enabling/disabling plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed May 19, 2020
1 parent 75e9835 commit 3324cb1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/plug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Tracker from '@croct/sdk/facade/trackerFacade';
import {EvaluationOptions} from '@croct/sdk/facade/evaluatorFacade';
import Sdk, {Configuration as SdkFacadeConfiguration} from '@croct/sdk/facade/sdkFacade';
import {formatCause} from '@croct/sdk/error';
import {describe} from '@croct/sdk/validation';
import {Plugin, PluginArguments, PluginFactory} from './plugin';

interface PluginConfigurations {
Expand Down Expand Up @@ -100,8 +101,23 @@ export class GlobalPlug implements Plug {
continue;
}

if (typeof options !== 'boolean' && (options === null || typeof options !== 'object')) {
logger.error(
`Invalid options for plugin "${name}", `
+ `expected either boolean or object but got ${describe(options)}`,
);

continue;
}

if (options === false) {
logger.warn(`Plugin "${name}" is declared but not enabled`);

continue;
}

const args: PluginArguments = {
options: options,
options: options === true ? {} : options,
sdk: {
tracker: sdk.tracker,
evaluator: sdk.evaluator,
Expand Down
66 changes: 58 additions & 8 deletions test/plug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ describe('The Croct plug', () => {
plugins: {foo: true},
});

expect(logger.error).toBeCalledWith('[Croct] Failed to initialize plugin "foo": failure');
expect(logger.error).toBeCalledWith(
expect.stringContaining('Failed to initialize plugin "foo": failure'),
);
});

test('should log an error if a plugin is not registered', async () => {
test('should log an error if a plugin is not registered', () => {
const logger: Logger = {
debug: jest.fn(),
info: jest.fn(),
Expand All @@ -89,14 +91,62 @@ describe('The Croct plug', () => {
logger: logger,
});

await croct.plugged;

expect(logger.error).toHaveBeenCalledWith(
expect.stringContaining('Plugin "foo" is not registered.'),
);
});

test('should initialize the specified plugins', () => {
test('should log an error if a plugin options is invalid', () => {
const logger: Logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

const factory: PluginFactory = jest.fn();

croct.extend('foo', factory);

croct.plug({
appId: appId,
plugins: {foo: null},
logger: logger,
});

expect(factory).not.toHaveBeenCalled();

expect(logger.error).toHaveBeenCalledWith(
expect.stringContaining('Invalid options for plugin "foo", expected either boolean or object but got null'),
);
});

test('should not initialize disabled plugins', () => {
const factory: PluginFactory = jest.fn();

croct.extend('foo', factory);

const logger: Logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

croct.plug({
appId: appId,
logger: logger,
plugins: {foo: false},
});

expect(factory).not.toHaveBeenCalled();

expect(logger.warn).toBeCalledWith(
expect.stringContaining('Plugin "foo" is declared but not enabled'),
);
});

test('should initialize the declared plugins', () => {
const fooFactory: PluginFactory = jest.fn().mockImplementation(({sdk}) => {
sdk.getLogger('namespace');
sdk.getTabStorage('namespace');
Expand All @@ -121,14 +171,14 @@ describe('The Croct plug', () => {
appId: appId,
plugins: {
foo: true,
bar: false,
bar: {flag: true},
},
});

expect(initialize).toBeCalledWith(config);

expect(fooFactory).toBeCalledWith(expect.objectContaining({options: true}));
expect(barFactory).toBeCalledWith(expect.objectContaining({options: false}));
expect(fooFactory).toBeCalledWith(expect.objectContaining({options: {}}));
expect(barFactory).toBeCalledWith(expect.objectContaining({options: {flag: true}}));

expect(getLogger).toBeCalledWith('Plugin', 'foo', 'namespace');
expect(getTabStorage).toBeCalledWith('Plugin', 'foo', 'namespace');
Expand Down

0 comments on commit 3324cb1

Please sign in to comment.