JEST component testing

后端 未结 2 1157
说谎
说谎 2020-12-22 00:44

I am new to unit testing with Jest. I am testing a component in a React app. There are two components: Home and LogOutButton

This is The Ho

2条回答
  •  北海茫月
    2020-12-22 01:31

    A fix for your test is a little addition to what you already have.

    delete window.location;
    window.location = { reload: jest.fn() };
    
    describe('Home', () => {
      it('should click logout button', () => {
        const component = Enzyme.mount();
        const logOutButton = component.find(LogOutButton);
        logOutButton.simulate('click');
        expect(window.location.href).toEqual('https://www.MICROSOFT.com');
      });
    });
    

    You quickly notice I deleted window.location as window.location is not modifiable in Jest. That is the missing link.

提交回复
热议问题