3
我使用Jest測試了React組件的表單輸入,但是我無法找到使用React TestUtils選擇表單輸入字段進行輸入的最佳方法。在線文檔顯示了一個使用refs
的示例,但React文檔還聲明refs
應僅用於父級而不是組件的子級。我應該將refs
附加到每個輸入字段還是有更好的方式來遍歷DOM並選擇特定的元素,以便我可以模擬點擊事件?在React with Jest中測試模擬表單輸入
這是我的渲染
render (
<form className="products">
<input onChange={this.handleName} name="name" type="text" />
<input onChange={this.hanndleAge} name="age" type="text" />
</form>
)
和我的測試
it('Should parse input fields',() => {
Product = TestUtils.renderIntoDocument(<ProductComponent />);
// This returns all the inputs but how do tell them apart?
let inputs = TestUtils.scryRenderedDOMComponentsWithTag(Product, 'input');
});