简体   繁体   English

在Jest中调用React的this.props.onClick(this)时我究竟要测试什么?

[英]What exactly should I test when calling React's this.props.onClick(this) in Jest?

I've been writing tests for all my events (and everything else of course) but I'm at a loss on how to test that this.props.onClick(this) was called on a child component. 我一直在为我的所有事件(以及其他所有事件)编写测试但是我对如何测试this.props.onClick(this)在子组件上调用感到茫然。

My child component has the following code: 我的子组件具有以下代码:

closeModal: function() {
  this.props.onClick(this);
},

render: function() {
  return (
    <i className="close-icon" onClick={this.closeModal}></i>
  )
}

and the parent is listening to it like so: 并且父母正在这样听:

onCloseModal: function() {
  this.replaceState({
    modalStatus: 'hidden'
  });
},

render: function() {
  return (
    <QuestionModal modalStatus={this.state.modalStatus} onClick={this.onCloseModal} />
  )
}

I know how to test the parent's click event and I know how to call the child's button click event in the tests as well but I'm not sure what I should be exactly testing. 我知道如何测试父母的点击事件,我知道如何在测试中调用孩子的按钮点击事件,但我不确定我应该准确测试什么。

If I used Sinon and Jasmine, I would stub the closeModal method and check that it was called. 如果我使用了Sinon和Jasmine,我会将closeModal方法存根并检查它是否被调用。 Can I do that with Jest and if so how exactly? 我可以用Jest做到这一点,如果是这样,究竟是怎么回事?

UPDATE UPDATE

I've tried writing a test based on @PhilVarg's answer but I'm not getting very far as I'm not able to mock closeModal . 我已经尝试过根据@ PhilVarg的答案编写测试但我没有达到很远,因为我无法模拟closeModal

Here's my test: 这是我的测试:

      var closeIcon,
          myMock = jest.genMockFunction();

      form = TestUtils.renderIntoDocument(
        <QuestionForm />
      );
      form.closeModal = myMock;

      closeIcon = TestUtils.findRenderedDOMComponentWithClass(form, 'close-icon');

      TestUtils.Simulate.click(closeIcon);

      expect(form.closeModal).toBeCalled();

The test errors with Expected Function to be called. 调用预期函数的测试错误 and closeModal is not mocked but still runs (I have a console log in it at the moment). 并且closeModal没有被模拟但仍然运行(我现在有一个控制台日志)。 I've been on it all afternoon but haven't been able to figure it out. 我整个下午一直在这里,但一直都没能弄清楚。 Any help would be very appreciated. 任何帮助将非常感激。

Thanks to some of the suggestions from Phil, I finally figured it out. 感谢菲尔的一些建议,我终于明白了。 What I want to test is that closeModal is called when I click the icon. 我想测试的是当我点击图标时调用closeModal。 I have already tested that the parent component behaves as expected but I couldn't work out how to mock closeModal because I'm testing this specific component and by default, this is the only one Jest doesn't mock for me. 我已经测试过,父组件的行为与预期的一样,但我无法弄清楚如何模拟closeModal,因为我正在测试这个特定的组件,默认情况下,这是Jest不会为我模拟的唯一一个。 I could stub it manually but that somehow didn't want to work. 我可以手动存根,但不知何故不想工作。

What I've done now is mock this.props.onClick within closeModal and I check that it fires. 我现在所做的是模拟this.props.onClick在closeModal中,我检查它是否会触发。

Here's how that looks in code: 以下是代码中的外观:

describe('QuestionForm', function() {
  var onClickFunc,
      form;

  beforeEach(function() {
    onClickFunc = jest.genMockFunction();

    form = TestUtils.renderIntoDocument(
      <QuestionForm onClick={onClickFunc} />
    );
  });

  it('should call closeModal when the icon is clicked', function() {
    var closeIcon = TestUtils.findRenderedDOMComponentWithClass(form, 'close-icon');

    TestUtils.Simulate.click(closeIcon);

    expect(onClickFunc).toBeCalled();
  });

});

