使用Promise和Fetch进行功能测试

时间:2019-04-16 16:26:37

标签: reactjs mocking jestjs

尝试使用Jest和Enzyme进行功能测试。没有在任何onClick或onChange事件内部调用该函数

这里是功能:

    patchSourceEntity = () => {
return new Promise((resolve, reject) => {
  let SourceEntityBody = { 
    FirmID: this.props.FirmId,
    Name: this.state.Name,
    ShortName: this.state.ShortName,
    SourceEntityTypeID: this.state.SourceEntityTypeID ? this.state.SourceEntityTypeID.value : null
  }

  fetch(`${this.props.services.MasterDB.URL}/SOURCE_ENTITY?id=${this.props.entityToEdit}`, {
    headers: {          
      'Content-Type': 'application/json',
      'Ocp-Apim-Subscription-Key': this.props.services.MasterDB.subscription_key,
      "DDHFirm": this.props.user.DDHFirm,
      'x-universal-firm': this.props.FirmId,
    },
    method: 'PATCH',
    credentials: 'include',
    body: JSON.stringify(SourceEntityBody),
    }).then(
      res => {return(validationManager.ResolveHTTPResponse(res, 'Request Successful', 'Request Failed', false))
    }).then(response => {
      if(!response.error) {
        resolve();
      } else {
        reject();
      }        
    })

目前这是我的测试:不确定我书写是否正确

it('Test patchSourceEntity method',() => {
 let SourceEntityBody = {
    Name: "",
    ShortName: "",
    SourceEntityTypeID: {value:"valuetesttwo"
  },}
 wrapper.find('SourceEntityForm').setState({
    SourceEntityTypeID:{
    label:'labeltesttwo',
    value:"valuetesttwo"
   },
  })
 wrapper.update();
 expect(wrapper.find('SourceEntityForm').instance().patchSourceEntity = jest.fn( () => {
return new Promise((resolve, reject) => {
 resolve()
  }) })).toEqual(resolve());
  });

目标是能够为调用fetch和Promise的函数编写测试

0 个答案:

没有答案