Search code examples
reactjsreact-routerjestjsenzyme

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


I have header component like below:

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

const Header = () => {
   let route = useLocation().pathname; 
   return route === "/user" ? <ComponentA /> : <ComponentB />;
}

How will you mock this useLocation() to get the path as user?

I cant simply call the Header component as below in my test file as I am getting an error:

TypeError: Cannot read property 'location' of undefined at useLocation

describe("<Header/>", () => {
    it("call the header component", () => {
        const wrapper = shallow(<Header />);
        expect(wrapper.find(ComponentA)).toHaveLength(1);
    });
});

I have tried looking similar to the link How to test components using new react router hooks? but it didnt work.

I have tried like below:

const wrapper = shallow(
      <Provider store={store}>
        <MemoryRouter initialEntries={['/abc']}>
          <Switch>
            <AppRouter />
          </Switch>
        </MemoryRouter>
      </Provider>,
    );
    jestExpect(wrapper.find(AppRouter)
      .dive()
      .find(Route)
      .filter({path: '/abc'})
      .renderProp('render', { history: mockedHistory})
      .find(ContainerABC)
    ).toHaveLength(1);

from the link Testing react-router with Shallow rendering but it didnt work.

Please let me know.

Thanks in advance.


Solution

  • 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(
        <MemoryRouter initialEntries={[`/`]}>
          <MyRoutes />
          <Route
            path={`*`}
            render={routeProps => {
              testHistory = routeProps.history
              testLocation = routeProps.location
              return null
            }}/>
        </MemoryRouter>
      )
    
      // Manipulate wrapper
    
      expect(testHistory)...
      expect(testLocation)...
    )}