使用Jest / Enzyme测试本机功能组件中的lodash反跳

时间:2019-03-04 22:01:52

标签: javascript react-native jestjs lodash enzyme

我试图测试是否最终调用了props.onPress,但出现“ TypeError:debouncePress不是函数”。有什么想法吗?

import _ from 'lodash';

export const Button = props => {
  const { debounceTime } = props;
  const debouncePress = _.debounce(() => {
    if (props.onPress) props.onPress();
  }, debounceTime, { leading: true, trailing: false });

  const handlePress = () => {
    debouncePress();
  };

  return (
    <TouchableOpacity testID="touchableOpacity" onPress={handlePress}>
      {props.label ?<Text>{props.label}</Text> : props.children}
    </TouchableOpacity>
  );
};

这是到目前为止的测试

it('should call onPress when press event occurs', () => {
    wrapper = shallow(<Button onPress={jest.fn()} />);
    wrapper.find({ testID: 'touchableOpacity' }).simulate('press');
    expect(props.onPress).toHaveBeenCalled();
});

1 个答案:

答案 0 :(得分:0)

我看不到您在考试中通过debouncePress的地方!

wrapper = shallow(<Button onPress={jest.fn()} />); // you passing onPress but debouncePress is undefined

const { debounceTime } = props;期望debounceTime作为属性传递,但不在测试中