无法使用Enzyme React Native模拟“ TouchableOpecity” onPress()事件

时间:2020-04-15 14:36:10

标签: react-native jestjs react-native-android enzyme react-native-ios

样式化的TouchableOpacity不会使用onPress道具进行渲染,因此无法在Jest中模拟按下事件以及如何在onPress事件中调用方法。

<TouchableOpacity
    testID={'NextBtn'}
    onPress={() => {
      this._submit();
    }}>
    <Text>Submit</Text>
  </TouchableOpacity>

测试课程

it("onSubmit", () => {
let wrapper = mount(<Login />);
const buttonPress = wrapper.find('TouchableOpacity')
  .findWhere((node) => {
    if (node.prop('testID') === 'NextBtn') {
      console.log('CtestID', node.prop('testID'))
    }
  })
  .first();
except(buttonPress).toHaveBeenCalled();

});

0 个答案:

没有答案