我的组件:
componentDidMount() {
// Make HTTP reques with Axios
axios.get(APIConfig.api_profile()).then((res) => {
// Set state with result
this.setState(res.data);
console.log('I was triggered during componentDidMount')
console.log(res)
});
}
我的测试:
//@see https://github.com/ctimmerm/axios-mock-adapter
mock.onGet(APIConfig.api_profile()).reply(200, {
"id_user": "1",
"id_person": "1",
"imageUrl": "",
"email": "xyz@zyz.com",
"name": "xyz xyz"
});
test('xyz', async() => {
var ProfilePic2 =require('../../src/views/ProfilePic');
const component = renderer.create(
<ProfilePic/>
);
expect(component.state).toBeDefined();
//tree.props.setProfile({})
let tree = component.toJSON();
await expect(tree).toMatchSnapshot();
});
问题是 jest 正在测试初始渲染,而我需要在收到 API 响应后对其进行测试。因此,它所比较的快照也大多是空的。
我无法让测试等到第二次渲染之后。我只是在尝试等待/异步但无法让它工作。我可以看到我的 api mocs 是从控制台日志中调用的。