React & Jest, how to test changing state and checking for another component

后端 未结 2 474
孤街浪徒
孤街浪徒 2020-12-08 20:07

React - Test Utilities Docs

I have a Login component which will display a Notification component if this.state.error i

2条回答
  •  无人及你
    2020-12-08 20:35

    Figured it out! Did not need React Test Utilities

    it('should render the Notification component if state.error is true', () => {
        const loginComponent = shallow();
        loginComponent.setState({ error: true });
        expect(loginComponent.find(Notification).length).toBe(1);
    });
    

    This will set the state of error to true in the Login component, then check if the Login component contains the Notification component.

提交回复
热议问题