Comments (5)
@scottrippey thank you for your quick answer and to taking care of it! Meanwhile I have sort of copied the code from next/router to make it work, it looks like this now, maybe it can help you as well (probably much less code without types:) ):
jest.mock('next/router', () => {
const router = require('next-router-mock')
return {
__esModule: true,
default: router,
withRouter<P extends WithRouterProps, C = NextPageContext>(
ComposedComponent: NextComponentType<C, any, P>
): React.ComponentType<ExcludeRouterProps<P>> {
function WithRouterWrapper(props: any): JSX.Element {
return <ComposedComponent router={router.useRouter()} {...props} />
}
WithRouterWrapper.getInitialProps = ComposedComponent.getInitialProps
// This is needed to allow checking for custom getInitialProps in _app
;(WithRouterWrapper as any).origGetInitialProps = (
ComposedComponent as any
).origGetInitialProps
if (process.env.NODE_ENV !== 'production') {
const name = ComposedComponent.displayName || ComposedComponent.name || 'Unknown'
WithRouterWrapper.displayName = `withRouter(${name})`
}
return WithRouterWrapper
},
}
})
from next-router-mock.
You're correct, I have not implemented the withRouter
method. However, I do think it would be pretty trivial to implement this, so I'll give it a shot :)
Thank you for the example use-case!
from next-router-mock.
Implemented in #20 👍
from next-router-mock.
Fixed in 0.5.2
from next-router-mock.
You are the best, thank you very much!!
from next-router-mock.
Related Issues (20)
- `next/link` not working with `passHref` HOT 1
- Next 13.4.20-canary.20 issue HOT 8
- MemoryRouterProvider url property does not work properly HOT 3
- Incorrect path in types
- With Next14, the path to the RouterContext seems to be wrong. HOT 2
- next/link not working with async HOT 2
- Modify the as param condition HOT 2
- configuring webpack alias in storybook, it doesn't to work in storybook test HOT 9
- `router.back` support HOT 4
- Any support planned for `next` version `13.2.x` ? HOT 3
- Add a changelog HOT 2
- In 0.9.3 test asserting `window.location.assign` fails HOT 1
- Is it possible to use dynamic routes with the MemoryRouterProvider for rendering in storybook? HOT 3
- dist folder auto-import inconsistencies HOT 2
- Using `next/link` with the locale property. HOT 1
- How to mock next router with jest `experimental-vm-modules` HOT 3
- router.push({ hash }) not triggering a re-render HOT 2
- Inconsistent useRouter behavior compared to the Next.js Router HOT 7
- Consider the skipTrailingSlashRedirect config HOT 1
- mocked next/link behavior seems inconsistenst with next/link from next.js when the `as` prop is used HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-router-mock.