Skip to content

Commit 751f3b0

Browse files
committed
Adds dispatcher tests
1 parent ebc33bf commit 751f3b0

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"eslint-plugin-react": "^6.4.1",
4646
"expect": "^1.6.0",
4747
"express": "^4.13.3",
48+
"sinon": "^1.17.6",
4849
"tap-spec": "^4.1.1",
4950
"tape": "^4.6.0"
5051
},

test/dispatcher.spec.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import tape from 'tape'
2+
import { stub, spy } from 'sinon'
3+
4+
import Dispatcher from '../src/dispatcher'
5+
import * as orderedHistory from '../src/orderedHistory'
6+
7+
import { UPDATE_SNAPSHOT } from '../src/constants'
8+
9+
import {
10+
META_TIMESTAMP,
11+
META_SOURCE,
12+
UPDATE_TIMESTAMP,
13+
UPDATE_SOURCE,
14+
} from '../src/constants'
15+
16+
function createAction(payload, type = 'ACTION') {
17+
return ({
18+
type,
19+
payload: payload,
20+
})
21+
}
22+
23+
tape('dispatcher.wrapDispatch', function (t) {
24+
const dispatcher = new Dispatcher(),
25+
dispatch = spy(),
26+
wrappedDispatch = dispatcher.wrapDispatch(dispatch)
27+
28+
spy(dispatcher, 'localUpdate')
29+
30+
const privateAction = createAction(1, '@@privateType'),
31+
publicAction = createAction(10, 'REAL_ACTION')
32+
33+
wrappedDispatch(privateAction)
34+
wrappedDispatch(publicAction)
35+
36+
t.ok(dispatch.calledWith(privateAction), 'dispatch called for gossip action')
37+
t.ok(dispatch.calledWith(privateAction), 'dispatch called for private action')
38+
39+
t.ok(dispatch.calledTwice, 'dispatch called twice')
40+
t.ok(dispatcher.localUpdate.calledOnce, 'localUpdate called once')
41+
42+
dispatcher.localUpdate.restore()
43+
44+
t.end()
45+
})
46+
47+
tape('dispatcher.wrapGetState (initialState)', function (t) {
48+
const dispatcher = new Dispatcher(),
49+
getState = () => orderedHistory.getInitialState(42)
50+
51+
spy(orderedHistory, 'getState')
52+
53+
// should call orderedHistory.getState for transform
54+
t.equal(dispatcher.wrapGetState(getState)(), 42, 'states are equal')
55+
56+
t.ok(orderedHistory.getState.calledOnce, 'orderedHistory.getState called')
57+
58+
orderedHistory.getState.restore();
59+
60+
t.end()
61+
})
62+
63+
tape('dispatcher.wrapInitialState', function (t) {
64+
const dispatcher = new Dispatcher(),
65+
initialState = { 'favs': 'dogs' },
66+
state = dispatcher.wrapInitialState(initialState)
67+
68+
t.ok(orderedHistory.getState(state), 'getState is ok')
69+
t.equal(orderedHistory.getState(state).favs, 'dogs', 'favs is dogs')
70+
71+
t.end()
72+
})
73+
74+
tape('dispatcher.wrapReducer', function (t) {
75+
const dispatcher = new Dispatcher(),
76+
rootReducer = spy((state = [], action) => [ ...state, action.payload]),
77+
reducer = dispatcher.wrapReducer(rootReducer)
78+
79+
let state = dispatcher.wrapInitialState(['hey'])
80+
81+
state = reducer(state, createAction('new'))
82+
state = reducer(state, createAction('yeah'))
83+
84+
t.ok(rootReducer.calledTwice, 'called rootReducer twice')
85+
86+
t.equal(orderedHistory.getState(state).length, 3, 'should have three entries')
87+
t.equal(orderedHistory.getState(state)[1], 'new', '"new" should be the second entry')
88+
89+
t.end()
90+
})
91+
92+
// dispatcher.applyUpdate - scuttlebutt super, eh
93+
94+
// dispatcher.history - scuttlebutt, history(sources) reduces getState of
95+
// gossips and seen actions acoording to sources
96+
97+
// dispatcher.localUpdate - scuttlebutt, calls super.localUpdate (which calls
98+
// other internals)
99+
100+
// options.customDispatch (ensure signature)
101+
// options.isGossipType (copy wrapDispatch)
102+
// options.verifyAsync (ensure behaviour)

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require('babel-register')
22

33
require('./orderedHistory.spec.js')
4+
require('./dispatcher.spec.js')

test/orderedHistory.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import tape from 'tape'
2-
import Dispatcher, { REWIND_ACTION } from '../src/dispatcher'
32
import { reducer, sort, getState } from '../src/orderedHistory'
43

54
import {

0 commit comments

Comments
 (0)