在Enzyme中,如何等待componentDidMount方法中的API调用?

时间:2020-10-14 16:06:34

标签: reactjs jestjs enzyme nock

我们有一个React组件,可以通过Enzyme进行安装:

const component = mount(<App/>);

该组件将数据加载到componentDidMount上,并用nock模拟:

nock('http://localhost:3100').get(`/api/data`).reply(200, DATA...);

我们要测试组件内部标签的value。但是,value仅在通过fetch调用加载数据之后才填充。在写期望之前,我们如何等待fetch调用完成:

expect(...

1 个答案:

答案 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线程

相关问题