mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
d654ccfc1a
There's apparently some interference with other tests when running the `withRouter` integration test in the CI, which leads to [frequent test failures](https://app.datadoghq.com/ci/test-runs?query=test_level%3Atest%20env%3Aci%20%40git.repository.id%3Agithub.com%2Fvercel%2Fnext.js%20%40test.service%3Anextjs%20%40test.suite%3A%2AwithRouter%2A%20%40test.status%3Afail&agg_m=count&agg_m_source=base&agg_t=count¤tTab=overview&eventStack=&fromUser=false&index=citest&start=1713009743249&end=1720785743249&paused=true). In lieu of identifying the interference source, we can refactor the test to an e2e test. This should also fix the flakiness. closes [NDX-64](https://linear.app/vercel/issue/NDX-64/investigate-flakiness-of-testintegrationwith-router) closes #67706
85 lines
2.0 KiB
JavaScript
85 lines
2.0 KiB
JavaScript
import * as React from 'react'
|
|
import Router, { withRouter } from 'next/router'
|
|
import Link from 'next/link'
|
|
|
|
const pages = {
|
|
'/a': 'Foo',
|
|
'/b': 'Bar',
|
|
}
|
|
|
|
class HeaderNav extends React.Component {
|
|
constructor({ router }) {
|
|
super()
|
|
|
|
this.state = {
|
|
activeURL: router.asPath,
|
|
activeURLTopLevelRouterDeprecatedBehavior: router.asPath,
|
|
activeURLTopLevelRouter: router.asPath,
|
|
}
|
|
}
|
|
|
|
componentDidMount() {
|
|
Router.onRouteChangeComplete =
|
|
this.handleRouteChangeTopLevelRouterDeprecatedBehavior
|
|
Router.events.on(
|
|
'routeChangeComplete',
|
|
this.handleRouteChangeTopLevelRouter
|
|
)
|
|
this.props.router.events.on('routeChangeComplete', this.handleRouteChange)
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
Router.onRouteChangeComplete = null
|
|
Router.events.off(
|
|
'routeChangeComplete',
|
|
this.handleRouteChangeTopLevelRouter
|
|
)
|
|
this.props.router.events.off('routeChangeComplete', this.handleRouteChange)
|
|
}
|
|
|
|
handleRouteChange = (url) => {
|
|
this.setState({
|
|
activeURL: url,
|
|
})
|
|
}
|
|
|
|
handleRouteChangeTopLevelRouter = (url) => {
|
|
this.setState({
|
|
activeURLTopLevelRouter: url,
|
|
})
|
|
}
|
|
|
|
handleRouteChangeTopLevelRouterDeprecatedBehavior = (url) => {
|
|
this.setState({
|
|
activeURLTopLevelRouterDeprecatedBehavior: url,
|
|
})
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<nav>
|
|
{Object.keys(pages).map((url) => (
|
|
<Link
|
|
href={url}
|
|
key={url}
|
|
prefetch
|
|
className={`${this.state.activeURL === url ? 'active' : ''} ${
|
|
this.state.activeURLTopLevelRouter === url
|
|
? 'active-top-level-router'
|
|
: ''
|
|
} ${
|
|
this.state.activeURLTopLevelRouterDeprecatedBehavior === url
|
|
? 'active-top-level-router-deprecated-behavior'
|
|
: ''
|
|
}`}
|
|
>
|
|
{pages[url]}
|
|
</Link>
|
|
))}
|
|
</nav>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default withRouter(HeaderNav)
|