I think that sufficiently tests that closeModal behaves as expected. 我认为这足以测试closeModal的行为与预期一致。

If you want to check that the function is called you'd want to use jest's toBeCalled function (or toBeCalledWith ). 如果要检查函数是否被调用,则需要使用jest的toBeCalled函数(或toBeCalledWith )。 Assuming you've done some set up to instantiate the components, renderIntoDocument, and simulate the click (checkout the tutorial if not) 假设你已经做了一些设置来实例化组件,renderIntoDocument,并模拟点击(如果没有,请查看教程

describe('#closeModal', function(){
  beforeEach(function(){
    // setup in here to instantiate / render component 
    // and simulate the click if the i tag
  })
  it('is called on click', function(){
    expect(questionModal.closeModal).toBeCalled()
  })
})

EDIT: Ok, so after tinkering with it, I was able to get a passing test doing something similar to your original structure. 编辑:好的,所以在修补它之后,我能够通过测试做一些类似于原始结构的测试。 I created a mock function, but instead of doing form.closeModal = mock, I passed the mock into the Question as the onClick prop, and checked if it got called. 我创建了一个模拟函数,但不是做form.closeModal = mock,而是将模拟作为onClick prop传递给Question,并检查它是否被调用。

describe('#closeModal', function(){
  var mock, form, closeIcon;

  beforeEach(function(){
    mock = jest.genMockFunction();
    form = TestUtils.renderIntoDocument(
      <QuestionForm onClick={ mock } />
    );
    closeIcon = TestUtils.findRenderedDOMComponentWithClass(form, 'close-icon');
    TestUtils.Simulate.click(closeIcon);
  })

  it('is called on click', function(){
    expect(mock).toBeCalled()
  })
})

You could use an asynchronous test. 您可以使用异步测试。 You have to call done() in your onClick handler. 您必须在onClick处理程序中调用done() If everything is fine the handler will call done() and the test passes. 如果一切正常,处理程序将调用done()并且测试通过。 If the handler won't be called the test fails after some time because Jasmine cannot end it. 如果处理程序不会被调用,测试会在一段时间后失败,因为Jasmine无法结束它。

No need for stubbing or mocking. 无需抄写或嘲笑。

it('should click', function(done) {
  function onClick() {
    done();
  }
  var instance = TestUtils.renderIntoDocument(
    <Component onClick={onClick} />
  );
  var button = TestUtils.findRenderedDOMComponentWithTag(instance, 'button');
  var buttonDOM = React.findDOMNode(button);
  React.addons.TestUtils.Simulate.click(buttonDOM);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “onClick = { () =&gt; this.props.onClick() } 和 onClick = {props.onClick } 有什么区别? - What is the difference between "onClick = { () => this.props.onClick() } and onClick = {props.onClick }? 当没有使用测试库 react 和 jest 传递给它的道具时,将调用按钮的测试 onClick - Test onClick of a button is called when there is no props passed down to it using testing library react and jest 如何使用反应测试库测试使用 props.history 的 onClick 按钮? - How can I test a button which uses props.history for it's onClick with react testing library? React Jest 测试按钮 onClick - React Jest test button onClick 如何使用Jest测试React组件的onClick行? - How can I test an onClick line of a React component using Jest? 在 Svelte 中访问组件的道具以进行 Jest 测试 - Access component 's props in Svelte for Jest test 使用redux操作创建者调用onClick()时应测试什么? - What should be tested when calling onClick() with a redux action creator? 我应该在反应 typescript 中将什么类型传递给我的 imageIcon 道具? - What type i should pass to my imageIcon props in react typescript? 如何使用Jest + Enzyme测试React组件的样式? - How can I test React component's style with Jest + Enzyme? 如何使用 onClick 函数 Jest/React 测试库测试 React 组件 - How to test react component with onClick function Jest/React testing library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM