-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspec.index.js
166 lines (144 loc) · 5.79 KB
/
spec.index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const index = require(APP_ROOT);
const express = require('express');
describe('hmpo-app', () => {
it('should export setup functions and libs', () => {
index.should.contain.all.keys([
'setup',
'middleware',
'config',
'logger',
'redisClient',
'translation',
'nunjucks',
'linkedFiles',
'featureFlag'
]);
});
describe('setup', () => {
let app;
beforeEach(() => {
app = {
use: sinon.stub(),
get: sinon.stub()
};
sinon.stub(express, 'Router');
express.Router.onCall(0).returns('staticRouter');
express.Router.onCall(1).returns('router');
express.Router.onCall(2).returns('errorRouter');
sinon.stub(index.config, 'get');
sinon.stub(index.config, 'setup');
sinon.stub(index.logger, 'setup');
sinon.stub(index.redisClient, 'setup');
sinon.stub(index.middleware, 'setup').returns(app);
sinon.stub(index.middleware, 'session');
sinon.stub(index.middleware, 'errorHandler');
sinon.stub(index.middleware, 'listen');
});
afterEach(() => {
express.Router.restore();
index.config.get.restore();
index.config.setup.restore();
index.logger.setup.restore();
index.redisClient.setup.restore();
index.middleware.setup.restore();
index.middleware.session.restore();
index.middleware.errorHandler.restore();
index.middleware.listen.restore();
});
it('calls config.setup', () => {
index.setup();
index.config.setup.should.have.been.calledWithExactly(undefined);
});
it('calls config.setup with options', () => {
index.setup({ config: { option: true} });
index.config.setup.should.have.been.calledWithExactly({ option: true });
});
it('should not call config.setup if option is false', () => {
index.setup({ config: false });
index.config.setup.should.not.have.been.called;
});
it('calls logger.setup with options', () => {
index.config.get.withArgs('logs').returns({ config: true });
index.setup({ logs: { option: true } });
index.logger.setup.should.have.been.calledWithExactly({
option: true,
config: true
});
});
it('should not call logger.setup if option is false', () => {
index.setup({ logs: false });
index.logger.setup.should.not.have.been.called;
});
it('calls redisClient.setup with options', () => {
index.config.get.withArgs('redis').returns({ config: true });
index.setup({ redis: { option: true } });
index.redisClient.setup.should.have.been.calledWithExactly({
option: true,
config: true
});
});
it('should not call redisClient.setup if option is false', () => {
index.setup({ redis: false });
index.redisClient.setup.should.not.have.been.called;
});
it('calls middleware.setup with options', () => {
index.config.get.withArgs().returns({ config: true });
index.setup({ option: true });
index.middleware.setup.should.have.been.calledWithExactly({
option: true,
config: true
});
});
it('calls middleware.session with options', () => {
index.config.get.withArgs('session').returns({ config: true });
index.setup({ session: { option: true } });
index.middleware.session.should.have.been.calledWithExactly(app, {
option: true,
config: true
});
});
it('should not call middleware.session if option is false', () => {
index.setup({ session: false });
index.middleware.session.should.not.have.been.called;
});
it('calls middleware.errorHandler with options', () => {
index.config.get.withArgs('errors').returns({ config: true });
index.setup({ errors: { option: true } });
index.middleware.errorHandler.should.have.been.calledWithExactly(app, {
option: true,
config: true
});
});
it('should not call middleware.errorHandler if option is false', () => {
index.setup({ errors: false });
index.middleware.errorHandler.should.not.have.been.called;
});
it('should call middlewareSetupFn if option is defined', () => {
const callbackStub = sinon.stub();
index.setup({ middlewareSetupFn: callbackStub});
callbackStub.should.have.been.called;
});
it('calls middleware.listen with options', () => {
index.config.get.withArgs('host').returns('hostname');
index.config.get.withArgs('port').returns(1234);
index.setup({ port: 5678});
index.middleware.listen.should.have.been.calledWithExactly(app, {
port: 5678,
host: 'hostname'
});
});
it('should not call middleware.listen if port is false', () => {
index.setup({ port: false });
index.middleware.listen.should.not.have.been.called;
});
it('returns apps and routers', () => {
const routers = index.setup();
routers.should.eql({
app,
staticRouter: 'staticRouter',
router: 'router',
errorRouter: 'errorRouter'
});
});
});
});