@@ -44,6 +44,66 @@ describe('Koa Compose', function () {
4444 } )
4545 } )
4646
47+ it ( 'should wrap functions with wrappers' , function ( ) {
48+ var arr = [ ]
49+ var stack = [ ]
50+
51+ stack . push ( function * ( context , next ) {
52+ arr . push ( 1 )
53+ yield wait ( 1 )
54+ yield next ( )
55+ yield wait ( 1 )
56+ arr . push ( 6 )
57+ } )
58+
59+ stack . push ( function * ( context , next ) {
60+ arr . push ( 2 )
61+ yield wait ( 1 )
62+ yield next ( )
63+ yield wait ( 1 )
64+ arr . push ( 5 )
65+ } )
66+
67+ stack . push ( function * ( context , next ) {
68+ arr . push ( 3 )
69+ yield wait ( 1 )
70+ yield next ( )
71+ yield wait ( 1 )
72+ arr . push ( 4 )
73+ } )
74+
75+ return compose ( stack ) ( {
76+ wrappers : [ co . wrap ]
77+ } ) . then ( function ( ) {
78+ arr . should . eql ( [ 1 , 2 , 3 , 4 , 5 , 6 ] )
79+ } )
80+ } )
81+
82+ it ( 'should use wrappers in the correct order' , function ( ) {
83+ let completed = [ ]
84+ let stack = [ ]
85+ let arr = [ ]
86+
87+ stack . push ( function * ( context , next ) {
88+ arr . push ( 1 )
89+ yield next ( )
90+ } )
91+
92+ stack . push ( function * ( context , next ) {
93+ arr . push ( 2 )
94+ yield next ( )
95+ } )
96+
97+ return compose ( stack ) ( {
98+ wrappers : [ co . wrap , function ( fn ) {
99+ return ( ctx , next ) => fn ( ctx , next ) . then ( completed . push ( fn ) )
100+ } ]
101+ } ) . then ( function ( ) {
102+ arr . should . eql ( [ 1 , 2 ] )
103+ completed . length . should . eql ( stack . length )
104+ } )
105+ } )
106+
47107 it ( 'should only accept an array' , function ( ) {
48108 var err
49109 try {
@@ -54,6 +114,26 @@ describe('Koa Compose', function () {
54114 return ( err ) . should . be . instanceof ( TypeError )
55115 } )
56116
117+ it ( 'should throw an error if calling generators or non-functions' , function ( ) {
118+ var stack = [
119+ function * ( ) { }
120+ ]
121+ return compose ( stack ) ( { } ) . then ( function ( ) {
122+ throw new Error ( 'No error thrown' )
123+ } ) . catch ( function ( err ) {
124+ ( err ) . should . be . instanceof ( TypeError )
125+ } ) . then ( function ( ) {
126+ stack = [
127+ { key : 'value' }
128+ ]
129+ return compose ( stack ) ( { } )
130+ } ) . then ( function ( ) {
131+ throw new Error ( 'No error thrown' )
132+ } ) . catch ( function ( err ) {
133+ ( err ) . should . be . instanceof ( TypeError )
134+ } )
135+ } )
136+
57137 it ( 'should work with 0 middleware' , function ( ) {
58138 return compose ( [ ] ) ( { } )
59139 } )
0 commit comments