GithubHelp home page GithubHelp logo

Comments (5)

kiuka avatar kiuka commented on August 16, 2024 1

@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.

scottrippey avatar scottrippey commented on August 16, 2024

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.

scottrippey avatar scottrippey commented on August 16, 2024

Implemented in #20 👍

from next-router-mock.

scottrippey avatar scottrippey commented on August 16, 2024

Fixed in 0.5.2

from next-router-mock.

kiuka avatar kiuka commented on August 16, 2024

You are the best, thank you very much!!

from next-router-mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.