@@ -30,24 +30,21 @@ function ResponseWrapper (body, init) {
30
30
31
31
const isAPromise = obj => obj && obj . then && ( typeof obj . then === 'function' )
32
32
33
- function resolve ( bodyOrPromise , init ) {
34
- return ( ) =>
35
- isAPromise ( bodyOrPromise ) ?
36
- bodyOrPromise . then ( ( res ) => new ResponseWrapper ( res . body , res . init ) )
37
- : Promise . resolve ( new ResponseWrapper ( bodyOrPromise , init ) )
38
- }
33
+ const resolve = ( bodyOrFunction , init ) => ( ) => isAPromise ( bodyOrFunction ) ?
34
+ bodyOrFunction . then ( ( res ) => new ResponseWrapper ( res . body , res . init ) )
35
+ : Promise . resolve ( new ResponseWrapper ( bodyOrFunction , init ) )
39
36
40
37
const fetch = jest . fn ( )
41
38
fetch . Headers = Headers
42
39
fetch . Response = ResponseWrapper
43
40
fetch . Request = Request
44
- fetch . mockResponse = ( bodyOrPromise , init ) => fetch . mockImplementation ( resolve ( bodyOrPromise , init ) )
41
+ fetch . mockResponse = ( bodyOrFunction , init ) => fetch . mockImplementation ( resolve ( bodyOrFunction , init ) )
45
42
46
43
fetch . mockReject = error => {
47
44
return fetch . mockImplementation ( ( ) => Promise . reject ( error ) )
48
45
}
49
46
50
- const mockResponseOnce = ( bodyOrPromise , init ) => fetch . mockImplementationOnce ( resolve ( bodyOrPromise , init ) )
47
+ const mockResponseOnce = ( bodyOrFunction , init ) => fetch . mockImplementationOnce ( resolve ( bodyOrFunction , init ) )
51
48
52
49
fetch . mockResponseOnce = mockResponseOnce
53
50
@@ -58,7 +55,7 @@ fetch.mockRejectOnce = errorOrPromise => {
58
55
}
59
56
60
57
fetch . mockResponses = ( ...responses ) => {
61
- responses . forEach ( ( [ bodyOrPromise , init ] ) => fetch . mockImplementationOnce ( resolve ( bodyOrPromise , init ) ) )
58
+ responses . forEach ( ( [ bodyOrFunction , init ] ) => fetch . mockImplementationOnce ( resolve ( bodyOrFunction , init ) ) )
62
59
return fetch
63
60
}
64
61
0 commit comments