Skip to content

NavLink should render child components wrapped by withRouter #5141 #5142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion packages/react-router-dom/modules/__tests__/NavLink-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react'
import ReactDOM from 'react-dom'
import MemoryRouter from 'react-router/MemoryRouter'
import NavLink from '../NavLink'
import withRouter from '../withRouter'

describe('NavLink', () => {
const node = document.createElement('div')
Expand All @@ -22,6 +23,24 @@ describe('NavLink', () => {
expect(a.className).toEqual('active')
})

it('renders child components that use withRouter', () => {
class WrappedComponent extends React.Component {
render() {
return null
}
}
const Component = withRouter(WrappedComponent)

let ref
ReactDOM.render((
<MemoryRouter initialEntries={['/pizza']}>
<NavLink to='/pizza'><Component wrappedComponentRef={r => ref = r} /></NavLink>
</MemoryRouter>
), node)

expect(ref).toBeA(WrappedComponent)
})

it('applies its passed activeClassName', () => {
ReactDOM.render((
<MemoryRouter initialEntries={['/pizza']}>
Expand Down Expand Up @@ -63,6 +82,24 @@ describe('NavLink', () => {
expect(a.className).toNotContain('active')
})

it('renders child components that use withRouter', () => {
class WrappedComponent extends React.Component {
render() {
return null
}
}
const Component = withRouter(WrappedComponent)

let ref
ReactDOM.render((
<MemoryRouter initialEntries={['/pizza']}>
<NavLink exact to="/salad"><Component wrappedComponentRef={r => ref = r} /></NavLink>
</MemoryRouter>
), node)

expect(ref).toBeA(WrappedComponent)
})

it('does not apply its passed activeClassName', () => {
ReactDOM.render((
<MemoryRouter initialEntries={['/pizza']}>
Expand Down Expand Up @@ -272,7 +309,7 @@ describe('NavLink', () => {
const a = node.getElementsByTagName('a')[0]
expect(a.className).toNotContain('active').toContain('selected')
})

it('is not overwritten by the current location', () => {
ReactDOM.render((
<MemoryRouter initialEntries={['/pasta']}>
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/modules/withRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const withRouter = (Component) => {
const C = (props) => {
const { wrappedComponentRef, ...remainingProps } = props
return (
<Route render={routeComponentProps => (
<Route children={routeComponentProps => (
<Component {...remainingProps} {...routeComponentProps} ref={wrappedComponentRef}/>
)}/>
)
Expand Down