2016-11-18 76 views
1

在分量I具有以下div的渲染部分:發送道具到陣營組件,其用於一玩笑單元測試

<div className="col-sm-8" > 
<input ref='full_link' className="formctrl" name='full_link' type='text' 
value={browser.getURL(this.props.params.id)} /> 
</div> 

要爲該組件寫一個測試單元,我試圖淺使其( const component = shallow(<myComponent />);)並返回該錯誤:

TypeError: Cannot read property 'id' of undefined 

所以我需要在測試這個道具發送到我的組件。我應該如何將idthis.props.params.id發送到淺層渲染?

回答

4

剛剛通過的道具,你通常會使用該組件時做:

const component = shallow(<myComponent params={{ id: 1 }} />) 

不要忘記JSX只是做華麗的方式:

React.createElement(MyComponent, { params: { id: 1 } }); 

注意:你叫你組件myComponent,但在JSX中,用戶定義的組件名稱應以大寫字母(MyComponent)開頭,而「正常」元素(如div)以小寫字母開頭。

+0

謝謝法比奧:) – Birish