This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.spec.js
79 lines (72 loc) · 2.69 KB
/
app.spec.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
const expect = require('chai').expect;
const injector = require('injectdeps');
const bunyan = require('bunyan');
const sinon = require('sinon');
require('chai').use(require('sinon-chai'));
const logger = require('./test/logger');
const rawRouter = (req,res,next) => next();
const mockValidator = ()=>(req,res,next)=>next();
const errorHandler = ()=>()=>{};
function standardBindings() {
return injector.getContainer()
.bindName('bunyan').toPlainObject(bunyan)
.bindName('logger').toObject(logger)
.bindName('app.config').toPlainObject({})
.bindName('container').toContainer()
.bindName('app.errorHandler').toPlainObject(errorHandler())
.bindName('swagger.validator').toPlainObject(mockValidator)
.bindName('app').toObject(require('./app'));
}
describe('app', () => {
it('should be initialised with the provided tools', (done) => {
const router = sinon.stub().returns(rawRouter);
const rawApp = {
get: sinon.spy(),
use: sinon.spy(),
listen: sinon.spy()
};
const engine = sinon.stub().returns(rawApp);
const tools = Promise.resolve({swaggerMetadata: () => null});
const container = standardBindings()
.bindName('swagger.tools').toPlainObject(tools)
.bindName('app.engine').toPlainObject(engine)
.bindName('swagger.router').toPlainObject(router);
const appPromise = container.newObject('app');
expect(appPromise).to.be.a('promise');
appPromise
.then((app) => {
expect(router).to.have.been.calledOnce;
expect(app).to.have.a.property('listen').that.is.a('function');
// use was used once for swagger-metadata,
// once for swagger-validation and once for the router
expect(rawApp.use).to.have.been.callCount(4);
expect(rawApp.get).to.have.been.calledOnce;
done();
})
.catch((err) => done(err));
});
it('should not initialise the app if the swagger tools fail to load', (done) => {
const router = sinon.stub().returns(rawRouter);
const rawApp = {
use: sinon.spy(),
listen: sinon.spy()
};
const engine = sinon.stub().returns(rawApp);
const tools = Promise.reject(Error('Bad swagger file'));
const container = standardBindings()
.bindName('swagger.tools').toPlainObject(tools)
.bindName('app.engine').toPlainObject(engine)
.bindName('swagger.router').toPlainObject(router);
const appPromise = container.newObject('app');
expect(appPromise).to.be.a('promise');
appPromise
.then(() => {
done(Error('It should not initialise the app'));
})
.catch((err) => {
expect(err).to.be.an('error');
expect(engine).to.have.not.been.called;
done();
});
});
});