Skip to content

Commit 83584c2

Browse files
committed
cover most unit test
1 parent 1ce8d19 commit 83584c2

File tree

6 files changed

+145
-20
lines changed

6 files changed

+145
-20
lines changed

server/src/api/Server.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import 'reflect-metadata';
2-
import App from './services/App';
2+
import WebAppService from './services/WebAppService';
33
import * as log from '../common/log';
44
import * as http from 'http';
55
import {container, AppService} from './ioc/Ioc';
66
import { InversifyExpressServer } from 'inversify-express-utils';
77

88
class Server {
9-
constructor(app: App){
9+
constructor(app: WebAppService){
1010

1111
let server = new InversifyExpressServer(container, null, null, app.getApp());
1212
server.build();
1313

1414
const notificationService = app.getNotificationService();
1515

1616
const httpServer = http.createServer(app.getApp());
17-
notificationService.listen(httpServer);
18-
17+
notificationService.listen(httpServer);
1918

2019
httpServer.listen(app.get("port"), () => {
2120
var msg = `App is running at http://localhost:${app.get("port")} in ${app.get("env")} mode`;
2221
log.logInfo(msg);
22+
app.startSubscribe();
2323
});
2424
}
2525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import 'reflect-metadata';
3+
import TYPES from '../../common/constant/Types';
4+
import { Container, decorate, injectable } from 'inversify';
5+
import ReceiveController from './ReceiveController';
6+
import { Request, Response } from 'express';
7+
8+
describe('ReceiveController', ()=>{
9+
let controller : ReceiveController;
10+
let container: Container | null;
11+
12+
const notifierMock = jest.fn();
13+
const decodeMock = jest.fn().mockImplementation(()=>{
14+
return {
15+
body: "test"
16+
}
17+
});
18+
19+
class NotifierMock{
20+
notify = notifierMock
21+
}
22+
class DecoderMock{
23+
decode = decodeMock
24+
}
25+
26+
decorate(injectable(), NotifierMock);
27+
decorate(injectable(), DecoderMock);
28+
29+
beforeEach(()=>{
30+
container = new Container();
31+
container.bind(TYPES.INotificationService).to(NotifierMock);
32+
container.bind(TYPES.IDecoder).to(DecoderMock);
33+
34+
controller = new ReceiveController(container.get(TYPES.INotificationService), container.get(TYPES.IDecoder));
35+
});
36+
37+
describe('receive', ()=>{
38+
test('receive new message should decode and notify', async ()=>{
39+
let request = {body:{body:"test"}} as Request;
40+
let response = {send: ()=>{}} as Response;
41+
42+
await controller.receive( request, response);
43+
44+
expect(notifierMock.mock.calls.length).toBe(1);
45+
expect(decodeMock.mock.calls.length).toBe(1);
46+
});
47+
});
48+
49+
50+
afterEach(() => {
51+
container = null;
52+
});
53+
54+
});

server/src/api/controllers/ReceiveController.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ import {
55

66
import { Request, Response } from 'express';
77
import * as log from '../../common/log';
8-
import INotificationService from '../services/INotificationService';
9-
108

119
import IDecoder from '../../common/decoders/IDecoder';
1210
import TYPES from '../../common/constant/Types';
11+
import INotifier from '../../common/subscribers/INotifier';
1312

1413

1514
@controller('/receive')
1615
class ReceiveController implements interfaces.Controller {
1716
constructor(
18-
@inject(TYPES.INotificationService) private notificationService: INotificationService,
17+
@inject(TYPES.INotificationService) private notificationService: INotifier,
1918
@inject(TYPES.IDecoder) private decoder: IDecoder
20-
){
21-
22-
}
19+
){}
2320

2421
@httpPost('/')
2522
async receive(req: Request, res: Response) {

server/src/api/ioc/Ioc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import INotificationService from '../services/INotificationService';
44
import NotificationService from '../services/NotificationService';
55
import ISubscriber from '../../common/subscribers/ISubscriber';
66
import AzureEventHubs from '../../common/subscribers/AzureEventHubSubscriber';
7-
import App from '../services/App';
7+
import App from '../services/WebAppService';
88
import Decoder from '../../common/decoders/Decoder';
99
import IDecoder from '../../common/decoders/IDecoder';
1010
import TYPES from '../../common/constant/Types';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
import 'reflect-metadata';
3+
import WebAppService from "./WebAppService";
4+
import TYPES from "../../common/constant/Types";
5+
import { Container, decorate, injectable } from "inversify";
6+
7+
describe('WebAppService', ()=>{
8+
let appService : WebAppService;
9+
let container: Container | null;
10+
11+
const notificationMock = jest.fn();
12+
const subscriberMock = jest.fn();
13+
const registerMock = jest.fn();
14+
const listenMock = jest.fn();
15+
16+
class NotificationMock{
17+
notify = notificationMock
18+
listen = listenMock
19+
}
20+
class SubscriberMock{
21+
registerNotifier = registerMock
22+
subscribe = subscriberMock
23+
}
24+
25+
decorate(injectable(), NotificationMock);
26+
decorate(injectable(), SubscriberMock);
27+
28+
beforeEach(()=>{
29+
container = new Container();
30+
container.bind(TYPES.INotificationService).to(NotificationMock);
31+
container.bind(TYPES.ISubscriber).to(SubscriberMock);
32+
container.bind<WebAppService>(TYPES.WebAppService).to(WebAppService);
33+
appService = container.get<WebAppService>(TYPES.WebAppService);
34+
});
35+
36+
describe('startSubscribe', ()=>{
37+
test('subscriber should be called', async ()=>{
38+
appService.startSubscribe();
39+
expect(registerMock.mock.calls.length).toBe(1);
40+
expect(subscriberMock.mock.calls.length).toBe(1);
41+
});
42+
});
43+
44+
describe('getNotificationService', ()=>{
45+
test('return notification service', async ()=>{
46+
let notifier = appService.getNotificationService();
47+
expect(notifier.listen).toEqual(listenMock);
48+
});
49+
});
50+
51+
describe('get', ()=>{
52+
test('port should be 5000', async ()=>{
53+
expect(appService.get("port")).toEqual(5000);
54+
});
55+
});
56+
57+
describe('getApp', ()=>{
58+
test('should throw no error', async ()=>{
59+
expect(()=>{
60+
appService.getApp();
61+
}).not.toThrowError();
62+
});
63+
});
64+
65+
66+
afterEach(() => {
67+
container = null;
68+
});
69+
70+
});

server/src/api/services/App.ts renamed to server/src/api/services/WebAppService.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import '../controllers/Controllers';
1010
import TYPES from '../../common/constant/Types';
1111

1212
@injectable()
13-
class App {
13+
class WebAppService {
1414
private app: Application;
1515
private notificationService: INotificationService;
16-
16+
private subscriberService: ISubscriber;
1717
constructor(
1818
@inject(TYPES.INotificationService) notificationService: INotificationService,
1919
@inject(TYPES.ISubscriber) subscriberService: ISubscriber ) {
2020

2121
this.notificationService = notificationService;
22+
this.subscriberService = subscriberService;
2223

2324
const app = express();
2425
app.set("port", process.env.PORT || 5000);
@@ -27,21 +28,24 @@ class App {
2728
app.use(bodyParser.urlencoded({ extended: true }));
2829
app.use(this.allowAnyRequest);
2930

30-
subscriberService.registerNotifier(notificationService);
31-
subscriberService.subscribe();
32-
this.app = app;
31+
this.subscriberService.registerNotifier(notificationService);
3332

33+
this.app = app;
3434
}
35-
36-
37-
allowAnyRequest(req,res,next){
35+
36+
/* istanbul ignore next */
37+
private allowAnyRequest(req,res,next){
3838
res.header("Access-Control-Allow-Origin", "*/*");
3939
res.header("Access-Control-Allow-Headers", "X-Requested-With");
4040
res.header("Access-Control-Allow-Headers", "Content-Type");
4141
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
4242
next();
4343
}
4444

45+
startSubscribe(){
46+
this.subscriberService.subscribe();
47+
}
48+
4549
getApp(){
4650
return this.app;
4751
}
@@ -56,4 +60,4 @@ class App {
5660
}
5761

5862

59-
export default App;
63+
export default WebAppService;

0 commit comments

Comments
 (0)