[英]How to test a async ComponentDidMount in React Native?
I do have a SplashContainer with a async componentDidMount
我确实有一个带有async componentDidMount
的SplashContainer
export class SplashContainer extends Component {
async componentDidMount() {
let token = await AsyncStorage.getItem('@XXX:token')
if (token !== null) {
await this.props.setTokenAvalability(true)
await this.props.getUserDetails()
}
await this.props.navToNextScreen()
}
render() {
return <Splash />
}
}
function mapDispatchToProps(dispatch) {
return {
navToNextScreen: () => dispatch(navToNextScreen(...)),
setTokenAvalability: (status) => dispatch(setTokenAvalability(status)),
getUserDetails: () => dispatch(getUserDetails()),
}
}
export default connect(null, mapDispatchToProps)(SplashContainer);
I do have two questions here. 我在这里有两个问题。
1 . 1 。 I wanted to test setTokenAvalability and getUserDetails
is been dispatched or not. 我想测试setTokenAvalability and getUserDetails
是否已调度。 I do know how to test if there is no async/await, like below. 我确实知道如何测试是否没有异步/等待,如下所示。
it('test SplashContainer', () => {
const store = mockStore({});
const props = {
dispatch: store.dispatch
}
const tree = renderer.create(
<SplashContainer {...props}/>
).toJSON();
const expectedAction = [
...
]
expect(store.getActions()).toEqual(expectedAction);
});
2 . 2 。 How to stub value for AsyncStorage.getItem()
如何存根AsyncStorage.getItem()
值
Thanks, 谢谢,
Well, componentDidMount is not an async function, the only thing you can do here I think is to use the componentDidUpdate
method which will give you an update of your component. 好吧,componentDidMount不是一个异步函数,我在这里唯一能做的就是使用componentDidUpdate
方法,该方法将为您提供组件的更新。
Reading the this.props.navToNextScreen()
function, I think you misunderstand how redux works here. 我认为阅读this.props.navToNextScreen()
函数this.props.navToNextScreen()
您误解了redux在这里的工作方式。
You probably don't need to wait for the navToNextScreen function, it should just send a global event and your main component should listen to a change in your store to show / hide your Splash screen 您可能不需要等待navToNextScreen函数,它应该只发送一个全局事件,并且您的主要组件应该监听商店中的更改以显示/隐藏您的启动屏幕
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.