Skip to content

Commit 8828289

Browse files
author
lsimone
committed
README + normalizeError fixed, tests added
1 parent 7e8f1c9 commit 8828289

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fetch.mockResponse(() => callMyApi().then(res => ({body: res}))
8787
The same goes for rejects:
8888

8989
```
90-
fetch.mockReject(() => doMyAsyncJob().then(res => res.errorToRaise))
90+
fetch.mockReject(() => doMyAsyncJob().then(res => Promise.reject(res.errorToRaise)))
9191
```
9292

9393
### Mock utilities

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const normalizeResponse = (bodyOrFunction, init) => () => isFn(bodyOrFunction) ?
4242
bodyOrFunction().then(({body, init}) => new ResponseWrapper(body, init)) :
4343
Promise.resolve(new ResponseWrapper(bodyOrFunction, init))
4444

45-
const normalizeError = errorOrFunction => () => isFn(errorOrFunction) ?
46-
errorOrFunction :
47-
() => Promise.reject(errorOrFunction)
45+
const normalizeError = errorOrFunction => isFn(errorOrFunction) ?
46+
errorOrFunction :
47+
() => Promise.reject(errorOrFunction)
4848

4949
const fetch = jest.fn()
5050
fetch.Headers = Headers

tests/test.js

+26
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,30 @@ describe('request', () => {
181181
done()
182182
})
183183
})
184+
185+
it('resolves with function', done => {
186+
fetch.mockResponseOnce(() => Promise.resolve({body: 'ok'}))
187+
188+
request()
189+
.then(response => {
190+
expect(response).toEqual('ok')
191+
done()
192+
})
193+
.catch(done.fail)
194+
})
195+
196+
it('rejects with function', done => {
197+
const errorData = {
198+
error:
199+
'Uh oh, something has gone wrong. Please tweet us @randomapi about the issue. Thank you.'
200+
}
201+
fetch.mockRejectOnce(() => Promise.reject(JSON.stringify(errorData)))
202+
203+
request()
204+
.then(done.fail)
205+
.catch(error => {
206+
expect(error.message).toBe(errorData.error)
207+
done()
208+
})
209+
})
184210
})

0 commit comments

Comments
 (0)