mercredi 24 avril 2019

how can I test a service call with promise in jest

I am trying to test a service call in jest

here is my code:

service.js

uploadSomething = filename => {
   return Api.post('/test')
     .then(response => {return response.data;})
     .catch(error => { return Promise.reject(error); }); 
};

myComponent.js

componentDidMount() {
    this.service = new Service();
  }

onUploadFile = event => {
    const resp = this.service.uploadSomething(event.target.href);
    resp.then(response => {
      console.log(response.data); 
    });
}

and my test:

const event = {
  target: {
      href: 'test',
  }
}
const wrapper = shallow(<myComponent />);
const myComponentInstance = wrapper.instance();
myComponentInstance.onUploadFile(event);

I try to add .then to myComponentInstance.onUploadFile(event), but it is failed, it returns

cannot read property 'then' of undefined, could anyone tell me how can I test this

Aucun commentaire:

Enregistrer un commentaire