2017-05-05 18 views
2

如何在调用updateItem之后让我的测试反映组件的新状态。我确实尝试了rendered.update(),但是我一定不知道如何工作。谢谢。阶级通过酶在类上调用方法时获取组件的更新状态

方法:

updateItem (id) { 
    return updateItem(id) 
    .then((result) => { 
     this.setState({ 
      item: {blah:'blah'} 
     }); 
    }, (error) => { 
     throw error; 
    }); 
} 

测试:

it("should update item accordingly",() => { 
    const rendered = shallow(React.createElement(Item)); 
    const result = rendered.instance().updateItem(); 
    expect(rendered.state('item')).toEqual({blah:'blah'}); 
}) 
+0

我不认为它会这样工作,通常如何在你的组件中调用函数。 –

+0

作为子组件回调 – groovy

+0

好的,但你可以从子元素道具中获取并调用它。 –

回答

1

你要得到孩子的道具功能,并调用它。当它返回一个承诺时,您需要使用异步/等待或从测试中返回承诺,请查看docs了解更多信息。然后测试渲染组件的状态。

it("should update item accordingly", async() => { 
    const rendered = shallow(React.createElement(Item)); 
    await rendered.find('someChild).prop('updateItem')('someId') 
    expect(item.state).toEqual({ 
     item: {blah:'blah'} 
    }) 
}) 
相关问题