【问题标题】:Testing using enyzme shallow and react-intl with react native使用 enyzme shallow 和 react-intl 与 react native 进行测试
【发布时间】:2017-10-30 08:05:04
【问题描述】:

我一直在尝试使用酶浅渲染来模拟 onPress。 但是由于 react-intl,我无法访问 TouchableOpacity

我已经看到了将 intl 注入到 react 组件中的辅助函数,但这些似乎只适用于 React 而不是 react-native。

it("should handle button presses", () => {
  const onPress = sinon.spy();
  const button = shallow(
    <IntlProvider locale='en'>
      <ButtonApprove
        tintColor={STYLES.COLOR.BRAND_SUCCESS}
        buttonStyle={{}}
        containerStyle={{}}
        handlePress={onPress}
      /> 
    </IntlProvider>
  );
  // console.log(button.instance());
  button.find(TouchableOpacity).simulate('press');
  expect(onPress.calledOnce).toEqual(true);
});

JSdom 会是解决这个问题的方法吗?

【问题讨论】:

标签: react-native enzyme react-intl


【解决方案1】:

这种方法帮助我避免使用 IntlProvider 包装我的组件。

How can I test react-native component with mocha + enzyme + chai when it's wrapped in a Provider component

我能够使用 .dive() 和 .instance() 遍历到所需的组件

希望这有助于其他人处理同样的问题。

【讨论】:

    猜你喜欢
    • 2018-12-11
    • 2020-05-12
    • 2018-09-07
    • 1970-01-01
    • 2017-10-01
    • 2016-11-12
    • 2016-08-20
    • 2020-07-17
    • 2021-08-02
    相关资源
    最近更新 更多