我试图测试是否最终调用了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();
});
答案 0 :(得分:0)
我看不到您在考试中通过debouncePress
的地方!
wrapper = shallow(<Button onPress={jest.fn()} />); // you passing onPress but debouncePress is undefined
const { debounceTime } = props;
期望debounceTime
作为属性传递,但不在测试中