Skip to content

Commit 8bf6653

Browse files
committed
Add error argument to createStubRequest
1 parent 73eee23 commit 8bf6653

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

docs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ A function that creates action creators for making stubbed API requests.
350350
Unlike [createRequest][21], these action creators do not make real API calls but rather
351351
resolve immediately with the provided data.
352352

353+
If an `error` key is provided in the stub data object, the "request" will reject with the value of that key instead of resolving.
354+
353355
### Parameters
354356

355357
- `type` **[String][33]** A unique key that will be used to identify the request internally in redux
@@ -358,6 +360,8 @@ resolve immediately with the provided data.
358360
### Examples
359361

360362
```javascript
363+
// ** Stubbing a successful request: **
364+
361365
export const fetchUser = createStubRequest('FETCH_USER', (id) => ({ id }))
362366

363367
fetchUsers(5)
@@ -369,6 +373,13 @@ handleActions({
369373
[apiActions.fetchUser]: (state, action) => ...
370374
})
371375

376+
// ** Stubbing a failed request: **
377+
378+
export const fetchUser = createStubRequest('FETCH_USER', (id) => ({ error: new Error('My mock error.') }))
379+
380+
fetchUsers(5)
381+
// -> won't make any api request, but will reject with the given error.
382+
372383
*
373384
```
374385

src/create-stub-request.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import { isObject, isFunction, identity } from 'lodash'
77
*
88
* Unlike {@link createRequest}, these action creators do not make real API calls but rather
99
* resolve immediately with the provided data.
10+
*
11+
* If an `error` key is provided in the stub data object, the "request" will reject with the value of that key instead of resolving.
1012
*
1113
* @name createStubRequest
1214
* @param {String} type - A unique key that will be used to identify the request internally in redux
1315
* @param {Object|Function} dataDefinition - Data that the request will resolve with, or a function that returns data to resolve with.
1416
* @returns {Function} An action creator that passes its arguments to `dataDefinition` and makes the resulting stubbed API request.
1517
* @example
1618
*
17-
*
19+
* // ** Stubbing a successful request: **
20+
*
1821
* export const fetchUser = createStubRequest('FETCH_USER', (id) => ({ id }))
1922
*
2023
* fetchUsers(5)
@@ -25,6 +28,13 @@ import { isObject, isFunction, identity } from 'lodash'
2528
* handleActions({
2629
* [apiActions.fetchUser]: (state, action) => ...
2730
* })
31+
*
32+
* // ** Stubbing a failed request: **
33+
*
34+
* export const fetchUser = createStubRequest('FETCH_USER', (id) => ({ error: new Error('My mock error.') }))
35+
*
36+
* fetchUsers(5)
37+
* // -> won't make any api request, but will reject with the given error.
2838
*
2939
**/
3040

src/middleware/middleware.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ import { isFunction } from 'lodash'
3232
* @type Function
3333
*/
3434

35-
// custom HTTP method for stub requests- makes no call, but resolves with provided data.
35+
// custom HTTP method for stub requests- makes no call, but resolves/rejects with provided data
3636
function createStubRequest (data) {
3737
return function request () {
38-
return Promise.resolve(data)
38+
return new Promise((resolve, reject) => {
39+
return data.error ? reject(data.error) : resolve(data)
40+
})
3941
}
4042
}
4143

test/middleware.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ test('middleware resolves stubbed requests with provided data', () => {
149149
})
150150
})
151151

152+
test('middleware rejects stubbed requests with error key', () => {
153+
expect.assertions(1)
154+
const ERROR = new Error('mock error')
155+
const store = mockStore({})
156+
const stubAction = {
157+
[LP_API]: {
158+
isStub: true,
159+
stubData: { error: ERROR }
160+
}
161+
}
162+
return store.dispatch(stubAction).catch((res) => {
163+
expect(res).toEqual(ERROR)
164+
})
165+
})
166+
152167
test('middleware dispatches default success action with correct data argument', () => {
153168
const store = mockStore({})
154169
return store.dispatch(actionWithURL(successUrl)).then(() => {

0 commit comments

Comments
 (0)