我们有一个React组件,可以通过Enzyme进行安装:
const component = mount(<App/>);
该组件将数据加载到componentDidMount
上,并用nock模拟:
nock('http://localhost:3100').get(`/api/data`).reply(200, DATA...);
我们要测试组件内部标签的value
。但是,value
仅在通过fetch调用加载数据之后才填充。在写期望之前,我们如何等待fetch调用完成:
expect(...
答案 0 :(得分:0)
您将必须执行以下操作:
Promise
.resolve(mounted)
.then(() => mounted.update())
.then(() => {
expect(label === value); // Just using pseudocode here to denote where to write the expect statement
})
您在这里可能仍会遇到不正确的行为,在这种情况下,您可以检查this线程