Skip to content

Commit c8a7dbb

Browse files
committed
Fix basename support. Add test.
1 parent 07725d7 commit c8a7dbb

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

modules/StaticRouter.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ class StaticRouter extends React.Component {
4646

4747
getChildContext() {
4848
const createHref = (to) => {
49-
const path = createRouterPath(
50-
this.props.basename ? this.props.basename + to : to,
51-
this.props.stringifyQuery
52-
)
49+
let path = createRouterPath(to, this.props.stringifyQuery)
50+
if (this.props.basename) path = this.props.basename + path
5351
return this.props.createHref(path)
5452
}
5553

modules/__tests__/StaticRouter-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import expect from 'expect'
22
import React from 'react'
33
import StaticRouter from '../StaticRouter'
4+
import { router as routerType } from '../PropTypes'
45
import { renderToString } from 'react-dom/server'
56

67
//console.error = () => {}
@@ -187,4 +188,32 @@ describe('StaticRouter', () => {
187188

188189
})
189190
})
191+
192+
describe('basename support', () => {
193+
class Test extends React.Component {
194+
static contextTypes = {
195+
router: routerType
196+
}
197+
198+
render() {
199+
return <div>{this.context.router.createHref('/bar')}</div>
200+
}
201+
}
202+
203+
const BASENAME = "/foo"
204+
const routerProps = {
205+
location: '/',
206+
action: 'POP',
207+
onPush: () => {},
208+
onReplace: () => {}
209+
}
210+
211+
it('uses the basename when creating hrefs', () => {
212+
expect(renderToString(
213+
<StaticRouter {...routerProps} basename={BASENAME}>
214+
<Test />
215+
</StaticRouter>
216+
)).toContain(BASENAME)
217+
})
218+
})
190219
})

0 commit comments

Comments
 (0)