当作为反应组件中的道具传递时,如何模拟函数并测试它们被调用?

时间:2016-09-05 01:45:31

标签: javascript unit-testing reactjs jestjs

我跟随此stackoverflow回答中的示例 - Test a React Component function with Jest。我有一个示例组件和测试设置。加载到App.js时组件正常工作。

组件 -

import React, { PropTypes, Component } from 'react';

export default class ExampleModule extends Component {
  static propTypes = {
    onAction: PropTypes.func,
  }

  static defaultProps = {
  onAction: () => { console.log("In onAction"); }
}

doAction = () => {
  // do something else
  console.log('In do action');
  this.props.onAction();
}

render() {
  return(
    <div>
      <button className='action-btn' onClick=  {this.doAction.bind(this)}>Do action</button>
    </div>
  )
}
}

这是测试 -

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import ExampleComponent from './ExampleModule.js';

let Example;

describe('Example component', function() {
  beforeEach(function() {
    Example = TestUtils.renderIntoDocument(<ExampleComponent />);
  })

  it('calls props functions', function() {
    Example.doAction = jest.genMockFunction();
    let actionBtn = TestUtils.findRenderedDOMComponentWithClass(Example, 'action-btn');
    TestUtils.Simulate.click(actionBtn);
    expect(Example.doAction).toBeCalled();
  })

  it('doAction calls onAction', function() {
    expect(Example.props.onAction).not.toBeCalled();
    Example.doAction();
    expect(Example.props.onAction).toBeCalled();
  })
})

但是,我收到以下错误 -

FAIL  src/App/components/Example/ExampleModule.test.js
  Console

    console.log src/App/components/Example/ExampleModule.js:14
      In do action
    console.log src/App/components/Example/ExampleModule.js:24
      In onAction

  Example component › calls props functions

    Expected the mock function to be called.

      at Object.<anonymous> (src/App/components/Example/ExampleModule.test.js:17:30)
      at process._tickCallback (node.js:369:9)

  Example component › doAction calls onAction

    toBeCalled matcher can only be used on a spy or mock function.

      at Object.<anonymous> (src/App/components/Example/ExampleModule.test.js:21:40)
      at process._tickCallback (node.js:369:9)

即使我想模拟doAction,我也可以看到onActiondoAction中的console.logs正在被调用。 另外,我无法模仿onAction。我收到此错误 -

TypeError: Cannot assign to read only property 'onAction' of #<Object>

我已经尝试了jest.fn(),但却遇到了同样的错误。

如何模拟这些功能并对其进行测试?

编辑:

我能够通过以下方式使用jest.fn()来模拟doAction -

let mockFn = jest.fn();
Example.doAction = mockFn()

但是,我仍然无法模仿Example.props.onAction

1 个答案:

答案 0 :(得分:16)

在渲染文档时的测试中,你需要传递一个模拟函数,以后可以监视它是否被调用,如下所示 -

let onActionMock = jest.fn();

beforeAll(function() {
  Example = TestUtils.renderIntoDocument(<ExampleComponent onAction={onActionMock}/>);
});

beforeEach(function() {
  onActionMock.mockClear();
});

it('doAction calls onAction', () => {
  Example.doAction();
  expect(onActionMock).toBeCalled();
});

您可以使用 -

进一步测试该功能
expect(onActionMock).toBeCalledWith('Some argument');

希望这有帮助。