jeudi 12 décembre 2019

mocha/chai how to test that a method is called inside a then or catch callback

Im trying to test something like this:

// component.js
import React from 'react';
import AsyncModule from '../some/async/module';
import { doSomething } from '../myModule';

.
.
.

const onSubmit = () => {
  const opts = {/* some config */};

  AsyncModule(opts).someAsyncCall().then(resolved => {
    doSomething();
  }).catch(e => {
    // also
    doSomething();
  });
}

class MyCompontent extends React.Component {

  render() {
    return <Formik onSubmit={onSubmit} ... />;
  }

}

I need to test that doSomething is being called. So far, I have some tests written, and i've tried some things but none have worked. I need your help with this:

.
.
.
  it.only('should doSomething on Formik submit when AsyncModule.someAsyncCall promise resolves', () => {
    spyDoSomething = spy(myModule, 'doSomething');
    const expectedResponse = {};
    const stubSomeAsyncCall = stub(AsyncModule, 'someAsyncCall');
    stubSomeAsyncCall.resolves(expectedResponse);
    wrapper = mount(<MyCompontent />);
    const formik = wrapper.find('Formik');

    formik.simulate('submit');

    return expect(spyDoSomething).to.has.been.called;
  });

this test obviously doesn't pass. How can I check that doSomething is being called in the body of the then and catch callbacks?

Im running node 9.1.0, mocha, chai, enzyme and sinon.

Aucun commentaire:

Enregistrer un commentaire