Skip to content

Commit a3bd3be

Browse files
author
lsimone
committed
functions are accepted as first argument instead of promises
1 parent 5766a59 commit a3bd3be

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ If you are using [Create-React-App](https://github.com/facebookincubator/create-
6868
* `fetch.once(bodyOrFunction, init): fetch` - Alias for mockResponseOnce
6969
* `fetch.mockResponses(...responses): fetch` - Mock multiple fetch calls independently
7070
* Each argument is an array taking `[bodyOrFunction, init]`
71-
* `fetch.mockReject(error): fetch` - Mock all fetch calls, letting them fail directly
72-
* `fetch.mockRejectOnce(error): fetch` - Let the next fetch call fail directly
71+
* `fetch.mockReject(errorOrFunction): fetch` - Mock all fetch calls, letting them fail directly
72+
* `fetch.mockRejectOnce(errorOrFunction): fetch` - Let the next fetch call fail directly
7373

74-
### Promises
74+
### Functions
7575

76-
Instead of passing body, it is possible to pass a function that returns a promise too.
76+
Instead of passing body, it is also possible to pass a function that returns a promise.
7777
The promise should resolve with an object containing body and init props
7878

7979
i.e:

src/index.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,34 @@ function ResponseWrapper (body, init) {
2828
return new ActualResponse(body, init)
2929
}
3030

31-
const isAPromise = obj => obj && obj.then && (typeof obj.then === 'function')
31+
const isFn = unknown => typeof unknown === 'function'
3232

33-
const resolve = (bodyOrFunction, init) => () => isAPromise(bodyOrFunction) ?
34-
bodyOrFunction.then((res) => new ResponseWrapper(res.body, res.init))
35-
: Promise.resolve(new ResponseWrapper(bodyOrFunction, init))
33+
const normalizeResponse = (bodyOrFunction, init) => () => isFn(bodyOrFunction) ?
34+
bodyOrFunction().then(({body, init}) => new ResponseWrapper(body, init)) :
35+
Promise.resolve(new ResponseWrapper(bodyOrFunction, init))
36+
37+
const normalizeError = errorOrFunction => () => isFn(errorOrFunction) ?
38+
errorOrFunction :
39+
() => Promise.reject(errorOrFunction)
3640

3741
const fetch = jest.fn()
3842
fetch.Headers = Headers
3943
fetch.Response = ResponseWrapper
4044
fetch.Request = Request
41-
fetch.mockResponse = (bodyOrFunction, init) => fetch.mockImplementation(resolve(bodyOrFunction, init))
45+
fetch.mockResponse = (bodyOrFunction, init) => fetch.mockImplementation(normalizeResponse(bodyOrFunction, init))
4246

43-
fetch.mockReject = error => {
44-
return fetch.mockImplementation(() => Promise.reject(error))
45-
}
47+
fetch.mockReject = errorOrFunction => fetch.mockImplementation(normalizeError(errorOrFunction))
4648

47-
const mockResponseOnce = (bodyOrFunction, init) => fetch.mockImplementationOnce(resolve(bodyOrFunction, init))
49+
const mockResponseOnce = (bodyOrFunction, init) => fetch.mockImplementationOnce(normalizeResponse(bodyOrFunction, init))
4850

4951
fetch.mockResponseOnce = mockResponseOnce
5052

5153
fetch.once = mockResponseOnce
5254

53-
fetch.mockRejectOnce = errorOrPromise => {
54-
return fetch.mockImplementationOnce(() => isAPromise(errorOrPromise) ? errorOrPromise : Promise.reject(error))
55-
}
55+
fetch.mockRejectOnce = errorOrFunction => fetch.mockImplementationOnce(normalizeError(errorOrFunction))
5656

5757
fetch.mockResponses = (...responses) => {
58-
responses.forEach(([bodyOrFunction, init]) => fetch.mockImplementationOnce(resolve(bodyOrFunction, init)))
58+
responses.forEach(([bodyOrFunction, init]) => fetch.mockImplementationOnce(normalizeResponse(bodyOrFunction, init)))
5959
return fetch
6060
}
6161

0 commit comments

Comments
 (0)