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()
感謝,