Skip to content

Commit

Permalink
spec and implement new ConnectSequence().appendList()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Becheras committed Dec 28, 2016
1 parent b1001aa commit fd4f73e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
21 changes: 19 additions & 2 deletions lib/ConnectSequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function ConnectSequence () {
ConnectSequence.run = run

ConnectSequence.prototype = {
append: append
append: append,
appendList: appendList
}

/**
Expand Down Expand Up @@ -52,10 +53,26 @@ function append () {
var i
for (i = 0; i < arguments.length; i++) {
if (typeof arguments[i] !== 'function') {
throw new TypeError('All given middleware must be a function')
var type = typeof arguments[i]
var errMsg = 'Given middlewares must be functions. "' + type + '" given.'
throw new TypeError(errMsg)
}
}
for (i = 0; i < arguments.length; i++) {
this.middlewares.push(arguments[i])
}
}

/**
* Append many middlewares in an array
* @method
* @param {array}
*/
function appendList (middlewares) {
if (!Array.isArray(middlewares)) {
var errorMsg = 'First argument must be an array of middlewares. '
errorMsg += typeof middlewares + ' given.'
throw new TypeError(errorMsg)
}
return this.append.apply(this, middlewares)
}
83 changes: 80 additions & 3 deletions tests/ConnectSequence.prototype.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ describe('ConnectSequence.prototype', function () {

it('should have a "append" method', function () {
expect(ConnectSequence.prototype).to.have.property('append')
expect(ConnectSequence.prototype.append).to.be.a('function')
})

// it('should have a "appendList" method', function () {
// expect(ConnectSequence.prototype).to.have.property('append')
// })
it('should have a "appendList" method', function () {
expect(ConnectSequence.prototype).to.have.property('appendList')
expect(ConnectSequence.prototype.appendList).to.be.a('function')
})
})

describe('new ConnectSequence()', function () {
Expand Down Expand Up @@ -105,3 +107,78 @@ describe('new ConnectSequence().append(mid_1, mid_2, ..., mid_n)', function () {
expect(seq.middlewares.length).to.be.equal(2)
})
})

describe('new ConnectSequence().appendList([mid_1, mid_2, ..., mid_n])', function () {
it('should throw TypeError if the first argument is not an array', function () {
var funcs = [
function () {
var seq = new ConnectSequence()
seq.appendList('not an array')
},
function () {
var seq = new ConnectSequence()
seq.appendList({foo: 'not an array'})
},
function () {
var seq = new ConnectSequence()
seq.appendList(function () { return 'not an array' })
}
]
for (var i = 0; i < funcs.length; i++) {
expect(funcs[i]).to.throw(TypeError)
}
})

it('should augments the length of the middlewares array by the number of given middlewares', function () {
var seq = new ConnectSequence()
var mid = function (req, res, next) { next() }
seq.appendList([mid])
expect(seq.middlewares.length).to.equal(1)
seq.appendList([mid])
expect(seq.middlewares.length).to.equal(2)
seq.appendList([mid, mid])
expect(seq.middlewares.length).to.equal(4)
seq.appendList([])
expect(seq.middlewares.length).to.equal(4)
seq.appendList([mid, mid, mid])
expect(seq.middlewares.length).to.equal(7)
})

it('should keep the same order of the given middlewares', function () {
var seq = new ConnectSequence()
var mid0 = function (req, res, next) { next() }
var mid1 = function (req, res, next) { next() }
var mid2 = function (req, res, next) { next() }
var mid3 = function (req, res, next) { next() }
var mid4 = function (req, res, next) { next() }
var mid5 = function (req, res, next) { next() }
var mid6 = function (req, res, next) { next() }
seq.appendList([mid0])
seq.appendList([mid1, mid2])
seq.appendList([])
seq.appendList([mid3, mid4, mid5])
seq.appendList([mid6])
var mids = [mid0, mid1, mid2, mid3, mid4, mid5, mid6]
for (var i = 0; i < mids.length; i++) {
var mid = mids[i]
expect(seq.middlewares[i]).to.equal(mid)
}
})

it('should throw a TypeError and reject all middlewares if one is not a function', function () {
var seq = new ConnectSequence()
var mid1 = function (req, res, next) { next() }
var mid2 = 'not a function'
var mid3 = function (req, res, next) { next() }
var cantAppend = function () {
seq.appendList([mid1, mid2, mid3])
}
var shouldAppend = function () {
seq.appendList([mid1, mid3])
}
expect(cantAppend).to.throw(TypeError)
expect(seq.middlewares.length).to.be.equal(0)
expect(shouldAppend).to.not.throw(Error)
expect(seq.middlewares.length).to.be.equal(2)
})
})

0 comments on commit fd4f73e

Please sign in to comment.