Skip to content

Commit

Permalink
feat(core): add logger option to FeatureAppManager, FeatureServiceReg…
Browse files Browse the repository at this point in the history
…istry, and createFeatureHub (#404)

closes #402
  • Loading branch information
unstubbable authored and clebert committed Mar 7, 2019
1 parent 458450b commit b70557e
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 84 deletions.
95 changes: 85 additions & 10 deletions packages/core/src/__tests__/create-feature-hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import {Stubbed, stubMethods} from 'jest-stub-methods';
import {FeatureHubOptions, createFeatureHub} from '../create-feature-hub';
import {FeatureAppDefinition, FeatureAppManager} from '../feature-app-manager';
import {FeatureServiceRegistry} from '../feature-service-registry';
import {
FeatureServiceProviderDefinition,
FeatureServiceRegistry,
SharedFeatureService
} from '../feature-service-registry';
import {Logger} from '../logger';

type MockObject<T extends {}> = {[key in keyof T]: T[key] & jest.Mock};

describe('createFeatureHub()', () => {
let featureHubOptions: FeatureHubOptions;
let stubbedConsole: Stubbed<Console>;

beforeAll(() => {
stubbedConsole = stubMethods(console);
});
let customLogger: MockObject<Logger>;

afterAll(() => {
stubbedConsole.restore();
beforeEach(() => {
customLogger = {info: jest.fn()} as MockObject<Logger>;
featureHubOptions = {logger: customLogger};
});

describe('without any options', () => {
Expand Down Expand Up @@ -55,7 +59,11 @@ describe('createFeatureHub()', () => {

beforeEach(() => {
mockModuleLoader = jest.fn(async () => Promise.resolve());
featureHubOptions = {moduleLoader: mockModuleLoader};

featureHubOptions = {
...featureHubOptions,
moduleLoader: mockModuleLoader
};
});

it('uses the module loader to load the Feature App definition', () => {
Expand Down Expand Up @@ -90,7 +98,10 @@ describe('createFeatureHub()', () => {
});

it('creates a Feature App', () => {
const {featureAppManager} = createFeatureHub('test:integrator');
const {featureAppManager} = createFeatureHub(
'test:integrator',
featureHubOptions
);

const {featureApp} = featureAppManager.getFeatureAppScope(
mockFeatureAppDefinition
Expand Down Expand Up @@ -168,6 +179,7 @@ describe('createFeatureHub()', () => {
}));

featureHubOptions = {
...featureHubOptions,
featureServiceDefinitions: [
{
id: 'test:feature-service',
Expand Down Expand Up @@ -254,4 +266,67 @@ describe('createFeatureHub()', () => {
});
});
});

describe('logging', () => {
let featureAppDefinition: FeatureAppDefinition<unknown>;

let featureServiceDefinitions: FeatureServiceProviderDefinition<
SharedFeatureService
>[];

let expectedLogCalls: string[][];

beforeEach(() => {
featureAppDefinition = {
id: 'test:feature-app',
create: jest.fn(() => ({}))
};

featureServiceDefinitions = [
{id: 'test:feature-service', create: () => ({'1.0.0': jest.fn()})}
];

expectedLogCalls = [
[
'The Feature Service "test:feature-service" has been successfully registered by consumer "test:integrator".'
],
['The Feature App "test:feature-app" has been successfully created.']
];
});

describe('with a custom logger', () => {
it('logs messages using the custom logger', () => {
const {featureAppManager} = createFeatureHub('test:integrator', {
featureServiceDefinitions,
logger: customLogger
});

featureAppManager.getFeatureAppScope(featureAppDefinition);

expect(customLogger.info.mock.calls).toEqual(expectedLogCalls);
});
});

describe('without a custom logger', () => {
let stubbedConsole: Stubbed<Console>;

beforeEach(() => {
stubbedConsole = stubMethods(console);
});

afterEach(() => {
stubbedConsole.restore();
});

it('logs messages using the console', () => {
const {featureAppManager} = createFeatureHub('test:integrator', {
featureServiceDefinitions
});

featureAppManager.getFeatureAppScope(featureAppDefinition);

expect(stubbedConsole.stub.info.mock.calls).toEqual(expectedLogCalls);
});
});
});
});
62 changes: 46 additions & 16 deletions packages/core/src/__tests__/feature-app-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import {
} from '..';
import {ExternalsValidator} from '../externals-validator';
import {FeatureAppModule} from '../internal/is-feature-app-module';
import {Logger} from '../logger';

interface MockFeatureServiceRegistry extends FeatureServiceRegistry {
registerFeatureServices: jest.Mock;
bindFeatureServices: jest.Mock;
}

type MockObject<T extends {}> = {[key in keyof T]: T[key] & jest.Mock};

describe('FeatureAppManager', () => {
let featureAppManager: FeatureAppManager;
let mockFeatureServiceRegistry: MockFeatureServiceRegistry;
Expand All @@ -27,10 +30,10 @@ describe('FeatureAppManager', () => {
let mockFeatureAppModule: FeatureAppModule | undefined;
let mockFeatureAppCreate: jest.Mock;
let mockFeatureApp: {};
let stubbedConsole: Stubbed<Console>;
let customLogger: MockObject<Logger>;

beforeEach(() => {
stubbedConsole = stubMethods(console);
customLogger = {info: jest.fn()} as MockObject<Logger>;

mockFeatureServicesBindingUnbind = jest.fn();

Expand All @@ -53,16 +56,15 @@ describe('FeatureAppManager', () => {
ExternalsValidator
>) as ExternalsValidator;

featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry);
});

afterEach(() => {
stubbedConsole.restore();
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
logger: customLogger
});
});

describe('#getAsyncFeatureAppDefinition', () => {
beforeEach(() => {
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
logger: customLogger,
moduleLoader: mockModuleLoader
});
});
Expand All @@ -72,11 +74,11 @@ describe('FeatureAppManager', () => {
'/example.js'
);

expect(stubbedConsole.stub.info.mock.calls).toEqual([]);
expect(customLogger.info.mock.calls).toEqual([]);

await asyncFeatureAppDefinition.promise;

expect(stubbedConsole.stub.info.mock.calls).toEqual([
expect(customLogger.info.mock.calls).toEqual([
[
'The Feature App module at the url "/example.js" has been successfully loaded.'
]
Expand Down Expand Up @@ -130,7 +132,9 @@ describe('FeatureAppManager', () => {
});

it('throws an error if no module loader was provided', () => {
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry);
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
logger: customLogger
});

expect(() =>
featureAppManager.getAsyncFeatureAppDefinition('/example.js')
Expand Down Expand Up @@ -158,7 +162,8 @@ describe('FeatureAppManager', () => {
const instanceConfig = 'testInstanceConfig';

featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
configs: {[mockFeatureAppDefinition.id]: config}
configs: {[mockFeatureAppDefinition.id]: config},
logger: customLogger
});

featureAppManager.getFeatureAppScope(mockFeatureAppDefinition, {
Expand Down Expand Up @@ -201,7 +206,8 @@ describe('FeatureAppManager', () => {
describe('with an ExternalsValidator provided to the FeatureAppManager', () => {
beforeEach(() => {
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
externalsValidator: mockExternalsValidator
externalsValidator: mockExternalsValidator,
logger: customLogger
});
});

Expand Down Expand Up @@ -357,7 +363,7 @@ describe('FeatureAppManager', () => {
it('logs an info message after creation', () => {
featureAppManager.getFeatureAppScope(mockFeatureAppDefinition);

expect(stubbedConsole.stub.info.mock.calls).toEqual([
expect(customLogger.info.mock.calls).toEqual([
['The Feature App "testId" has been successfully created.']
]);
});
Expand Down Expand Up @@ -393,7 +399,7 @@ describe('FeatureAppManager', () => {
idSpecifier: 'testIdSpecifier'
});

expect(stubbedConsole.stub.info.mock.calls).toEqual([
expect(customLogger.info.mock.calls).toEqual([
[
'The Feature App "testId:testIdSpecifier" has been successfully created.'
]
Expand Down Expand Up @@ -505,7 +511,8 @@ describe('FeatureAppManager', () => {
describe('#preloadFeatureApp', () => {
beforeEach(() => {
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
moduleLoader: mockModuleLoader
moduleLoader: mockModuleLoader,
logger: customLogger
});
});

Expand All @@ -520,11 +527,34 @@ describe('FeatureAppManager', () => {
});

it('throws an error if no module loader was provided', () => {
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry);
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry, {
logger: customLogger
});

expect(() =>
featureAppManager.getAsyncFeatureAppDefinition('/example.js')
).toThrowError(new Error('No module loader provided.'));
});
});

describe('without a custom logger', () => {
let stubbedConsole: Stubbed<Console>;

beforeEach(() => {
stubbedConsole = stubMethods(console);
featureAppManager = new FeatureAppManager(mockFeatureServiceRegistry);
});

afterEach(() => {
stubbedConsole.restore();
});

it('logs messages using the console', () => {
featureAppManager.getFeatureAppScope(mockFeatureAppDefinition);

expect(stubbedConsole.stub.info.mock.calls).toEqual([
['The Feature App "testId" has been successfully created.']
]);
});
});
});
Loading

0 comments on commit b70557e

Please sign in to comment.