我正在为React组件使用酶和jest-of编写测试用例。我的react组件的定义类似
class MyComponent extends React.Component {
// some code here
showModalandSetValues () {
//some code here
this.confirmModal.setFormValues (values);
}
render (){
const {val1 , val2} = this.props
return <div>
// some more markup
<MyForm>
<MyModal ref = {ref => this.confirmModal = ref}} />
</MyForm>
</div>
}
}
export default MyComponent
我用的是浅酶法。如何在测试中设置this.confirmModal的值?它只能通过渲染方法进行设置。请提供建议。
他们说引用没有在shallow()
中设置。
您可以使用mount()
但模拟子组件来实现半浅层渲染:
import MyModal from '../some/path/MyModal.js'; // to be able to .find(MyModal)
// modal is rendered as a span
jest.mock('../some/path/MyModal.js', () => (props) => <span {...props} />);
...
const wrapper = mount(<MyComponent />);