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

Commit aa1130a

Browse files
committed
Fix basename path issues
1 parent 7d338ca commit aa1130a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ function locationsAreEqual(a, b) {
7171
return a != null && b != null && a.path === b.path && deepEqual(a.state, b.state)
7272
}
7373

74+
function createPath(location) {
75+
const { pathname, search, hash } = location
76+
let result = pathname
77+
if (search)
78+
result += search
79+
if (hash)
80+
result += hash
81+
return result
82+
}
83+
7484
function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
7585
const getRouterState = () => selectRouterState(store.getState())
7686

@@ -92,7 +102,7 @@ function syncReduxAndRouter(history, store, selectRouterState = SELECT_STATE) {
92102

93103
const unsubscribeHistory = history.listen(location => {
94104
const route = {
95-
path: history.createPath(location),
105+
path: createPath(location),
96106
state: location.state
97107
}
98108

test/createTests.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { pushPath, replacePath, UPDATE_PATH, routeReducer, syncReduxAndRouter } =
55
const { createStore, combineReducers, compose } = require('redux')
66
const { devTools } = require('redux-devtools')
77
const { ActionCreators } = require('redux-devtools/lib/devTools')
8+
const { useBasename } = require('history')
89

910
expect.extend({
1011
toContainRoute({
@@ -602,5 +603,21 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
602603
).toNotThrow()
603604
})
604605
})
606+
607+
it('handles basename history option', () => {
608+
const store = createStore(combineReducers({
609+
routing: routeReducer
610+
}))
611+
const history = useBasename(createHistory)({ basename:'/foobar' })
612+
syncReduxAndRouter(history, store)
613+
614+
store.dispatch(pushPath('/bar'))
615+
expect(store).toContainRoute({
616+
path: '/bar',
617+
changeId: 2,
618+
replace: false,
619+
state: undefined
620+
})
621+
})
605622
})
606623
}

0 commit comments

Comments
 (0)