测试customSelectComponent的onChange事件

时间:2020-02-29 06:23:18

标签: reactjs unit-testing jestjs onchange react-select

我正在尝试使用玩笑来测试我的customSelect组件。但是,它会引发错误-

匹配器错误:接收到的值必须是模拟或间谍函数

这是我的customSelect组件:

render() {
 const updatedProps = assign({}, this.props, {
  onChange: value => this.onChange(value, this.props.onChange || false),
  onBlur: event => this.onBlur(event, this.props.onBlur || false),
  onFocus: event => this.onFocus(event, this.props.onFocus || false),
 });

 return (<div className='customSelect'><Select ...updatedProps /></div>);
}

onChange:

onChange(value, changeHandler) {
 return Promise.resolve(changeHandler && changeHandler(value.value)).then(() => {
  !this.componentIsUnmounted && this.setState({
   selectedValue: value
  }, () => this.validate());
 });
}

测试案例:

test('verify onChange logic', () => {
    const props =  {
    label: 'label',
    options: [
      { value: 'value1', label: 'Value One' },
      { value: 'value2', label: 'Value Two' },
    ],
    defaultValue: 'defaultValue',
    onChange: jest.fn(),
  };
    const onChangeMockFn = jest.fn();
    const customSelectComponent = shallow(<CustomSelect {...props} />);
    const event = { target: { value: { value: 'testValue' } } };
    customSelectComponent.find(Select).simulate('change', event);
    expect(customSelectComponent.instance().onChange).toBeCalledWith(event.target.value, jest.fn());
    // expect(customSelectComponent.instance().state).toEqual('testValue');
});

0 个答案:

没有答案