Skip to content

Commit

Permalink
improve the test suite for #appendIf() with error handlers (4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Becheras committed Jan 10, 2017
1 parent 045022f commit 174bfbc
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions tests/ConnectSequence.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,18 @@ describe('ConnectSequence', function () {
describe('#appendIf()', function () {
var filter
var mid0, mid1, mid2, mid3, mid4
var errorEmitter, errorHandler

beforeEach(function () {
filter = function (req) { return !!req.foo }
errorEmitter = function (req, res, next) {
req.errorEmitter = 'errorEmitter'
next('errorEmitter')
}
errorHandler = function (err, req, res, next) {
if (err) { req.errorHandler = 'errorHandler' }
next()
}
mid0 = function (req, res, next) {
req.mid0 = 'mid0'
next()
Expand Down Expand Up @@ -331,6 +340,10 @@ describe('ConnectSequence', function () {
})

describe('when the previous condition on `req` is `true`', function () {
beforeEach(function () {
filter = function (req) { return true }
})

describe('when passing a normal middleware', function () {
it('should run the given middlewares', function (done) {
next = function () {
Expand All @@ -341,23 +354,25 @@ describe('ConnectSequence', function () {
expect(req.mid4).to.equal('mid4')
done()
}
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 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')
it('should run the given error handler', function (done) {
next = function () {
expect(req.errorHandler).to.equal('errorHandler')
done()
}
seq = new ConnectSequence(req, res, next)
seq.append(errorEmitter)
seq.appendIf(filter, errorHandler)
seq.run()
})

it('should run the given error handler and skip the normal middlewares', function (done) {
next = function () {
expect(req.mid0).to.equal('mid0')
expect(req.errorEmitter).to.equal('errorEmitter')
Expand Down Expand Up @@ -399,27 +414,16 @@ describe('ConnectSequence', function () {
describe('when passing a error handler middleware', function () {
it('should not run the given error handler', function (done) {
next = function () {
expect(res.foo).to.not.equal('bar')
expect(req.errorHandler).to.be.undefined
done()
}
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 = new ConnectSequence(req, res, next)
seq.append(errorEmitter)
seq.appendIf(filter, errorHandler)
seq.run()
})

it('should not run any given error handler or 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')
Expand Down

0 comments on commit 174bfbc

Please sign in to comment.