forked from jefflau/jest-fetch-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (58 loc) · 2.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const crossFetch = require('cross-fetch')
global.fetch = crossFetch
global.Response = crossFetch.Response
global.Headers = crossFetch.Headers
global.Request = crossFetch.Request
if (!Promise) {
Promise = require('promise-polyfill')
} else if (!Promise.finally) {
Promise.finally = require('promise-polyfill').finally
}
const ActualResponse = Response
function ResponseWrapper(body, init) {
if (
body &&
typeof body.constructor === 'function' &&
body.constructor.__isFallback
) {
const response = new ActualResponse(null, init)
response.body = body
const actualClone = response.clone
response.clone = () => {
const clone = actualClone.call(response)
const [body1, body2] = body.tee()
response.body = body1
clone.body = body2
return clone
}
return response
}
return new ActualResponse(body, init)
}
const isFn = unknown => typeof unknown === 'function'
const normalizeResponse = (bodyOrFunction, init) => () => isFn(bodyOrFunction) ?
bodyOrFunction().then(({body, init}) => new ResponseWrapper(body, init)) :
Promise.resolve(new ResponseWrapper(bodyOrFunction, init))
const normalizeError = errorOrFunction => isFn(errorOrFunction) ?
errorOrFunction :
() => Promise.reject(errorOrFunction)
const fetch = jest.fn()
fetch.Headers = Headers
fetch.Response = ResponseWrapper
fetch.Request = Request
fetch.mockResponse = (bodyOrFunction, init) => fetch.mockImplementation(normalizeResponse(bodyOrFunction, init))
fetch.mockReject = errorOrFunction => fetch.mockImplementation(normalizeError(errorOrFunction))
const mockResponseOnce = (bodyOrFunction, init) => fetch.mockImplementationOnce(normalizeResponse(bodyOrFunction, init))
fetch.mockResponseOnce = mockResponseOnce
fetch.once = mockResponseOnce
fetch.mockRejectOnce = errorOrFunction => fetch.mockImplementationOnce(normalizeError(errorOrFunction))
fetch.mockResponses = (...responses) => {
responses.forEach(([bodyOrFunction, init]) => fetch.mockImplementationOnce(normalizeResponse(bodyOrFunction, init)))
return fetch
}
fetch.resetMocks = () => {
fetch.mockReset()
}
// Default mock is just a empty string.
fetch.mockResponse('')
module.exports = fetch