您如何使用Enzyme和Jest在功能内测试Promise?

时间:2019-01-16 17:03:05

标签: reactjs jestjs enzyme

因此,我在一个函数中有一个promise解析,并且一直试图对其进行测试。有什么想法可以尝试吗?

  //I am currently using the below test case. 
  it('Test onIdle', () =>{
   const props = { route: { routes: [] }, hasError: true, userLoggedOut: 
                  jest.fn()  };
  const spy = jest.spyOn(MyComponent.prototype, 'onIdle');
  const wrapper = shallow(<MyComponent{...props} state={{isloggedIn : 
   false}}/>);
  wrapper.instance().onIdle();
  expect(spy).toHaveBeenCalled();
 });


 //function in react component
  onIdle(){
    getCode().then(result => {
        Logout(result, this.props.history).then(res => {
         this.props.userLoggedOut()
       }).catch(error => {
     })
   }).catch(error => {
  })
 }

它目前正在测试除其中功能以外的所有内容,并且迫切需要帮助。

0 个答案:

没有答案