Skip to content
This repository was archived by the owner on Feb 2, 2018. It is now read-only.

Commit 5661a28

Browse files
committed
test: added acceptance tests + renamed files
Added acceptance test as there is now a concrete Logger implementation interface.
1 parent 93ba5a0 commit 5661a28

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
2+
// Node module: loopback-next-extension-starter
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
import {Application, inject} from '@loopback/core';
7+
import {RestComponent, RestServer, get, param} from '@loopback/rest';
8+
import {sinon, Client, createClientForHandler} from '@loopback/testlab';
9+
import {LoggerMixin, Logger, LogArgs} from '../../..';
10+
11+
describe('logger.mixin (acceptance)', () => {
12+
// tslint:disable-next-line:no-any
13+
let app: any;
14+
let server: RestServer;
15+
// tslint:disable-next-line:no-any
16+
let spy: any;
17+
18+
beforeEach(createApp);
19+
beforeEach(createLogger);
20+
beforeEach(createController);
21+
beforeEach(getServerFromApp);
22+
beforeEach(() => {
23+
spy = sinon.spy(console, 'log');
24+
});
25+
26+
afterEach(() => {
27+
spy.restore();
28+
});
29+
30+
it('.log() logs request information', async () => {
31+
const client: Client = createClientForHandler(server.handleHttp);
32+
await client.get('/?name=John').expect(200, 'Hi John');
33+
sinon.assert.calledWith(spy, sinon.match('log: hello() called with: John'));
34+
});
35+
36+
it('.error() logs request information', async () => {
37+
const client: Client = createClientForHandler(server.handleHttp);
38+
await client.get('/error?name=John').expect(200, 'Hi John');
39+
sinon.assert.calledWith(
40+
spy,
41+
sinon.match('error: hello() called with: John'),
42+
);
43+
});
44+
45+
function createApp() {
46+
class LoggerApplication extends LoggerMixin(Application) {
47+
// tslint:disable-next-line:no-any
48+
constructor(...args: any[]) {
49+
super({
50+
components: [RestComponent],
51+
});
52+
}
53+
}
54+
55+
app = new LoggerApplication();
56+
}
57+
58+
function createLogger() {
59+
class ColorLogger implements Logger {
60+
log(...args: LogArgs) {
61+
const data = 'log: ' + args.join(' ');
62+
console.log(data);
63+
}
64+
65+
error(...args: LogArgs) {
66+
const data = args.join(' ');
67+
// log in red color
68+
console.log('\x1b[31m error: ' + data + '\x1b[0m');
69+
}
70+
}
71+
72+
app.logger(ColorLogger);
73+
}
74+
75+
function createController() {
76+
class MyController {
77+
constructor(@inject('loggers.ColorLogger') protected log: Logger) {}
78+
79+
@get('/')
80+
@param.query.string('name')
81+
hello(name: string) {
82+
this.log.log('hello() called with:', name);
83+
return `Hi ${name}`;
84+
}
85+
86+
@get('/error')
87+
@param.query.string('name')
88+
helloError(name: string) {
89+
this.log.error('hello() called with:', name);
90+
return `Hi ${name}`;
91+
}
92+
}
93+
94+
app.controller(MyController);
95+
}
96+
97+
async function getServerFromApp() {
98+
server = await app.getServer(RestServer);
99+
}
100+
});
File renamed without changes.

0 commit comments

Comments
 (0)