Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Commit 0c11a85

Browse files
committed
Merge pull request #289 from getstreamline/fix-initial-redirect
Fix issue when initial location redirects
2 parents dd9c395 + 47254cd commit 0c11a85

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
"karma-webpack": "^1.7.0",
6565
"mocha": "^2.3.4",
6666
"react": "^0.14.3",
67+
"react-dom": "^0.14.3",
68+
"react-redux": "^4.4.0",
69+
"react-router": "^2.0.0",
6770
"redux": "^3.0.4",
6871
"redux-devtools": "^3.0.0",
6972
"redux-devtools-dock-monitor": "^1.0.1",

src/sync.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ export default function syncHistoryWithStore(history, store, {
101101
listen(listener) {
102102
// Copy of last location.
103103
let lastPublishedLocation = getLocationInStore(true)
104-
// History listeners expect a synchronous call
105-
listener(lastPublishedLocation)
106104

107105
// Keep track of whether we unsubscribed, as Redux store
108106
// only applies changes in subscriptions on next dispatch
@@ -118,6 +116,11 @@ export default function syncHistoryWithStore(history, store, {
118116
}
119117
})
120118

119+
// History listeners expect a synchronous call. Make the first call to the
120+
// listener after subscribing to the store, in case the listener causes a
121+
// location change (e.g. when it redirects)
122+
listener(lastPublishedLocation)
123+
121124
// Let user unsubscribe later
122125
return () => {
123126
unsubscribed = true

test/_createSyncTest.js

+80
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import expect from 'expect'
22

3+
import React from 'react'
4+
import ReactDOM from 'react-dom'
5+
import { Router, Route, useRouterHistory } from 'react-router'
6+
import { Provider } from 'react-redux'
37
import { createStore, combineReducers } from 'redux'
48
import { ActionCreators, instrument } from 'redux-devtools'
59

@@ -214,5 +218,81 @@ export default function createTests(createHistory, name, reset = defaultReset) {
214218
historyUnsubscribe()
215219
})
216220
})
221+
222+
if (typeof(document) !== 'undefined') {
223+
describe('Redux Router component', () => {
224+
let store, history, rootElement
225+
226+
beforeEach(() => {
227+
store = createStore(combineReducers({
228+
routing: routerReducer
229+
}))
230+
231+
history = syncHistoryWithStore(useRouterHistory(createHistory)(), store)
232+
233+
rootElement = document.createElement('div')
234+
document.body.appendChild(rootElement)
235+
})
236+
237+
afterEach(() => {
238+
history.unsubscribe()
239+
rootElement.parentNode.removeChild(rootElement)
240+
})
241+
242+
it('syncs history -> components', () => {
243+
history.push('/foo')
244+
245+
ReactDOM.render(
246+
React.createElement(Provider, { store },
247+
React.createElement(Router, { history },
248+
React.createElement(Route,
249+
{
250+
path: '/',
251+
component: props => React.createElement('span', {}, props.children)
252+
},
253+
[ 'foo', 'bar' ].map(path =>
254+
React.createElement(Route, {
255+
path: path,
256+
component: () => React.createElement('span', {}, `at /${path}`)
257+
})
258+
)
259+
)
260+
)
261+
),
262+
rootElement
263+
)
264+
expect(rootElement.textContent).toEqual('at /foo')
265+
266+
history.push('/bar')
267+
expect(rootElement.textContent).toEqual('at /bar')
268+
})
269+
270+
it('syncs history -> components when the initial route gets replaced', () => {
271+
history.push('/foo')
272+
273+
ReactDOM.render(
274+
React.createElement(Provider, { store },
275+
React.createElement(Router, { history }, [
276+
React.createElement(Route, {
277+
path: '/',
278+
component: props => React.createElement('span', {}, props.children)
279+
}, [
280+
React.createElement(Route, {
281+
path: 'foo',
282+
onEnter: (nextState, replace) => replace('/bar')
283+
}),
284+
React.createElement(Route, {
285+
path: 'bar',
286+
component: () => React.createElement('span', {}, [ 'at /bar' ])
287+
})
288+
])
289+
])
290+
),
291+
rootElement
292+
)
293+
expect(rootElement.textContent).toEqual('at /bar')
294+
})
295+
})
296+
}
217297
})
218298
}

0 commit comments

Comments
 (0)