2017-05-05 34 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'} 
    }) 
}) 
相關問題