0
我有一个async componentDidMount
如何测试React Native中的异步ComponentDidMount?
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);
一个SplashContainer我有两个问题在这里。
。我想测试setTokenAvalability and getUserDetails
已被调度或不调度。我知道如何测试是否没有异步/等待,如下所示。
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);
});
。如何存根价值AsyncStorage.getItem()
感谢,