用酶模拟Picker的onvaluechange事件

时间:2018-02-20 09:01:35

标签: react-native enzyme jest

我正在尝试使用Jest和Enzyme来测试onValueChange for Picker,我不确定我是否正确地使用它。

我的组件:

const ManufacturersPicker = ({
   Manufacturers,
   ManufacturersPickerOnValueChange,
   selectedManufacturerId,
}) => {
  const ManufacturersPickerItems = [];
  ManufacturersPickerItems.push(Manufacturers.map(item => (
  <Picker.Item
    label={item.manufacturer}
    value={item.manufactureId}
  />)));

  return (
    <Picker
        selectedValue={selectedManufacturerId}
        onValueChange={(itemValue, itemIndex) => 
        ManufacturersPickerOnValueChange(itemValue, itemIndex)}
        mode="dialog"
    >
        {ManufacturersPickerItems}
    </Picker>
  );
};

我的测试如下

const manufacturersPickerOnValueChange = jest.fn();
const valueChangeEvent = {target: {value: 1, index: 0}};
const tree = shallow(<ManufacturersPicker
        Manufacturers={manufacturers}
        ManufacturersPickerOnValueChange={manufacturersPickerOnValueChange}
        selectedManufacturerId={1}/>);
tree.find('Picker').simulate('onValueChange', valueChangeEvent);
expect(manufacturersPickerOnValueChange).toHaveBeenCalledTimes(1);
expect(manufacturersPickerOnValueChange).toHaveBeenCalledWith(1,0);

我一直收到错误,表明没有调用jest函数

expect(jest.fn()).toHaveBeenCalledTimes(1)
Expected mock function to have been called one time, but it was called zero times.

我不确定这是我传递的事件是错误还是发现错误的元素。

0 个答案:

没有答案