@@ -12,7 +12,7 @@ let router
12
12
beforeEach ( ( ) => {
13
13
router = Router ( )
14
14
. get ( '/' , ( ) => 'index' )
15
- . get ( '/items' , ( ) => [ ] )
15
+ . get ( '/items' , ( ) => [ 1 , 2 , 3 ] )
16
16
. get ( '/throw' , ( r ) => r . a . b . c )
17
17
} )
18
18
@@ -30,7 +30,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
30
30
31
31
it ( 'formats as json' , async ( ) => {
32
32
let response = await flow ( router ) ( request ( '/items' ) ) . then ( r => r . json ( ) )
33
- expect ( response ) . toEqual ( [ ] )
33
+ expect ( response ) . toEqual ( [ 1 , 2 , 3 ] )
34
34
} )
35
35
36
36
it ( 'catches errors' , async ( ) => {
@@ -47,6 +47,49 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
47
47
} )
48
48
49
49
describe ( 'OPTIONS' , ( ) => {
50
+ describe ( 'errors?: Function | false' , ( ) => {
51
+ it ( 'should handle custom error function' , async ( ) => {
52
+ const customError = ( ) => ( { status : 418 , body : 'I\'m a teapot' } )
53
+ let response = await flow ( router , { errors : customError } ) ( request ( '/throw' ) )
54
+ expect ( response . status ) . toBe ( 418 )
55
+ expect ( response . body ) . toBe ( 'I\'m a teapot' )
56
+ } )
57
+
58
+ it ( 'should not handle errors if set to false' , async ( ) => {
59
+ const errorHandler = vi . fn ( )
60
+ let response = await flow ( router , { errors : false } ) ( request ( '/throw' ) ) . catch ( errorHandler )
61
+ expect ( errorHandler ) . toHaveBeenCalled ( )
62
+ } )
63
+ } )
64
+
65
+ describe ( 'format?: Function | false' , ( ) => {
66
+ it ( 'should handle custom format function' , async ( ) => {
67
+ const customFormat = ( data ) => ( { status : 200 , body : `num items = ${ data . length } ` } )
68
+ let response = await flow ( router , { format : customFormat } ) ( request ( '/items' ) )
69
+ expect ( response . status ) . toBe ( 200 )
70
+ expect ( response . body ) . toBe ( 'num items = 3' )
71
+ } )
72
+
73
+ it ( 'should not format response if set to false' , async ( ) => {
74
+ let response = await flow ( router , { format : false } ) ( request ( '/items' ) )
75
+ expect ( response . body ) . toBeUndefined ( )
76
+ } )
77
+ } )
78
+
79
+ describe ( 'notFound?: RouteHandler | false' , ( ) => {
80
+ it ( 'should handle custom notFound function' , async ( ) => {
81
+ const customNotFound = ( ) => ( { status : 404 , body : 'Custom Not Found' } )
82
+ let response = await flow ( router , { notFound : customNotFound } ) ( request ( '/missing' ) )
83
+ expect ( response . status ) . toBe ( 404 )
84
+ expect ( response . body ) . toBe ( 'Custom Not Found' )
85
+ } )
86
+
87
+ it ( 'should not handle notFound if set to false' , async ( ) => {
88
+ let response = await flow ( router , { notFound : false } ) ( request ( '/missing' ) )
89
+ expect ( response . status ) . toBeUndefined ( )
90
+ } )
91
+ } )
92
+
50
93
describe ( 'cors?: CorsOptions | true' , ( ) => {
51
94
it ( 'will embed CORS headers if provided' , async ( ) => {
52
95
let response = await flow ( router , {
@@ -68,14 +111,14 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
68
111
describe ( 'error?: RouteHandler | false' , ( ) => {
69
112
it ( 'does not catch internally if set to false' , async ( ) => {
70
113
let onError = vi . fn ( )
71
- let response = await flow ( router , { handleErrors : false } ) ( request ( '/throw' ) ) . catch ( onError )
114
+ let response = await flow ( router , { errors : false } ) ( request ( '/throw' ) ) . catch ( onError )
72
115
73
116
expect ( onError ) . toHaveBeenCalled ( )
74
117
} )
75
118
76
119
it ( 'can reshape errors if provided' , async ( ) => {
77
120
let response = await flow ( router , {
78
- handleErrors : ( ) => error ( 418 , 'CUSTOM' ) ,
121
+ errors : ( ) => error ( 418 , 'CUSTOM' ) ,
79
122
} ) ( request ( '/throw' ) )
80
123
81
124
expect ( response . status ) . toBe ( 418 )
@@ -85,14 +128,14 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
85
128
describe ( 'format?: ResponseFormatter | false' , ( ) => {
86
129
it ( 'does not catch internally if set to false' , async ( ) => {
87
130
let onError = vi . fn ( )
88
- let response = await flow ( router , { handleErrors : false } ) ( request ( '/throw' ) ) . catch ( onError )
131
+ let response = await flow ( router , { errors : false } ) ( request ( '/throw' ) ) . catch ( onError )
89
132
90
133
expect ( onError ) . toHaveBeenCalled ( )
91
134
} )
92
135
93
136
it ( 'can reshape errors if provided' , async ( ) => {
94
137
let response = await flow ( router , {
95
- handleErrors : ( ) => error ( 418 , 'CUSTOM' ) ,
138
+ errors : ( ) => error ( 418 , 'CUSTOM' ) ,
96
139
} ) ( request ( '/throw' ) )
97
140
98
141
expect ( response . status ) . toBe ( 418 )
@@ -102,7 +145,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
102
145
describe ( 'notFound?: RouteHandler | false' , ( ) => {
103
146
it ( 'uses a custom 404 handler' , async ( ) => {
104
147
let response = await flow ( router , {
105
- handleNotFound : ( ) => error ( 418 , 'CUSTOM' ) ,
148
+ notFound : ( ) => error ( 418 , 'CUSTOM' ) ,
106
149
} ) ( request ( '/missing' ) ) . then ( r => r . json ( ) )
107
150
108
151
expect ( response . status ) . toBe ( 418 )
@@ -111,7 +154,7 @@ describe('flow(router: RouterType, options: FlowOptions): RequestHandler', () =>
111
154
112
155
it ( 'if set to false, will not add notFound handler (allow undefined passthrough)' , async ( ) => {
113
156
let response = await flow ( router , {
114
- handleNotFound : false ,
157
+ notFound : false ,
115
158
format : false ,
116
159
} ) ( request ( '/missing' ) )
117
160
0 commit comments