React Router v4 Redirect unit test

后端 未结 3 1315
忘掉有多难
忘掉有多难 2021-02-04 00:50

How do I unit test the component in react router v4? I am unsuccessfully trying to unit test a simple component with a redirect using jest and enzyme.

My component:

3条回答
  •  难免孤独
    2021-02-04 01:20

    Answering my own question. Basically I'm making a shallow render of my component and verifying that if authenticated is rendering the redirect component otherwise the App one. Here the code:

    function setup() {
      const enzymeWrapper = shallow();
    
      return {
        enzymeWrapper
      };
    }
    
    describe("AuthenticatedApp component", () => {
      it("renders Redirect when user NOT autheticated", () => {
        authApi.isUserAuthenticated = jest.fn(() => false);
        const { enzymeWrapper } = setup();
    
        expect(enzymeWrapper.find(Redirect)).toHaveLength(1);
      });
    
      it("renders AppWithData when user autheticated", () => {
        authApi.isUserAuthenticated = jest.fn(() => true);
        const { enzymeWrapper } = setup();
    
        expect(enzymeWrapper.find(AppWithData)).toHaveLength(1);
      });
    });
    

提交回复
热议问题