|
1 | 1 | import expect from 'expect'
|
2 | 2 |
|
3 | 3 | import { createStore, combineReducers } from 'redux'
|
4 |
| -// import { ActionCreators, instrument } from 'redux-devtools' |
5 |
| -// import { useBasename, useQueries } from 'history' |
| 4 | +import { ActionCreators, instrument } from 'redux-devtools' |
6 | 5 |
|
7 | 6 | import syncHistoryWithStore from '../src/sync'
|
8 | 7 | import { routerReducer } from '../src/reducer'
|
9 |
| -// import routerMiddleware from '../src/middleware' |
10 | 8 |
|
11 | 9 | expect.extend({
|
12 | 10 | toContainLocation({
|
@@ -42,7 +40,7 @@ function createSyncedHistoryAndStore(testHistory) {
|
42 | 40 |
|
43 | 41 | const defaultReset = () => {}
|
44 | 42 |
|
45 |
| -module.exports = function createTests(testHistory, name, reset = defaultReset) { |
| 43 | +export default function createTests(testHistory, name, reset = defaultReset) { |
46 | 44 | describe(name, () => {
|
47 | 45 |
|
48 | 46 | beforeEach(reset)
|
@@ -131,7 +129,91 @@ module.exports = function createTests(testHistory, name, reset = defaultReset) {
|
131 | 129 | action: 'REPLACE'
|
132 | 130 | })
|
133 | 131 | })
|
| 132 | + |
| 133 | + it('provices an unsubscribe method to stop listening to history and store', () => { |
| 134 | + history.push('/foo') |
| 135 | + expect(store).toContainLocation({ |
| 136 | + pathname: '/foo' |
| 137 | + }) |
| 138 | + |
| 139 | + history.unsubscribe() |
| 140 | + |
| 141 | + history.push('/bar') |
| 142 | + expect(store).toContainLocation({ |
| 143 | + pathname: '/foo' |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + it('updates the router even if path is the same', () => { |
| 148 | + history.push('/') |
| 149 | + |
| 150 | + const updates = [] |
| 151 | + const historyUnsubscribe = history.listen(location => { |
| 152 | + updates.push(location.pathname) |
| 153 | + }) |
| 154 | + |
| 155 | + history.push('/foo') |
| 156 | + history.push('/foo') |
| 157 | + history.replace('/foo') |
| 158 | + |
| 159 | + expect(updates).toEqual([ '/', '/foo', '/foo', '/foo' ]) |
| 160 | + |
| 161 | + historyUnsubscribe() |
| 162 | + }) |
134 | 163 | })
|
135 | 164 |
|
| 165 | + describe('Redux DevTools', () => { |
| 166 | + let history, store, devToolsStore |
| 167 | + |
| 168 | + beforeEach(() => { |
| 169 | + // Set initial URL before syncing |
| 170 | + testHistory.push('/foo') |
| 171 | + |
| 172 | + store = createStore( |
| 173 | + combineReducers({ |
| 174 | + routing: routerReducer |
| 175 | + }), |
| 176 | + instrument() |
| 177 | + ) |
| 178 | + devToolsStore = store.liftedStore |
| 179 | + |
| 180 | + history = syncHistoryWithStore(testHistory, store) |
| 181 | + }) |
| 182 | + |
| 183 | + afterEach(() => { |
| 184 | + history.unsubscribe() |
| 185 | + }) |
| 186 | + |
| 187 | + it('resets to the initial url', () => { |
| 188 | + let currentPath |
| 189 | + const historyUnsubscribe = history.listen(location => { |
| 190 | + currentPath = location.pathname |
| 191 | + }) |
| 192 | + |
| 193 | + history.push('/bar') |
| 194 | + devToolsStore.dispatch(ActionCreators.reset()) |
| 195 | + |
| 196 | + expect(currentPath).toEqual('/foo') |
| 197 | + |
| 198 | + historyUnsubscribe() |
| 199 | + }) |
| 200 | + |
| 201 | + it('handles toggle after history change', () => { |
| 202 | + let currentPath |
| 203 | + const historyUnsubscribe = history.listen(location => { |
| 204 | + currentPath = location.pathname |
| 205 | + }) |
| 206 | + |
| 207 | + history.push('/foo2') // DevTools action #2 |
| 208 | + history.push('/foo3') // DevTools action #3 |
| 209 | + |
| 210 | + // When we toggle an action, the devtools will revert the action |
| 211 | + // and we therefore expect the history to update to the previous path |
| 212 | + devToolsStore.dispatch(ActionCreators.toggleAction(3)) |
| 213 | + expect(currentPath).toEqual('/foo2') |
| 214 | + |
| 215 | + historyUnsubscribe() |
| 216 | + }) |
| 217 | + }) |
136 | 218 | })
|
137 | 219 | }
|
0 commit comments