-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helper.jsx
170 lines (157 loc) · 4.73 KB
/
test_helper.jsx
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React, { Component } from 'react'
import createSagaMiddleware, { runSaga } from 'redux-saga'
import * as effects from 'redux-saga/effects'
import * as utils from 'redux-saga/utils'
import teaspoon from 'teaspoon'
import { Provider, connect } from 'react-redux'
import { createStore, combineReducers, applyMiddleware } from 'redux'
import reducer from '../src/reducer'
const fakeWeekReducer = (state = 1) => state
export const tests = Object.keys(utils.asEffect).reduce((effects, name) => ({
...effects,
[name]: effect => utils.is.notUndef(utils.asEffect[name](effect))
}), {
isPromise: effect => utils.is.promise(effect),
isHelper: effect => utils.is.helper(effect),
isIterator: effect => utils.is.iterator(effect),
takeEvery: effect => utils.is.helper(effect),
takeLatest: effect => utils.is.helper(effect),
throttle: effect => utils.is.helper(effect),
takem: effect => utils.is.notUndef(utils.asEffect.take(effect)) && effect.TAKE.maybe,
apply: () => false,
spawn: effect => utils.is.notUndef(utils.asEffect.fork(effect)) && effect.FORK.detached,
exception: effect => effect instanceof Error,
endSaga: effect => Object.hasOwnProperty.call(effect, 'effect') && effect.effect === undefined,
})
function sagaStore(state, reducers = { routing: reducer, week: fakeWeekReducer }, middleware = []) {
const log = []
const logger = store => next => action => { // eslint-disable-line
log.push(action)
return next(action)
}
const monitor = {
}
const sagaMiddleware = createSagaMiddleware({ sagaMonitor: monitor })
const store = createStore(combineReducers(reducers),
state, applyMiddleware(sagaMiddleware, ...middleware, logger))
return {
log,
store,
sagaMiddleware,
monitor,
}
}
export const effectNames =
Object.keys(utils.asEffect).reduce((effects, name) => ({
...effects,
[name]: name,
}), {
isPromise: 'promise',
isHelper: 'helper',
isIterator: 'iterator',
takeEvery: 'takeEvery',
takeLatest: 'takeLatest',
throttle: 'throttle',
takem: 'takem',
apply: 'apply',
spawn: 'spawn',
exception: 'exception',
endSaga: 'endSaga',
})
export const effectName = effect =>
effectNames[Object.keys(tests).map(test => tests[test](effect) && test).filter(t => t)[0]]
export const toEffectAction = effect => ({
type: effectName(effect),
effect
})
export const START = { type: '###@@@start' }
export function testSaga(state = undefined,
reducers = { routing: reducer, week: fakeWeekReducer }) {
const { sagaMiddleware, store, monitor } = sagaStore(state, {
...reducers,
routing: reducer
})
return (sagas, test) => {
const cbs = []
sagaMiddleware.run(function*() {
yield effects.take('###@@@start')
yield sagas.map(args => effects.fork(function *f() {
try {
yield effects.call(...args)
} catch (e) {
cbs.forEach(cb => cb(e))
}
}))
})
const vars = {
log: []
}
monitor.effectTriggered = (info) => {
const effect = info.effect || info
if (effect && !effect.length) {
vars.log.push(toEffectAction(effect))
cbs.forEach((cb) => {
cb(toEffectAction(effect))
})
}
}
runSaga(test(), {
subscribe: (callback) => {
vars.log.forEach(effect => callback(effect))
cbs.push(callback)
return () => {
cbs.splice(cbs.indexOf(callback), 1)
}
},
dispatch: (output) => {
store.dispatch(output)
},
getState: () => vars.log
})
}
}
function renderComponent(ComponentClass, props = {}, state = {}, returnStore = false,
sagaStore = false) {
let store
let log
const sagaMiddleware = createSagaMiddleware()
if (!sagaStore) {
log = []
const logger = store => next => action => { // eslint-disable-line
log.push(action)
return next(action)
}
store = createStore(combineReducers({ routing: reducer, week: fakeWeekReducer }),
state, applyMiddleware(logger, sagaMiddleware))
}
class Tester extends Component {
constructor(props) {
super(props)
this.state = props
}
componentWillReceiveProps(props) {
if (props !== this.props) {
this.setState(props)
}
}
render() {
return (
<Provider store={sagaStore ? sagaStore.store : store}>
<ComponentClass {...this.state} />
</Provider>
)
}
}
const componentInstance = teaspoon(
<Tester {...props} />
).render()
const ret = componentInstance
if (returnStore) {
if (sagaStore) {
return [ret, sagaStore.store, sagaStore.log, sagaStore.sagaMiddleware]
}
return [ret, store, log, sagaMiddleware]
}
return ret
}
export { renderComponent, connect, sagaStore } // eslint-disable-line