我正在使用酶、jest、sinon进行单元测试。我想模拟一个组件状态,并在组件渲染之前传入自定义状态。我怎样才能做到这一点?
我的组件从一些初始状态开始:
constructor(props) {
super(props);
this.state = {
sample: "hello"
}
}
我想在渲染发生之前,在调用shallow((之前,通过模拟假状态来覆盖sample的值。
是否直接在组件上调用setState?
不是100%确定,但也许。。。
const myComponent = <MyComponent {...props} />
myComponent.setState({ ...mockState })
const myShallowRenderedComponent = shallow(myComponent)
const instance = myShallowRenderedComponent .instance()
expect(instance.state).toEqual(mockState)