File tree 1 file changed +56
-0
lines changed
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -207,4 +207,60 @@ describe('request', () => {
207
207
done ( )
208
208
} )
209
209
} )
210
+
211
+ it ( 'resolves with function' , async ( ) => {
212
+ fetch . mockResponseOnce ( ( ) => Promise . resolve ( { body : 'ok' } ) )
213
+
214
+ try {
215
+ const response = await request ( )
216
+ expect ( response ) . toEqual ( 'ok' )
217
+ } catch ( e ) {
218
+ throw e
219
+ }
220
+ } )
221
+
222
+ it ( 'resolves with function and timeout' , async ( ) => {
223
+ fetch . mockResponseOnce (
224
+ ( ) => new Promise ( resolve => setTimeout ( ( ) => resolve ( { body : 'ok' } ) ) ) ,
225
+ 100
226
+ )
227
+ try {
228
+ const response = await request ( )
229
+ expect ( response ) . toEqual ( 'ok' )
230
+ } catch ( e ) {
231
+ throw e
232
+ }
233
+ } )
234
+
235
+ it ( 'rejects with function' , async ( ) => {
236
+ const errorData = {
237
+ error :
238
+ 'Uh oh, something has gone wrong. Please tweet us @randomapi about the issue. Thank you.'
239
+ }
240
+ fetch . mockRejectOnce ( ( ) => Promise . reject ( JSON . stringify ( errorData ) ) )
241
+ try {
242
+ await request ( )
243
+ } catch ( error ) {
244
+ expect ( error . message ) . toBe ( errorData . error )
245
+ }
246
+ } )
247
+
248
+ it ( 'rejects with function and timeout' , async ( ) => {
249
+ const errorData = {
250
+ error :
251
+ 'Uh oh, something has gone wrong. Please tweet us @randomapi about the issue. Thank you.'
252
+ }
253
+ fetch . mockRejectOnce (
254
+ ( ) =>
255
+ new Promise ( ( _ , reject ) =>
256
+ setTimeout ( ( ) => reject ( JSON . stringify ( errorData ) ) )
257
+ ) ,
258
+ 100
259
+ )
260
+ try {
261
+ await request ( )
262
+ } catch ( error ) {
263
+ expect ( error . message ) . toBe ( errorData . error )
264
+ }
265
+ } )
210
266
} )
You can’t perform that action at this time.
0 commit comments