testing-library / react-hooks-testing-library

🐏 Simple and complete React hooks testing utilities that encourage good testing practices.
https://react-hooks-testing-library.com
MIT License
5.26k stars 233 forks source link

useHistory in react-router-dom v5 #912

Closed zhex900 closed 2 years ago

zhex900 commented 2 years ago

What is your question:

I want to use useHistory hook in my test.

    const {result} = renderHook(() => useHistory(), {
      wrapper: BrowserRouter,
    })

The problem is the result is got giving me the correct route. It seems like the mounted App and the useHistory hook in the test are using two different instances of BrowserRouter

I want to reference useHistory hook inside the test. So I can know the current path and change routes.

mpeyper commented 2 years ago

You'll need to share some more code and/or clarify what are are try to do. It sounds like you want to render App (presumably using react-testing-library then use renderHook to read the value of useHistory from the other render.

If that is the case then I'm sorry, but that is not what this library is for. renderHook renders the hook inside its own react tree so that you can test the hook in isolation of the component code. We do not share context or rendered component values with any other react tree by design (and I'm not sure it's technically possible either).

zhex900 commented 2 years ago

Thank you.