简体   繁体   English

如何在React Native中测试异步ComponentDidMount?

[英]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.

相关问题 如何测试在componentDidMount中设置异步调用以设置React Component的状态 - How to TEST async calls made in componentDidMount that set the state of React Component 如何使用链式承诺测试react componentDidMount中的异步提取? - How do I test async fetch in react componentDidMount with chained promises? React Native中的componentDidMount()警告中的异步SetState - async SetState in componentDidMount() warning in React Native React Enzyme - 测试`componentDidMount`异步调用 - React Enzyme - Test `componentDidMount` Async Call 使用 react-test-renderer 测试异步 componentDidMount() - Testing async componentDidMount() with react-test-renderer 如何通过React componentDidMount()方法使用异步等待? - How to use async await with React componentDidMount() method? 如何在 componentDidMount 中存根异步操作以使用 Jest 进行反应 redux 连接组件测试 - How to stub async action in componentDidMount for react redux connected component test using Jest React Native组件未使用componentWillMount或componentDidMount中的异步redux操作呈现 - React Native component not rendering with async redux action in componentWillMount or componentDidMount React Native从异步组件获取初始状态 - React Native get initial state from async componentDidMount 反应本机提取和componentDidMount - React native fetch and componentDidMount
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM