Skip to content

Commit 5766a59

Browse files
author
lsimone
committed
small refactor
1 parent c7abcdb commit 5766a59

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ If you are using [Create-React-App](https://github.com/facebookincubator/create-
6363

6464
### Mock Responses
6565

66-
* `fetch.mockResponse(body, init): fetch` - Mock all fetch calls
67-
* `fetch.mockResponseOnce(body, init): fetch` - Mock each fetch call independently
68-
* `fetch.once(body, init): fetch` - Alias for mockResponseOnce
66+
* `fetch.mockResponse(bodyOrFunction, init): fetch` - Mock all fetch calls
67+
* `fetch.mockResponseOnce(bodyOrFunction, init): fetch` - Mock each fetch call independently
68+
* `fetch.once(bodyOrFunction, init): fetch` - Alias for mockResponseOnce
6969
* `fetch.mockResponses(...responses): fetch` - Mock multiple fetch calls independently
70-
* Each argument is an array taking `[body, init]`
70+
* Each argument is an array taking `[bodyOrFunction, init]`
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

7474
### Promises
7575

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
76+
Instead of passing body, it is possible to pass a function that returns a promise too.
77+
The promise should resolve with an object containing body and init props
7878

7979
i.e:
8080

src/index.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,21 @@ function ResponseWrapper (body, init) {
3030

3131
const isAPromise = obj => obj && obj.then && (typeof obj.then === 'function')
3232

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))
3936

4037
const fetch = jest.fn()
4138
fetch.Headers = Headers
4239
fetch.Response = ResponseWrapper
4340
fetch.Request = Request
44-
fetch.mockResponse = (bodyOrPromise, init) => fetch.mockImplementation(resolve(bodyOrPromise, init))
41+
fetch.mockResponse = (bodyOrFunction, init) => fetch.mockImplementation(resolve(bodyOrFunction, init))
4542

4643
fetch.mockReject = error => {
4744
return fetch.mockImplementation(() => Promise.reject(error))
4845
}
4946

50-
const mockResponseOnce = (bodyOrPromise, init) => fetch.mockImplementationOnce(resolve(bodyOrPromise, init))
47+
const mockResponseOnce = (bodyOrFunction, init) => fetch.mockImplementationOnce(resolve(bodyOrFunction, init))
5148

5249
fetch.mockResponseOnce = mockResponseOnce
5350

@@ -58,7 +55,7 @@ fetch.mockRejectOnce = errorOrPromise => {
5855
}
5956

6057
fetch.mockResponses = (...responses) => {
61-
responses.forEach(([bodyOrPromise, init]) => fetch.mockImplementationOnce(resolve(bodyOrPromise, init)))
58+
responses.forEach(([bodyOrFunction, init]) => fetch.mockImplementationOnce(resolve(bodyOrFunction, init)))
6259
return fetch
6360
}
6461

0 commit comments

Comments
 (0)