dimanche 29 octobre 2017

Test function calls inside a resolved promise

export class SomeComponent extends Component {

  submitFunction() {
    this.props.functionCall1()
      .then(
        () => {
          this.props.functionCall2();
        },
        () => this.props.functionCall3()
      )
  }
}

I am trying to test that functionCall2 gets called when the promise is resolved, and test functionCall3 is called when the promise is rejected. I am using mocha, chai, enzyme, and sinon for testing. I have tried a lot, but could not get a working solution. That is what I have done so far:

it('calls functionCall1 when the form is submitted', () => {
  const stub = sinon.stub().returns(Promise.resolve());
  const props = {...someProps, functionCall1: stub};
  const container = new SomeComponent(props);
  container.submitFunction();

  expect(props.functionCall1).to.have.been.called();
  expect(props.functionCall2).to.have.been.called();
  expect(stub).to.have.been.called();
});

it('calls functionCall1 when the form is submitted', () => {
  const stub = sinon.stub().returns(Promise.reject());
  const props = {...someProps, functionCall1: stub};
  const container = new SomeComponent(props);
  container.submitFunction();

  expect(props.functionCall1).to.have.been.called();
  expect(props.functionCall3).to.have.been.called();
  expect(stub).to.have.been.called();
});

I appreciate any help.

Aucun commentaire:

Enregistrer un commentaire