How to test components using new react router hooks?

后端 未结 6 506
庸人自扰
庸人自扰 2020-12-08 01:52

Until now, in unit tests, react router match params were retrieved as props of component. So testing a component considering some specific match, with specific url parameter

6条回答
  •  臣服心动
    2020-12-08 02:42

    I looked at the tests for hooks in the react-router repo and it looks like you have to wrap your component inside a MemoryRouter and Route. I ended up doing something like this to make my tests work:

    import {Route, MemoryRouter} from 'react-router-dom';
    
    ...
    
    const renderWithRouter = ({children}) => (
      render(
        
          
            {children}
          
        
      )
    )
    

    Hope that helps!

提交回复
热议问题