2016-03-22 22 views
0

我試圖模擬點擊反應組件中的dom元素並聲明基於此調用了間諜。下面是測試的相關部分(利用人緣,摩卡,薛寶釵,興農但我相當肯定這是無關緊要這裏):反應測試實用程序 - 執行模擬點擊時未調用間諜 -

const selectSpy = sinon.spy(); 
const component = TestUtils.renderIntoDocument(<SomeComponent onSelect={selectSpy} />); 
TestUtils.Simulate.click(TestUtils.scryRenderedDOMComponentsWithTag(component, 'li')); 
expect(selectSpy.called).to.be.ok; 

和這裏的反應成分:

render() { 
    let { title , data, onSelect } = this.props; 
    console.log(this.props.onSelect); // This correctly logs out the spy 
    return (
     <div> 
     <ul>{data.map((row, i) => { 
      return <li onClick={this.handle.bind(this,row)} key={i}>{row.title}</li>; })} 
     </ul> 
     </div> 
    ) 
} 

handle(row) { 
    this.props.onSelect(row); 
} 

試運行但結果是一個失敗的測試,並顯示「預計虛假是真的」。我看不出有太多錯誤的代碼 - 沒有錯誤,我的間諜被正確傳遞。 還有什麼我需要做的?

回答

1

TestUtils.Simulate.click需要一個DOM元素,但scryRenderedDOMComponentsWithTag會返回一個DOM元素數組。

嘗試改變

TestUtils.Simulate.click(TestUtils.scryRenderedDOMComponentsWithTag(component, 'li')); 

TestUtils.Simulate.click(TestUtils.scryRenderedDOMComponentsWithTag(component, 'li')[0]); 
+0

就是這樣。非常感謝 –

相關問題