Skip to content

Commit

Permalink
bugfix #4292 (#4295)
Browse files Browse the repository at this point in the history
* withRouter: Return the wrapper component if no router is present fixes #4292

* Add test for without Router case
  • Loading branch information
webdeb authored and timdorr committed Dec 19, 2016
1 parent e06190e commit bc66a96
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions modules/__tests__/withRouter-test.js
Expand Up @@ -128,4 +128,17 @@ describe('withRouter', function () {
done()
})
})

it('should render Component even without Router context', function (done) {
const MyComponent = withRouter(({ router }) => {
expect(router).toNotExist()

return <h1>Hello</h1>
})

render((<MyComponent />), node, function () {
expect(node.firstChild.textContent).toEqual('Hello')
done()
})
})
})
4 changes: 4 additions & 0 deletions modules/withRouter.js
Expand Up @@ -29,6 +29,10 @@ export default function withRouter(WrappedComponent, options) {

render() {
const router = this.props.router || this.context.router
if (!router) {
return <WrappedComponent {...this.props} />
}

const { params, location, routes } = router
const props = { ...this.props, router, params, location, routes }

Expand Down

0 comments on commit bc66a96

Please sign in to comment.