react-native - 用多个状态渲染反应原生测试快照?
问题描述
componentDidMount
我有一个组件在其方法中多次设置状态。例如:
componentDidMount = async () => {
this.setState({ waiting: true });
try {
const data = await this.fetchData();
this.setState({ data })
} catch (err) {
} finally {
this.setState({ waiting: false });
}
};
使用类似于以下的渲染方法:
render() {
if (this.state.waiting) {
return (
<View style={{ flex: 1 }}>
<ActivityIndicator size="large" />
</View>
);
}
return (
... actual view ...
)
}
我可以运行测试react-native-testing-library
const tree = testing.render(<HomeScreen {...props} />);
然而我只得到等待视图。但是,当我逐步完成测试时,其他渲染被击中。
所以看起来快照测试只是捕获第一个渲染。有没有办法让它给我最终的渲染?
解决方案
原来我需要等待微任务。
await testing.flushMicrotasksQueue();
做到了
推荐阅读
- apache-spark-sql - 使用直线连接到 spark thift 服务器时无法加载哨兵配置
- asp.net-mvc - 快速报告预览 - C# MVC
- angular - Ionic:promise 是如何工作的?
- python-2.7 - 压缩 .npy 数据以节省磁盘空间
- data-warehouse - 维度模型 VS 非归一化模型
- sql - 状态的平均持续时间 - 差距和岛屿
- python - 在 Paramiko 中计算文件传输的剩余时间
- python - Streamlabs API 405 响应代码
- r - RJDBC:类(obj)<-“rs.scalar”中的错误:无法在符号上设置属性
- c# - 我可以将外部配置与 ASP.NET Core 的默认依赖注入一起使用吗?