Skip to content

Commit

Permalink
improve the test suite for #appendIf() with error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Becheras committed Jan 10, 2017
1 parent 5b35337 commit bbd7755
Showing 1 changed file with 39 additions and 42 deletions.
81 changes: 39 additions & 42 deletions tests/ConnectSequence.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,30 @@ describe('ConnectSequence', function () {

describe('#appendIf()', function () {
var filter
var mid0, mid1, mid2, mid3, mid4

beforeEach(function () {
filter = function (req) { return !!req.foo }
mid0 = function (req, res, next) {
req.mid0 = 'mid0'
next()
}
mid1 = function (req, res, next) {
req.mid1 = 'mid1'
next()
}
mid2 = function (req, res, next) {
req.mid2 = 'mid2'
next()
}
mid3 = function (req, res, next) {
req.mid3 = 'mid3'
next()
}
mid4 = function (req, res, next) {
req.mid4 = 'mid4'
next()
}
})

it('should be chainable', function () {
Expand Down Expand Up @@ -312,7 +333,6 @@ describe('ConnectSequence', function () {
describe('when the previous condition on `req` is `true`', function () {
describe('when passing a normal middleware', function () {
it('should run the given middlewares', function (done) {
var mid0, mid1, mid2, mid3, mid4
next = function () {
expect(req.mid0).to.equal('mid0')
expect(req.mid1).to.equal('mid1')
Expand All @@ -321,58 +341,35 @@ describe('ConnectSequence', function () {
expect(req.mid4).to.equal('mid4')
done()
}
mid0 = function (req, res, next) {
console.log('mid0')
req.mid0 = 'mid0'
next()
}
mid1 = function (req, res, next) {
console.log('mid1')
req.mid1 = 'mid1'
next()
}
mid2 = function (req, res, next) {
console.log('mid2')
req.mid2 = 'mid2'
next()
}
mid3 = function (req, res, next) {
console.log('mid3')
req.mid3 = 'mid3'
next()
}
mid4 = function (req, res, next) {
console.log('mid4')
req.mid4 = 'mid4'
next()
}
filter = function (req) {
return true
}
filter = function (req) { return true }
seq = new ConnectSequence(req, res, next)
seq.appendIf(filter, mid0, mid1, mid2, mid3, mid4)
seq.run()
})
})

describe('when passing a error handler middleware', function () {
it('should run the given error handler', function (done) {
it('should run the given error handler and skip the normal middlewares', function (done) {
var errorHandler = function (err, req, res, next) {
if (err) { req.errorHandler = 'errorHandler' }
next()
}
var errorEmitter = function (req, res, next) {
req.errorEmitter = 'errorEmitter'
next('errorEmitter')
}
next = function () {
expect(res.foo).to.equal('bar')
expect(req.mid0).to.equal('mid0')
expect(req.errorEmitter).to.equal('errorEmitter')
expect(req.mid1).to.be.undefined
expect(req.mid2).to.be.undefined
expect(req.errorHandler).to.equal('errorHandler')
expect(req.mid3).to.equal('mid3')
done()
}
filter = function (req) { return true }
seq = new ConnectSequence(req, res, next)
seq.append(function (req, res, next) {
next('middleware in error')
})
seq.appendIf(function (req) {
return req.foo === 'bar'
}, function (err, req, res, next) {
if (err) {
res.foo = req.foo // 'bar'
}
next()
})
seq.appendIf(filter, mid0, errorEmitter, mid1, mid2, errorHandler, mid3)
seq.run()
})
})
Expand Down

0 comments on commit bbd7755

Please sign in to comment.