How do you mock useLocation() pathname using shallow test enzyme Reactjs?

前端 未结 3 2457
别那么骄傲
别那么骄傲 2021-02-20 18:56

I have header component like below:

import { useLocation } from \"react-router-dom\";

const Header = () => {
   let route = useLocation().pathname; 
   retur         


        
3条回答
  •  无人共我
    2021-02-20 19:31

    I know this isn’t a direct answer to your question, but if what you want is to test the browser location or history, you can use mount and add an extra Route at the end where you can “capture” the history and location objects.

    test(`Foobar`, () => {
      let testHistory
      let testLocation
    
      const wrapper = mount(
        
          
           {
              testHistory = routeProps.history
              testLocation = routeProps.location
              return null
            }}/>
        
      )
    
      // Manipulate wrapper
    
      expect(testHistory)...
      expect(testLocation)...
    )}
    

提交回复
热议问题