Skip to content

Commit 9002bf6

Browse files
committed
Adds dispatch(verifyAsync) test
1 parent b68e4dd commit 9002bf6

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

test/dispatcher.spec.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,46 @@ tape('dispatcher.wrapReducer', function (t) {
8080
t.end()
8181
})
8282

83+
tape('dispatcher({ verifyAsync })', function (t) {
84+
const
85+
invalid = ['new', 'yeah'], valid = ['what', 'up'],
86+
verifyAsync = (callback, action, getHistory) => {
87+
t.ok(Array.isArray(getHistory()), 'getHistory() returns an array')
88+
89+
setTimeout(() => {
90+
// payloads containing 'e' are invalid
91+
if (action && action.payload && action.payload.indexOf('e') !== -1) {
92+
t.ok(invalid.includes(action.payload), 'invalid action is invalid')
93+
callback(false)
94+
} else {
95+
t.ok(action.payload && valid.includes(action.payload), 'valid action is valid')
96+
callback(true)
97+
}
98+
}, 5)
99+
},
100+
dispatcher = new Dispatcher({ verifyAsync }),
101+
dispatch = spy(),
102+
wrappedDispatch = dispatcher.wrapDispatch(dispatch),
103+
getState = spy(() => []) // becomes getHistory, returns array
104+
105+
dispatcher.wrapGetState(getState)
106+
107+
wrappedDispatch(createAction(invalid[0]))
108+
wrappedDispatch(createAction(invalid[1]))
109+
wrappedDispatch(createAction(valid[0]))
110+
wrappedDispatch(createAction(valid[1]))
111+
112+
setTimeout(() => {
113+
t.ok(dispatch.calledTwice, 'called dispatch twice')
114+
// first call
115+
t.equal(dispatch.getCall(0).args[0].payload, valid[0], 'called dispatch with valid action 1')
116+
// second call
117+
t.equal(dispatch.getCall(1).args[0].payload, valid[1], 'called dispatch with valid action 2')
118+
t.equal(getState.callCount, 4, 'getState was called for each getHistory')
119+
t.end()
120+
}, 20)
121+
})
122+
83123
// dispatcher.applyUpdate - scuttlebutt super, eh
84124

85125
// dispatcher.history - scuttlebutt, history(sources) reduces getState of
@@ -89,5 +129,4 @@ tape('dispatcher.wrapReducer', function (t) {
89129
// other internals)
90130

91131
// options.customDispatch (ensure signature)
92-
// options.isGossipType (copy wrapDispatch)
93-
// options.verifyAsync (ensure behaviour)
132+
// options.isGossipType (copy wrapDispatch, can't be true for redux init)

0 commit comments

Comments
 (0)