Skip to content

Commit c7abcdb

Browse files
author
lsimone
committed
added async support
* promises are accepted as first argument instead of body * README and typescript interfaces updated
1 parent 103c409 commit c7abcdb

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ If you are using [Create-React-App](https://github.com/facebookincubator/create-
7171
* `fetch.mockReject(error): fetch` - Mock all fetch calls, letting them fail directly
7272
* `fetch.mockRejectOnce(error): fetch` - Let the next fetch call fail directly
7373

74+
### Promises
75+
76+
Instead of passing body, it is possible to pass a promise too.
77+
The promise should resolve with an object containing body and init
78+
79+
i.e:
80+
81+
```
82+
fetch.mockResponse(new Promise((resolve, reject) => {
83+
return callMyApi().then(res => ({body: res}))
84+
}))
85+
```
86+
87+
The same goes for rejects:
88+
89+
```
90+
fetch.mockReject(new Promise((resolve, reject) => {
91+
return doMyAsyncJob().then(res => res.errorToRaise)
92+
}))
93+
```
94+
95+
7496
### Mock utilities
7597

7698
* `fetch.resetMocks()` - Clear previously set mocks so they do not bleed into other mocks

src/index.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ declare module "jest-fetch-mock" {
1010

1111
interface Fetch {
1212
(input?: string | Request, init?: RequestInit): Promise<Response>;
13-
mockResponse(body: string, init?: MockParams): Fetch;
14-
mockResponseOnce(body: string, init?: MockParams): Fetch;
13+
mockResponse(body: string | Promise, init?: MockParams): Fetch;
14+
mockResponseOnce(body: string | Promise, init?: MockParams): Fetch;
1515
mockResponses(...responses : Array<[string] | [string, MockParams]>): Fetch;
16-
mockReject(error?: Error): Fetch;
17-
mockRejectOnce(error?: Error): Fetch;
16+
mockReject(error?: Error | Promise): Fetch;
17+
mockRejectOnce(error?: Error | Promise): Fetch;
1818
resetMocks(): void;
1919
}
2020

src/index.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
require('isomorphic-fetch')
22

33
if (!Promise) {
4-
Promise = require('promise-polyfill');
4+
Promise = require('promise-polyfill')
55
} else if (!Promise.finally) {
6-
Promise.finally = require('promise-polyfill').finally;
6+
Promise.finally = require('promise-polyfill').finally
77
}
88

99
const ActualResponse = Response
1010

11-
function ResponseWrapper(body, init) {
11+
function ResponseWrapper (body, init) {
1212
if (body && typeof body.constructor === 'function' && body.constructor.__isFallback) {
1313
const response = new ActualResponse(null, init)
1414
response.body = body
@@ -28,40 +28,37 @@ function ResponseWrapper(body, init) {
2828
return new ActualResponse(body, init)
2929
}
3030

31+
const isAPromise = obj => obj && obj.then && (typeof obj.then === 'function')
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+
}
39+
3140
const fetch = jest.fn()
3241
fetch.Headers = Headers
3342
fetch.Response = ResponseWrapper
3443
fetch.Request = Request
35-
fetch.mockResponse = (body, init) => {
36-
return fetch.mockImplementation(() =>
37-
Promise.resolve(new ResponseWrapper(body, init))
38-
)
39-
}
44+
fetch.mockResponse = (bodyOrPromise, init) => fetch.mockImplementation(resolve(bodyOrPromise, init))
4045

4146
fetch.mockReject = error => {
4247
return fetch.mockImplementation(() => Promise.reject(error))
4348
}
4449

45-
const mockResponseOnce = (body, init) => {
46-
return fetch.mockImplementationOnce(() =>
47-
Promise.resolve(new ResponseWrapper(body, init))
48-
)
49-
}
50+
const mockResponseOnce = (bodyOrPromise, init) => fetch.mockImplementationOnce(resolve(bodyOrPromise, init))
5051

5152
fetch.mockResponseOnce = mockResponseOnce
5253

5354
fetch.once = mockResponseOnce
5455

55-
fetch.mockRejectOnce = error => {
56-
return fetch.mockImplementationOnce(() => Promise.reject(error))
56+
fetch.mockRejectOnce = errorOrPromise => {
57+
return fetch.mockImplementationOnce(() => isAPromise(errorOrPromise) ? errorOrPromise : Promise.reject(error))
5758
}
5859

5960
fetch.mockResponses = (...responses) => {
60-
responses.forEach(([body, init]) => {
61-
fetch.mockImplementationOnce(() =>
62-
Promise.resolve(new ResponseWrapper(body, init))
63-
)
64-
})
61+
responses.forEach(([bodyOrPromise, init]) => fetch.mockImplementationOnce(resolve(bodyOrPromise, init)))
6562
return fetch
6663
}
6764

0 commit comments

Comments
 (0)