如何使用酶测试React组件内的window.confirm方法?

时间:2018-01-24 13:09:21

标签: javascript reactjs testing enzyme confirm

我有一个阻止按钮,使用window.confirm检查某人是否真的要阻止某些内容。在我的测试中,在aButton.simulate('click');之后我想检查确认是否被取消。我试着在if块中包装支票:

  if (window.confirm == 'false') {
    expect(api.postRequest.calledOnce).to.equal(false);
  }

但酶不喜欢这样。有没有办法可以检查我的测试中是否取消了window.confirm

block_button_spec.js

it('DOES NOT call postRequest when Block is clicked, it is NOT disabled, and confirm is canceled', () => {
  wrapper.setState({ disabled: false });
  const aButton = wrapper.find('button');
  aButton.simulate('click');
  // code to check window.confirm was canceled
  expect(api.postRequest.calledOnce).to.equal(false);
});

block_button.js

  handleClick = () => {
    if (window.confirm(this.confirmBlockAll())) {
      this.disableButton();
      const promise = postRequest(this.props.blockPath);
      promise.then((err) => {
        const snackbarMessage = JSON.parse(err.response.text);
        this.openSnackbar(snackbarMessage);
      });
    }
  };

0 个答案:

没有答案