|
| 1 | +import tape from 'tape' |
| 2 | +import { spy } from 'sinon' |
| 3 | + |
| 4 | +import Dispatcher from '../src/dispatcher' |
| 5 | +import * as orderedHistory from '../src/orderedHistory' |
| 6 | + |
| 7 | +function createAction(payload, type = 'ACTION') { |
| 8 | + return ({ |
| 9 | + type, |
| 10 | + payload: payload, |
| 11 | + }) |
| 12 | +} |
| 13 | + |
| 14 | +tape('dispatcher.wrapDispatch', function (t) { |
| 15 | + const dispatcher = new Dispatcher(), |
| 16 | + dispatch = spy(), |
| 17 | + wrappedDispatch = dispatcher.wrapDispatch(dispatch) |
| 18 | + |
| 19 | + spy(dispatcher, 'localUpdate') |
| 20 | + |
| 21 | + const privateAction = createAction(1, '@@privateType'), |
| 22 | + publicAction = createAction(10, 'REAL_ACTION') |
| 23 | + |
| 24 | + wrappedDispatch(privateAction) |
| 25 | + wrappedDispatch(publicAction) |
| 26 | + |
| 27 | + t.ok(dispatch.calledWith(privateAction), 'dispatch called for gossip action') |
| 28 | + t.ok(dispatch.calledWith(privateAction), 'dispatch called for private action') |
| 29 | + |
| 30 | + t.ok(dispatch.calledTwice, 'dispatch called twice') |
| 31 | + t.ok(dispatcher.localUpdate.calledOnce, 'localUpdate called once') |
| 32 | + |
| 33 | + dispatcher.localUpdate.restore() |
| 34 | + |
| 35 | + t.end() |
| 36 | +}) |
| 37 | + |
| 38 | +tape('dispatcher.wrapGetState (initialState)', function (t) { |
| 39 | + const dispatcher = new Dispatcher(), |
| 40 | + getState = () => orderedHistory.getInitialState(42) |
| 41 | + |
| 42 | + spy(orderedHistory, 'getState') |
| 43 | + |
| 44 | + // should call orderedHistory.getState for transform |
| 45 | + t.equal(dispatcher.wrapGetState(getState)(), 42, 'states are equal') |
| 46 | + |
| 47 | + t.ok(orderedHistory.getState.calledOnce, 'orderedHistory.getState called') |
| 48 | + |
| 49 | + orderedHistory.getState.restore(); |
| 50 | + |
| 51 | + t.end() |
| 52 | +}) |
| 53 | + |
| 54 | +tape('dispatcher.wrapInitialState', function (t) { |
| 55 | + const dispatcher = new Dispatcher(), |
| 56 | + initialState = { 'favs': 'dogs' }, |
| 57 | + state = dispatcher.wrapInitialState(initialState) |
| 58 | + |
| 59 | + t.ok(orderedHistory.getState(state), 'getState is ok') |
| 60 | + t.equal(orderedHistory.getState(state).favs, 'dogs', 'favs is dogs') |
| 61 | + |
| 62 | + t.end() |
| 63 | +}) |
| 64 | + |
| 65 | +tape('dispatcher.wrapReducer', function (t) { |
| 66 | + const dispatcher = new Dispatcher(), |
| 67 | + rootReducer = spy((state = [], action) => [ ...state, action.payload]), |
| 68 | + reducer = dispatcher.wrapReducer(rootReducer) |
| 69 | + |
| 70 | + let state = dispatcher.wrapInitialState(['hey']) |
| 71 | + |
| 72 | + state = reducer(state, createAction('new')) |
| 73 | + state = reducer(state, createAction('yeah')) |
| 74 | + |
| 75 | + t.ok(rootReducer.calledTwice, 'called rootReducer twice') |
| 76 | + |
| 77 | + t.equal(orderedHistory.getState(state).length, 3, 'should have three entries') |
| 78 | + t.equal(orderedHistory.getState(state)[1], 'new', '"new" should be the second entry') |
| 79 | + |
| 80 | + t.end() |
| 81 | +}) |
| 82 | + |
| 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 | +}) |
0 commit comments