Skip to content

Commit

Permalink
more test
Browse files Browse the repository at this point in the history
  • Loading branch information
gyson committed Oct 30, 2015
1 parent 26a740e commit fe32dd4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
4 changes: 4 additions & 0 deletions package.json
Expand Up @@ -25,7 +25,11 @@
"co": "^4.6.0"
},
"devDependencies": {
"koa-compose": "^3.0.0",
"mocha": "^2.3.3",
"standard": "^5.3.1"
},
"engines": {
"node": ">= 4"
}
}
69 changes: 57 additions & 12 deletions test.js
Expand Up @@ -2,28 +2,28 @@

'use strict'

// const co = require('co')
const co = require('co')
const assert = require('assert')
const convert = require('./index')
const compose = require('koa-compose')

describe('Koa Convert', function () {
it('should works', function (done) {
describe('Koa Convert', () => {
it('should works', () => {
let call = []
let ctx = {}
let mw = convert(function * (next) {
assert.ok(ctx === this)
call.push(1)
})

mw(ctx, function () {
done(new Error('this should not be called'))
return mw(ctx, function () {
call.push(2)
}).then(function () {
assert.deepEqual(call, [1])
done()
})
})

it('should works with `yield next`', function (done) {
it('should works with `yield next`', () => {
let call = []
let ctx = {}
let mw = convert(function * (next) {
Expand All @@ -33,16 +33,15 @@ describe('Koa Convert', function () {
call.push(3)
})

mw(ctx, function () {
return mw(ctx, function () {
call.push(2)
return Promise.resolve()
}).then(function () {
assert.deepEqual(call, [1, 2, 3])
done()
})
})

it('should works with `yield* next`', function (done) {
it('should works with `yield* next`', () => {
let call = []
let ctx = {}
let mw = convert(function * (next) {
Expand All @@ -52,12 +51,58 @@ describe('Koa Convert', function () {
call.push(3)
})

mw(ctx, function () {
return mw(ctx, function () {
call.push(2)
return Promise.resolve()
}).then(function () {
assert.deepEqual(call, [1, 2, 3])
done()
})
})

it('should works with koa-compose', () => {
let call = []
let context = {}
let _context
let mw = compose([
function * name (next) {
call.push(1)
yield next
call.push(11)
},
(ctx, next) => {
call.push(2)
return next().then(() => {
call.push(10)
})
},
function * (next) {
call.push(3)
yield* next
call.push(9)
},
co.wrap(function * (ctx, next) {
call.push(4)
yield next()
call.push(8)
}),
function * (next) {
try {
call.push(5)
yield next
} catch (e) {
call.push(7)
}
},
(ctx, next) => {
_context = ctx
call.push(6)
throw new Error()
}
].map(convert))

return mw(context).then(() => {
assert.equal(context, _context)
assert.deepEqual(call, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
})
})
})

0 comments on commit fe32dd4

Please sign in to comment.