mercredi 24 janvier 2018

How to test a window.confirm method inside of a React component using enzyme?

I have a block button that uses window.confirm to check if someone really wants to block something. In my test, after aButton.simulate('click'); I want to check if the confirmation was canceled. I tried wrapping the check in an if block:

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

but enzyme doesn't like that. Is there a way I can check if a window.confirm has been canceled in my tests?

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);
      });
    }
  };

Aucun commentaire:

Enregistrer un commentaire