我想测试我的组件,假设它叫<TestComponent />
该组件使用装饰器来使用redux,我无法改变它,因为如果我改变它,那么我将不得不改变其他组件中的方法。
我可以使用以下代码访问组件的状态:
describe("Component States After API Calls", ()=>{
const wrapper = mount(<Provider store={store}><TestComponent/></Provider>)
var testcomponent = wrapper.find("testcomponent")
const expectedState = {
isLoading: false,
testdata: [],
loaded: true,
loading: false
}
it("Should match the expectedState", ()=>{
expect(testcomponent.getNode().state).toEqual(expectedState)
})
但是我不确定它是否足够好,是否有更好的方法以较少的步骤实现相同的结果?
谢谢