2017-03-02 60 views
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()

感谢,

回答

0

嘛,componentDidMount不是一个异步功能,您可以在这里做,我认为是使用componentDidUpdate方法,这将给你你的组件的更新的唯一的事情。

读取this.props.navToNextScreen()函数,我想你误解了redux在这里的工作原理。

您可能不需要等待navToNextScreen功能,它应该只发送一个全局事件,并且您的主要组件应该听取您商店中的更改以显示/隐藏您的Splash屏幕

相关问题