-
Notifications
You must be signed in to change notification settings - Fork 149
/
index.js
116 lines (97 loc) · 3.36 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { applyMiddleware } from 'redux'
import isPlainObject from 'lodash.isplainobject'
const isFunction = (arg) => typeof arg === 'function'
/**
* @deprecated
*
* 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.
*
* 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.
*
* You can test the entire combination of action creators, reducers, and selectors in a single test, for example:
* ```js
* it("should add a todo", () => {
* const store = makeStore(); // a user defined reusable store factory
*
* store.dispatch(addTodo("Use Redux"));
*
* expect(selectTodos(store.getState())).toEqual([{ text: "Use Redux", completed: false }]);
* });
* ```
*
* This avoids common pitfalls of testing each of these in isolation, such as mocked state shape becoming out of sync with the actual application.
*
* If you want to use `configureStore` without this visual deprecation warning, use the `legacy_configureStore` export instead.
*
* `import { legacy_configureStore as configureStore } from 'redux-mock-store';`
*/
export function configureStore(middlewares = []) {
return function mockStore(getState = {}) {
function mockStoreWithoutMiddleware() {
let actions = []
let listeners = []
const self = {
getState() {
return isFunction(getState) ? getState(actions) : getState
},
getActions() {
return actions
},
dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}
if (typeof action.type === 'undefined') {
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant? ' +
'Action: ' +
JSON.stringify(action)
)
}
actions.push(action)
for (let i = 0; i < listeners.length; i++) {
listeners[i]()
}
return action
},
clearActions() {
actions = []
},
subscribe(cb) {
if (isFunction(cb)) {
listeners.push(cb)
}
return () => {
const index = listeners.indexOf(cb)
if (index < 0) {
return
}
listeners.splice(index, 1)
}
},
replaceReducer(nextReducer) {
if (!isFunction(nextReducer)) {
throw new Error('Expected the nextReducer to be a function.')
}
}
}
return self
}
const mockStoreWithMiddleware = applyMiddleware(...middlewares)(
mockStoreWithoutMiddleware
)
return mockStoreWithMiddleware()
}
}
/**
* Create Mock Store returns a function that will create a mock store from a state
* with the supplied set of middleware applied.
*
* @param middlewares The list of middleware to be applied.
*/
export const legacy_configureStore = configureStore
export default configureStore