Jest Test:如何在React组件方法中覆盖导入的辅助函数调用

时间:2017-03-10 20:00:59

标签: reactjs jestjs enzyme

在我的React组件中,我有一个调用导入的辅助函数的实例方法。我现在正在编写的当前Jest测试不应该关心实例方法中对导入的辅助函数的调用。那么我如何用一些真的没有做任何事情的东西覆盖帮助函数的定义呢?

MyComponent的:

import { callSomeHelperFunction } from './some/where';

class MyComponent extends Component {
  ...
  onSomethingChange(event) {
    // ...
    callSomeHelperFunction(data, this.props, name, etc);
    this.props.doWhatever();
  }
  ...
}

测试

const onSomethingChange = jest.fn();
const component = shallow(
  <MyComponent ... onSomethingChange={onSomethingChange} />
);

const event = {};

// right here onSomethingChange is called, with first a call to
// callSomeHelperFunction(), but I really don't care about that.
// The test fails because some parameter is not provided to the
// helper function callSomeHelperFunction().

component.instance().onSomethingChange(event);
expect(onSomethingChange).toHaveBeenCalled();

0 个答案:

没有答案