|
| 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) |
0 commit comments