2
2
3
3
'use strict'
4
4
5
- // const co = require('co')
5
+ const co = require ( 'co' )
6
6
const assert = require ( 'assert' )
7
7
const convert = require ( './index' )
8
+ const compose = require ( 'koa-compose' )
8
9
9
- describe ( 'Koa Convert' , function ( ) {
10
- it ( 'should works' , function ( done ) {
10
+ describe ( 'Koa Convert' , ( ) => {
11
+ it ( 'should works' , ( ) => {
11
12
let call = [ ]
12
13
let ctx = { }
13
14
let mw = convert ( function * ( next ) {
14
15
assert . ok ( ctx === this )
15
16
call . push ( 1 )
16
17
} )
17
18
18
- mw ( ctx , function ( ) {
19
- done ( new Error ( 'this should not be called' ) )
19
+ return mw ( ctx , function ( ) {
20
+ call . push ( 2 )
20
21
} ) . then ( function ( ) {
21
22
assert . deepEqual ( call , [ 1 ] )
22
- done ( )
23
23
} )
24
24
} )
25
25
26
- it ( 'should works with `yield next`' , function ( done ) {
26
+ it ( 'should works with `yield next`' , ( ) => {
27
27
let call = [ ]
28
28
let ctx = { }
29
29
let mw = convert ( function * ( next ) {
@@ -33,16 +33,15 @@ describe('Koa Convert', function () {
33
33
call . push ( 3 )
34
34
} )
35
35
36
- mw ( ctx , function ( ) {
36
+ return mw ( ctx , function ( ) {
37
37
call . push ( 2 )
38
38
return Promise . resolve ( )
39
39
} ) . then ( function ( ) {
40
40
assert . deepEqual ( call , [ 1 , 2 , 3 ] )
41
- done ( )
42
41
} )
43
42
} )
44
43
45
- it ( 'should works with `yield* next`' , function ( done ) {
44
+ it ( 'should works with `yield* next`' , ( ) => {
46
45
let call = [ ]
47
46
let ctx = { }
48
47
let mw = convert ( function * ( next ) {
@@ -52,12 +51,58 @@ describe('Koa Convert', function () {
52
51
call . push ( 3 )
53
52
} )
54
53
55
- mw ( ctx , function ( ) {
54
+ return mw ( ctx , function ( ) {
56
55
call . push ( 2 )
57
56
return Promise . resolve ( )
58
57
} ) . then ( function ( ) {
59
58
assert . deepEqual ( call , [ 1 , 2 , 3 ] )
60
- done ( )
59
+ } )
60
+ } )
61
+
62
+ it ( 'should works with koa-compose' , ( ) => {
63
+ let call = [ ]
64
+ let context = { }
65
+ let _context
66
+ let mw = compose ( [
67
+ function * name ( next ) {
68
+ call . push ( 1 )
69
+ yield next
70
+ call . push ( 11 )
71
+ } ,
72
+ ( ctx , next ) => {
73
+ call . push ( 2 )
74
+ return next ( ) . then ( ( ) => {
75
+ call . push ( 10 )
76
+ } )
77
+ } ,
78
+ function * ( next ) {
79
+ call . push ( 3 )
80
+ yield * next
81
+ call . push ( 9 )
82
+ } ,
83
+ co . wrap ( function * ( ctx , next ) {
84
+ call . push ( 4 )
85
+ yield next ( )
86
+ call . push ( 8 )
87
+ } ) ,
88
+ function * ( next ) {
89
+ try {
90
+ call . push ( 5 )
91
+ yield next
92
+ } catch ( e ) {
93
+ call . push ( 7 )
94
+ }
95
+ } ,
96
+ ( ctx , next ) => {
97
+ _context = ctx
98
+ call . push ( 6 )
99
+ throw new Error ( )
100
+ }
101
+ ] . map ( convert ) )
102
+
103
+ return mw ( context ) . then ( ( ) => {
104
+ assert . equal ( context , _context )
105
+ assert . deepEqual ( call , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] )
61
106
} )
62
107
} )
63
108
} )
0 commit comments