How to test components using new react router hooks?

后端 未结 6 510
庸人自扰
庸人自扰 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:29

    If using the enzyme library, I found a much less verbose way to solve the problem (using this section from the react-router-dom docs):

    import React from 'react'
    import { shallow } from 'enzyme'
    import { MemoryRouter } from 'react-router-dom'
    import Navbar from './Navbar'
    
    it('renders Navbar component', () => {
      expect(
        shallow(
          
            
          
        )
      ).toMatchSnapshot()
    })

提交回复
热议问题