mardi 28 avril 2020

In React, How to mock action creator(thunk) with an api endpoint ? (react-testing-library)

I'm using react-testing-library as a testing tool.

// Actions
import Api from '../Api';

const getA = () => {
  return (dispatch) => {
    dispatch(...);
    Api.requestA(..) <-- SDK function that calls an api endpoint
    dispatch(...);
  }  
}
class A extends React.Component {
  componentDidMount() {
    this.props.getA();
  }

  render() {
    ...
  }
}

if i call api directly not through SDK, i think i could mock the endpoint doing like

fetchMock.getOnce('/something', {
}

However, since what i am actually calling is a SDK function, I am not really sure how to mock this async action creator. Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire