Skip to content

Commit dedc93e

Browse files
committed
refactor(example-log-extension): automatically load LogComponent from Mixin
- automatically mount LogComponent - rename LogLevelMixin to LogMixin - add new test and refactor existing tests
1 parent d41275b commit dedc93e

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

packages/example-log-extension/src/component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {EXAMPLE_LOG_BINDINGS} from './keys';
76
import {Component, ProviderMap} from '@loopback/core';
8-
import {TimerProvider, LogActionProvider} from './';
7+
import {EXAMPLE_LOG_BINDINGS} from './keys';
8+
import {LogActionProvider} from './providers/log-action.provider';
9+
import {TimerProvider} from './providers/timer.provider';
910

1011
export class LogComponent implements Component {
1112
providers?: ProviderMap = {

packages/example-log-extension/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
export * from './decorators/log.decorator';
7-
export * from './mixins/log-level.mixin';
7+
export * from './mixins/log.mixin';
88
export * from './providers/log-action.provider';
99
export * from './providers/timer.provider';
1010
export * from './component';

packages/example-log-extension/src/mixins/log-level.mixin.ts renamed to packages/example-log-extension/src/mixins/log.mixin.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55

66
import {Constructor} from '@loopback/context';
77
import {EXAMPLE_LOG_BINDINGS} from '../keys';
8+
import {LogComponent} from '../component';
89

910
/**
1011
* A mixin class for Application that can bind logLevel from `options`.
1112
* Also provides .logLevel() to bind application wide logLevel.
1213
* Functions with a log level set to logLevel or higher sill log data
1314
*
1415
* ```ts
15-
* class MyApplication extends LogLevelMixin(Application) {}
16+
* class MyApplication extends LogMixin(Application) {}
1617
* ```
1718
*/
1819
// tslint:disable-next-line:no-any
19-
export function LogLevelMixin<T extends Constructor<any>>(superClass: T) {
20+
export function LogMixin<T extends Constructor<any>>(superClass: T) {
2021
return class extends superClass {
2122
// A mixin class has to take in a type any[] argument!
2223
// tslint:disable-next-line:no-any
2324
constructor(...args: any[]) {
2425
super(...args);
25-
if (!this.options) this.options = {};
26-
27-
if (this.options.logLevel) {
26+
if (this.options && this.options.logLevel) {
2827
this.logLevel(this.options.logLevel);
2928
}
29+
this.component(LogComponent);
3030
}
3131

3232
/**

packages/example-log-extension/test/acceptance/log.extension.acceptance.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import {
1818
} from '@loopback/rest';
1919
import {get, param} from '@loopback/openapi-v3';
2020
import {
21-
LogComponent,
22-
LogLevelMixin,
21+
LogMixin,
2322
LOG_LEVEL,
2423
log,
2524
EXAMPLE_LOG_BINDINGS,
@@ -46,7 +45,7 @@ describe('log extension acceptance test', () => {
4645
let server: RestServer;
4746
let spy: SinonSpy;
4847

49-
class LogApp extends LogLevelMixin(RestApplication) {}
48+
class LogApp extends LogMixin(RestApplication) {}
5049

5150
const debugMatch: string = chalk.white(
5251
'DEBUG: /debug :: MyController.debug() => debug called',
@@ -240,8 +239,6 @@ describe('log extension acceptance test', () => {
240239

241240
async function createApp() {
242241
app = new LogApp();
243-
app.component(LogComponent);
244-
245242
app.bind(EXAMPLE_LOG_BINDINGS.TIMER).to(timer);
246243
app.bind(EXAMPLE_LOG_BINDINGS.LOGGER).to(logToMemory);
247244
server = await app.getServer(RestServer);

packages/example-log-extension/test/unit/mixins/log-level.mixin.unit.ts renamed to packages/example-log-extension/test/unit/mixins/log.mixin.unit.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,42 @@
55

66
import {expect} from '@loopback/testlab';
77
import {Application} from '@loopback/core';
8-
import {LogLevelMixin, LOG_LEVEL, EXAMPLE_LOG_BINDINGS} from '../../..';
8+
import {LogMixin, LOG_LEVEL, EXAMPLE_LOG_BINDINGS} from '../../..';
9+
10+
describe('LogMixin (unit)', () => {
11+
let myApp: AppWithLogMixin;
12+
13+
beforeEach(getApp);
914

10-
describe('LogLevelMixin (unit)', () => {
1115
it('mixed class has .logLevel()', () => {
12-
const myApp = new AppWithLogLevel();
13-
expect(typeof myApp.logLevel).to.be.eql('function');
16+
expect(myApp.logLevel).to.be.a.Function();
1417
});
1518

1619
it('binds LogLevel from constructor', () => {
17-
const myApp = new AppWithLogLevel({
20+
myApp = new AppWithLogMixin({
1821
logLevel: LOG_LEVEL.ERROR,
1922
});
20-
21-
expectLogLevelToBeBound(myApp);
23+
expectLogLevelToBeBound();
2224
});
2325

2426
it('bind logLevel from app.logLevel()', () => {
25-
const myApp = new AppWithLogLevel();
2627
myApp.logLevel(LOG_LEVEL.ERROR);
27-
expectLogLevelToBeBound(myApp);
28+
expectLogLevelToBeBound();
2829
});
2930

30-
class AppWithLogLevel extends LogLevelMixin(Application) {}
31+
it('adds LogComponent to target class', () => {
32+
const boundComponent = myApp.find('components.*').map(b => b.key);
33+
expect(boundComponent).to.containEql('components.LogComponent');
34+
});
3135

32-
function expectLogLevelToBeBound(myApp: Application) {
36+
class AppWithLogMixin extends LogMixin(Application) {}
37+
38+
function expectLogLevelToBeBound() {
3339
const logLevel = myApp.getSync(EXAMPLE_LOG_BINDINGS.APP_LOG_LEVEL);
3440
expect(logLevel).to.be.eql(LOG_LEVEL.ERROR);
3541
}
42+
43+
function getApp() {
44+
myApp = new AppWithLogMixin();
45+
}
3646
});

0 commit comments

Comments
 (0)