-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathindex.js
82 lines (65 loc) · 1.9 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
import { applyMiddleware } from 'redux'
import isPlainObject from 'lodash.isplainobject'
const isFunction = arg => typeof arg === 'function'
export default 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](action)
}
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()
}
}