samedi 28 juillet 2018

Mock imports to test function logic

Lets say I have the following file.

This file imports promiseFunction from a library and I'd like to test the doSomething() function.

In particular, assert the states when the promise resolves or fails

// file: MyComponent.js

import promiseFunction from 'somewhere`;

class MyClass extends Component {

  doSomething = () => {

    promiseFunction()
      .then((data) => {

        this.setState({...this.statename: data.name});

      }.catch(() => {

        this.setState({...this.state, error: 'error'});

      });

  }
}

How do I mock the promiseFunction thats being imported. Or really just any function that's being imported.

Aucun commentaire:

Enregistrer un commentaire