Skip to content

Commit b0dea64

Browse files
authored
Add type to stub action creator (#157)
1 parent 46f9c79 commit b0dea64

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ handleActions({
359359

360360
// ** Stubbing a failed request: **
361361

362-
export const fetchUser = createStubRequest('FETCH_USER', (id) => {
362+
export const fetchUser = createStubRequest('FETCH_USER', (id) => {
363363
throw new Error('My mock error.')
364364
})
365365

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@launchpadlab/lp-redux-api",
3-
"version": "6.3.0",
3+
"version": "6.3.1",
44
"description": "redux middleware and api library",
55
"main": "lib/index.js",
66
"scripts": {

src/create-stub-request.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { isObject, isFunction, identity } from 'lodash'
44

55
/**
66
* A function that creates action creators for making stubbed API requests.
7-
*
7+
*
88
* Unlike {@link createRequest}, these action creators do not make real API calls but rather
99
* resolve immediately with the provided data.
10-
*
10+
*
1111
* If an exception is thrown from the data creator function, the "request" will reject with that exception instead of resolving.
1212
*
1313
* @name createStubRequest
@@ -19,29 +19,29 @@ import { isObject, isFunction, identity } from 'lodash'
1919
* @example
2020
*
2121
* // ** Stubbing a successful request: **
22-
*
22+
*
2323
* export const fetchUser = createStubRequest('FETCH_USER', (id) => ({ id }))
2424
*
2525
* fetchUsers(5)
2626
* // -> won't make any api request, but will resolve with data { id: 5 }
2727
*
2828
* // Just like in redux-actions, this action can be referenced in a reducer by name:
29-
*
29+
*
3030
* handleActions({
3131
* [apiActions.fetchUser]: (state, action) => ...
3232
* })
33-
*
33+
*
3434
* // ** Stubbing a failed request: **
35-
*
36-
* export const fetchUser = createStubRequest('FETCH_USER', (id) => {
35+
*
36+
* export const fetchUser = createStubRequest('FETCH_USER', (id) => {
3737
* throw new Error('My mock error.')
3838
* })
3939
*
4040
* fetchUsers(5)
4141
* // -> won't make any api request, but will reject with the given error.
4242
*
4343
* // ** Simulating a response delay: **
44-
*
44+
*
4545
* export const fetchUser = createStubRequest('FETCH_USER', (id) => {
4646
* return {
4747
* id
@@ -74,6 +74,7 @@ function createStubRequest (type, definition=identity, { delay }={}) {
7474
}
7575
}
7676
actionCreator.toString = () => LP_API_ACTION_NAMESPACE + type
77+
actionCreator.type = LP_API_ACTION_NAMESPACE + type
7778
return actionCreator
7879
}
7980

test/create-stub-request.test.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import { createStubRequest, LP_API } from '../src'
2+
import { LP_API_ACTION_NAMESPACE } from '../src/actions'
23
import { REQUEST_TYPE } from './fixtures'
34

45
describe('createStubRequest', () => {
56
test('requires a type argument', () => {
67
expect(createStubRequest).toThrow()
78
})
8-
9+
910
test('accepts object data', () => {
1011
const stubData = { foo: 'bar' }
1112
const actionCreator = createStubRequest(REQUEST_TYPE, stubData)
1213
const action = actionCreator()
1314
expect(action[LP_API]).toEqual({ type: REQUEST_TYPE, isStub: true, stubData })
1415
})
15-
16+
1617
test('accepts function data creator', () => {
1718
const actionCreator = createStubRequest(REQUEST_TYPE, (arg) => ({
1819
foo: arg
1920
}))
2021
const action = actionCreator('bar')
2122
expect(action[LP_API]).toEqual({ type: REQUEST_TYPE, isStub: true, stubData: { foo: 'bar' } })
2223
})
23-
24+
2425
test('accepts delay option', () => {
2526
const stubData = { foo: 'bar' }
2627
const actionCreator = createStubRequest(REQUEST_TYPE, stubData, { delay: 500 })
2728
const action = actionCreator()
2829
expect(action[LP_API]).toEqual({ type: REQUEST_TYPE, isStub:true, stubData, delay: 500 })
2930
})
30-
31+
3132
test('function sets data and error flag from thrown exception', () => {
3233
const myException = new Error('oops')
3334
const actionCreator = createStubRequest(REQUEST_TYPE, () => {
@@ -36,14 +37,24 @@ describe('createStubRequest', () => {
3637
const action = actionCreator('bar')
3738
expect(action[LP_API]).toEqual({ type: REQUEST_TYPE, isStub: true, isStubError: true, stubData: myException })
3839
})
39-
40+
4041
test('defaults to identity for data creator', () => {
4142
const actionCreator = createStubRequest(REQUEST_TYPE)
4243
const action = actionCreator('bar')
4344
expect(action[LP_API]).toEqual({ type: REQUEST_TYPE, isStub: true, stubData: 'bar' })
4445
})
45-
46+
4647
test('rejects other types of data definitions', () => {
4748
expect(() => createStubRequest(REQUEST_TYPE, 'wtf')).toThrow()
4849
})
50+
51+
test('has the namespaced action type as its string representation', () => {
52+
const actionCreator = createStubRequest(REQUEST_TYPE)
53+
expect(actionCreator.toString()).toEqual(LP_API_ACTION_NAMESPACE+REQUEST_TYPE)
54+
})
55+
56+
test('has the namespaced action type set to the special `type` property', () => {
57+
const actionCreator = createStubRequest(REQUEST_TYPE)
58+
expect(actionCreator.type).toEqual(LP_API_ACTION_NAMESPACE+REQUEST_TYPE)
59+
})
4960
})

0 commit comments

Comments
 (0)