|
| 1 | +const expect = require('expect'); |
| 2 | +const { updatePath, UPDATE_PATH, routeReducer, syncReduxAndRouter } = require('../src/index'); |
| 3 | +const { createStore, combineReducers } = require('redux'); |
| 4 | +const { createMemoryHistory: createHistory } = require('history'); |
| 5 | + |
| 6 | +function createSyncedHistoryAndStore() { |
| 7 | + const store = createStore(combineReducers({ |
| 8 | + routing: routeReducer |
| 9 | + })); |
| 10 | + const history = createHistory(); |
| 11 | + syncReduxAndRouter(history, store); |
| 12 | + return { history, store }; |
| 13 | +} |
| 14 | + |
| 15 | +describe('updatePath', () => { |
| 16 | + it('creates actions', () => { |
| 17 | + expect(updatePath('/foo')).toEqual({ |
| 18 | + type: UPDATE_PATH, |
| 19 | + path: '/foo', |
| 20 | + avoidRouterUpdate: false |
| 21 | + }); |
| 22 | + |
| 23 | + expect(updatePath('/foo', { avoidRouterUpdate: true })).toEqual({ |
| 24 | + type: UPDATE_PATH, |
| 25 | + path: '/foo', |
| 26 | + avoidRouterUpdate: true |
| 27 | + }); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe('routeReducer', () => { |
| 32 | + const state = { |
| 33 | + path: '/foo', |
| 34 | + changeId: 1 |
| 35 | + }; |
| 36 | + |
| 37 | + it('updates the path', () => { |
| 38 | + expect(routeReducer(state, { |
| 39 | + type: UPDATE_PATH, |
| 40 | + path: '/bar' |
| 41 | + })).toEqual({ |
| 42 | + path: '/bar', |
| 43 | + changeId: 2 |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('respects `avoidRouterUpdate` flag', () => { |
| 48 | + expect(routeReducer(state, { |
| 49 | + type: UPDATE_PATH, |
| 50 | + path: '/bar', |
| 51 | + avoidRouterUpdate: true |
| 52 | + })).toEqual({ |
| 53 | + path: '/bar', |
| 54 | + changeId: 1 |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('syncReduxAndRouter', () => { |
| 60 | + it('syncs router -> redux', () => { |
| 61 | + const { history, store } = createSyncedHistoryAndStore(); |
| 62 | + expect(store.getState().routing.path).toEqual('/'); |
| 63 | + |
| 64 | + history.pushState(null, '/foo'); |
| 65 | + expect(store.getState().routing.path).toEqual('/foo'); |
| 66 | + |
| 67 | + history.pushState(null, '/bar'); |
| 68 | + expect(store.getState().routing.path).toEqual('/bar'); |
| 69 | + |
| 70 | + history.pushState(null, '/bar?query=1'); |
| 71 | + expect(store.getState().routing.path).toEqual('/bar?query=1'); |
| 72 | + |
| 73 | + history.pushState(null, '/bar?query=1#hash=2'); |
| 74 | + expect(store.getState().routing.path).toEqual('/bar?query=1#hash=2'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('syncs redux -> router', () => { |
| 78 | + const { history, store } = createSyncedHistoryAndStore(); |
| 79 | + expect(store.getState().routing).toEqual({ |
| 80 | + path: '/', |
| 81 | + changeId: 1 |
| 82 | + }); |
| 83 | + |
| 84 | + store.dispatch(updatePath('/foo')); |
| 85 | + expect(store.getState().routing).toEqual({ |
| 86 | + path: '/foo', |
| 87 | + changeId: 2 |
| 88 | + }); |
| 89 | + |
| 90 | + store.dispatch(updatePath('/bar')); |
| 91 | + expect(store.getState().routing).toEqual({ |
| 92 | + path: '/bar', |
| 93 | + changeId: 3 |
| 94 | + }); |
| 95 | + |
| 96 | + store.dispatch(updatePath('/bar?query=1')); |
| 97 | + expect(store.getState().routing).toEqual({ |
| 98 | + path: '/bar?query=1', |
| 99 | + changeId: 4 |
| 100 | + }); |
| 101 | + |
| 102 | + store.dispatch(updatePath('/bar?query=1#hash=2')); |
| 103 | + expect(store.getState().routing).toEqual({ |
| 104 | + path: '/bar?query=1#hash=2', |
| 105 | + changeId: 5 |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('updates the router even if path is the same', () => { |
| 110 | + const { history, store } = createSyncedHistoryAndStore(); |
| 111 | + expect(store.getState().routing).toEqual({ |
| 112 | + path: '/', |
| 113 | + changeId: 1 |
| 114 | + }); |
| 115 | + |
| 116 | + store.dispatch(updatePath('/foo')); |
| 117 | + expect(store.getState().routing).toEqual({ |
| 118 | + path: '/foo', |
| 119 | + changeId: 2 |
| 120 | + }); |
| 121 | + |
| 122 | + store.dispatch(updatePath('/foo')); |
| 123 | + expect(store.getState().routing).toEqual({ |
| 124 | + path: '/foo', |
| 125 | + changeId: 3 |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('does not update the router for other state changes', () => { |
| 130 | + const { history, store } = createSyncedHistoryAndStore(); |
| 131 | + store.dispatch({ |
| 132 | + type: 'RANDOM_ACTION', |
| 133 | + value: 5 |
| 134 | + }); |
| 135 | + |
| 136 | + expect(store.getState().routing).toEqual({ |
| 137 | + path: '/', |
| 138 | + changeId: 1 |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('only updates the router once when dispatching from `listenBefore`', () => { |
| 143 | + const { history, store } = createSyncedHistoryAndStore(); |
| 144 | + expect(store.getState().routing).toEqual({ |
| 145 | + path: '/', |
| 146 | + changeId: 1 |
| 147 | + }); |
| 148 | + |
| 149 | + history.listenBefore(location => { |
| 150 | + expect(location.pathname).toEqual('/foo'); |
| 151 | + store.dispatch({ |
| 152 | + type: 'RANDOM_ACTION', |
| 153 | + value: 5 |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + store.dispatch(updatePath('/foo')); |
| 158 | + expect(store.getState().routing).toEqual({ |
| 159 | + path: '/foo', |
| 160 | + changeId: 2 |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + it('allows updating the route from within `listenBefore`', () => { |
| 165 | + const { history, store } = createSyncedHistoryAndStore(); |
| 166 | + expect(store.getState().routing).toEqual({ |
| 167 | + path: '/', |
| 168 | + changeId: 1 |
| 169 | + }); |
| 170 | + |
| 171 | + history.listenBefore(location => { |
| 172 | + if(location.pathname === '/foo') { |
| 173 | + expect(store.getState().routing).toEqual({ |
| 174 | + path: '/foo', |
| 175 | + changeId: 2 |
| 176 | + }); |
| 177 | + store.dispatch(updatePath('/bar')); |
| 178 | + } |
| 179 | + }); |
| 180 | + |
| 181 | + store.dispatch(updatePath('/foo')); |
| 182 | + expect(store.getState().routing).toEqual({ |
| 183 | + path: '/bar', |
| 184 | + changeId: 3 |
| 185 | + }); |
| 186 | + }) |
| 187 | +}); |
0 commit comments