Skip to content

Commit 8c1b40a

Browse files
authored
Merge pull request #188 from reduxjs/deprecate
Add deprecation notice to README and JSDoc
2 parents b943c3b + 67cb143 commit 8c1b40a

File tree

3 files changed

+95
-31
lines changed

3 files changed

+95
-31
lines changed

README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1-
# redux-mock-store [![Circle CI](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master.svg?style=svg)](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master)
1+
# Deprecation notice
2+
3+
The Redux team does not recommend testing using this library. Instead, see our [docs](https://redux.js.org/usage/writing-tests) for recommended practices, using a real store.
4+
5+
Testing with a mock store leads to potentially confusing behaviour, such as state not updating when actions are dispatched. Additionally, it's a lot less useful to assert on the actions dispatched rather than the observable state changes.
6+
7+
You can test the entire combination of action creators, reducers, and selectors in a single test, for example:
8+
9+
```js
10+
it('should add a todo', () => {
11+
const store = makeStore() // a user defined reusable store factory
12+
13+
store.dispatch(addTodo('Use Redux'))
14+
15+
expect(selectTodos(store.getState())).toEqual([
16+
{ text: 'Use Redux', completed: false }
17+
])
18+
})
19+
```
20+
21+
This avoids common pitfalls of testing each of these in isolation, such as mocked state shape becoming out of sync with the actual application.
222

23+
# redux-mock-store [![Circle CI](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master.svg?style=svg)](https://circleci.com/gh/arnaudbenard/redux-mock-store/tree/master)
324

425
![npm](https://nodei.co/npm/redux-mock-store.png?downloads=true&downloadRank=true&stars=true)
526

@@ -36,7 +57,6 @@ const mockStore = configureStore(middlewares)
3657
const addTodo = () => ({ type: 'ADD_TODO' })
3758

3859
it('should dispatch action', () => {
39-
4060
// Initialize mockstore with empty state
4161
const initialState = {}
4262
const store = mockStore(initialState)
@@ -69,22 +89,21 @@ function success() {
6989
}
7090
}
7191

72-
function fetchData () {
73-
return dispatch => {
92+
function fetchData() {
93+
return (dispatch) => {
7494
return fetch('/users.json') // Some async action with promise
7595
.then(() => dispatch(success()))
76-
};
96+
}
7797
}
7898

7999
it('should execute fetch data', () => {
80100
const store = mockStore({})
81101

82102
// Return the promise
83-
return store.dispatch(fetchData())
84-
.then(() => {
85-
const actions = store.getActions()
86-
expect(actions[0]).toEqual(success())
87-
})
103+
return store.dispatch(fetchData()).then(() => {
104+
const actions = store.getActions()
105+
expect(actions[0]).toEqual(success())
106+
})
88107
})
89108
```
90109

@@ -93,41 +112,49 @@ it('should execute fetch data', () => {
93112
```js
94113
configureStore(middlewares?: Array) => mockStore: Function
95114
```
115+
96116
Configure mock store by applying the middlewares.
97117

98118
```js
99119
mockStore(getState?: Object,Function) => store: Function
100120
```
121+
101122
Returns an instance of the configured mock store. If you want to reset your store after every test, you should call this function.
102123

103124
```js
104125
store.dispatch(action) => action
105126
```
127+
106128
Dispatches an action through the mock store. The action will be stored in an array inside the instance and executed.
107129

108130
```js
109131
store.getState() => state: Object
110132
```
133+
111134
Returns the state of the mock store.
112135

113136
```js
114137
store.getActions() => actions: Array
115138
```
139+
116140
Returns the actions of the mock store.
117141

118142
```js
119143
store.clearActions()
120144
```
145+
121146
Clears the stored actions.
122147

123148
```js
124149
store.subscribe(callback: Function) => unsubscribe: Function
125150
```
151+
126152
Subscribe to the store.
127153

128154
```js
129155
store.replaceReducer(nextReducer: Function)
130156
```
157+
131158
Follows the Redux API.
132159

133160
### Old version (`< 1.x.x`)
@@ -138,9 +165,9 @@ https://github.com/arnaudbenard/redux-mock-store/blob/v0.0.6/README.md
138165

139166
The following versions are exposed by redux-mock-store from the `package.json`:
140167

141-
* `main`: commonJS Version
142-
* `module`/`js:next`: ES Module Version
143-
* `browser` : UMD version
168+
- `main`: commonJS Version
169+
- `module`/`js:next`: ES Module Version
170+
- `browser` : UMD version
144171

145172
## License
146173

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,10 @@
5555
},
5656
"peerDependencies": {
5757
"redux": "*"
58+
},
59+
"prettier": {
60+
"singleQuote": true,
61+
"semi": false,
62+
"trailingComma": "none"
5863
}
5964
}

src/index.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
11
import { applyMiddleware } from 'redux'
22
import isPlainObject from 'lodash.isplainobject'
33

4-
const isFunction = arg => typeof arg === 'function'
5-
6-
export function configureStore (middlewares = []) {
7-
return function mockStore (getState = {}) {
8-
function mockStoreWithoutMiddleware () {
4+
const isFunction = (arg) => typeof arg === 'function'
5+
6+
/**
7+
* @deprecated
8+
*
9+
* The Redux team does not recommend using this package for testing. Instead, check out our {@link https://redux.js.org/recipes/writing-tests testing docs} to learn more about testing Redux code.
10+
*
11+
* Testing with a mock store leads to potentially confusing behaviour, such as state not updating when actions are dispatched. Additionally, it's a lot less useful to assert on the actions dispatched rather than the observable state changes.
12+
*
13+
* You can test the entire combination of action creators, reducers, and selectors in a single test, for example:
14+
* ```js
15+
* it("should add a todo", () => {
16+
* const store = makeStore(); // a user defined reusable store factory
17+
*
18+
* store.dispatch(addTodo("Use Redux"));
19+
*
20+
* expect(selectTodos(store.getState())).toEqual([{ text: "Use Redux", completed: false }]);
21+
* });
22+
* ```
23+
*
24+
* This avoids common pitfalls of testing each of these in isolation, such as mocked state shape becoming out of sync with the actual application.
25+
*
26+
* If you want to use `configureStore` without this visual deprecation warning, use the `legacy_configureStore` export instead.
27+
*
28+
* `import { legacy_configureStore as configureStore } from 'redux-mock-store';`
29+
*/
30+
export function configureStore(middlewares = []) {
31+
return function mockStore(getState = {}) {
32+
function mockStoreWithoutMiddleware() {
933
let actions = []
1034
let listeners = []
1135

1236
const self = {
13-
getState () {
37+
getState() {
1438
return isFunction(getState) ? getState(actions) : getState
1539
},
1640

17-
getActions () {
41+
getActions() {
1842
return actions
1943
},
2044

21-
dispatch (action) {
45+
dispatch(action) {
2246
if (!isPlainObject(action)) {
2347
throw new Error(
2448
'Actions must be plain objects. ' +
25-
'Use custom middleware for async actions.'
49+
'Use custom middleware for async actions.'
2650
)
2751
}
2852

2953
if (typeof action.type === 'undefined') {
3054
throw new Error(
3155
'Actions may not have an undefined "type" property. ' +
32-
'Have you misspelled a constant? ' +
33-
'Action: ' +
34-
JSON.stringify(action)
56+
'Have you misspelled a constant? ' +
57+
'Action: ' +
58+
JSON.stringify(action)
3559
)
3660
}
3761

@@ -44,11 +68,11 @@ export function configureStore (middlewares = []) {
4468
return action
4569
},
4670

47-
clearActions () {
71+
clearActions() {
4872
actions = []
4973
},
5074

51-
subscribe (cb) {
75+
subscribe(cb) {
5276
if (isFunction(cb)) {
5377
listeners.push(cb)
5478
}
@@ -63,7 +87,7 @@ export function configureStore (middlewares = []) {
6387
}
6488
},
6589

66-
replaceReducer (nextReducer) {
90+
replaceReducer(nextReducer) {
6791
if (!isFunction(nextReducer)) {
6892
throw new Error('Expected the nextReducer to be a function.')
6993
}
@@ -73,12 +97,20 @@ export function configureStore (middlewares = []) {
7397
return self
7498
}
7599

76-
const mockStoreWithMiddleware = applyMiddleware(
77-
...middlewares
78-
)(mockStoreWithoutMiddleware)
100+
const mockStoreWithMiddleware = applyMiddleware(...middlewares)(
101+
mockStoreWithoutMiddleware
102+
)
79103

80104
return mockStoreWithMiddleware()
81105
}
82106
}
83107

108+
/**
109+
* Create Mock Store returns a function that will create a mock store from a state
110+
* with the supplied set of middleware applied.
111+
*
112+
* @param middlewares The list of middleware to be applied.
113+
*/
114+
export const legacy_configureStore = configureStore
115+
84116
export default configureStore

0 commit comments

Comments
 (0)