-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
by-property controller middleware handles rejected promises (#19)
- Loading branch information
1 parent
bd4ece3
commit 82b9d9a
Showing
5 changed files
with
48 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,58 @@ | ||
const controller = require('../../../middlewares/controllers/by-property') | ||
const path = require('path') | ||
|
||
describe('Controller', () => { | ||
|
||
it('should return middleware', () => { | ||
let req, res, next | ||
|
||
beforeEach(() => { | ||
req = sinon.spy() | ||
res = sinon.spy() | ||
next = sinon.spy() | ||
}) | ||
|
||
it('should build middleware', done => { | ||
// given | ||
const operation = {'x-swagger-router-controller': 'example'} | ||
const middleware = controller({dir: __dirname})(operation) | ||
|
||
// when | ||
const result = middleware(req, res, next) | ||
|
||
// then | ||
result.then(({req: reqArg, res: resArg, next: nextArg}) => { | ||
expect(reqArg).to.eql(req) | ||
expect(resArg).to.eql(res) | ||
expect(nextArg).to.eql(next) | ||
}).then(done) | ||
}) | ||
|
||
it('should build middleware from nested file', done => { | ||
// given | ||
const operation = {'x-swagger-router-controller': 'package'} | ||
const operation = {'x-swagger-router-controller': 'dir.example'} | ||
const middleware = controller({dir: __dirname, delimiter: /\./g})(operation) | ||
|
||
// when | ||
const middleware = controller({dir: path.join(__dirname, '../../..')})(operation) | ||
const result = middleware(req, res, next) | ||
|
||
// then | ||
expect(middleware).to.equal(require('../../../package')) | ||
result.then(({req: reqArg, res: resArg, next: nextArg}) => { | ||
expect(reqArg).to.eql(req) | ||
expect(resArg).to.eql(res) | ||
expect(nextArg).to.eql(next) | ||
}).then(done) | ||
}) | ||
|
||
it('should return middleware', () => { | ||
it('should pass errors from rejected Promises to next', done => { | ||
// given | ||
const operation = {'x-swagger-router-controller': 'test.globals'} | ||
const operation = {'x-swagger-router-controller': 'rejected-promise-controller'} | ||
const middleware = controller({dir: __dirname})(operation) | ||
|
||
// when | ||
const middleware = controller({dir: path.join(__dirname, '../../..'), delimiter: /\./g})(operation) | ||
const result = middleware(req, res, next) | ||
|
||
// then | ||
expect(middleware).to.equal(require('../../globals')) | ||
result.then(() => { | ||
expect(next.calledOnceWith(sinon.match.instanceOf(Error))).to.be.true | ||
}).then(done) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const example = require('../example') | ||
|
||
module.exports = example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = (req, res, next) => ({req, res, next}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = () => Promise.reject(new Error()) |