如何在JEST测试用例中显示回调API的成功和失败消息

时间:2018-12-31 11:26:24

标签: reactjs jestjs babeljs jest-fetch-mock

如何为回调API成功和失败编写JEST测试用例。 下面是我的代码

function userLogin(username, password) {
  const reqBody = { companyEmailAddress: username, password, };
  const url = `${config.apiBaseUrl}${serviceMethodConstants.login}`; 

  return (dispatch) => {
    dispatch(serviceFactory.postData(
      url,
      false,
      reqBody,
      function (response, dispatch) { 
        if (response !== undefined) {
          console.log(response )
        }
      }
    ));
  };
}

同样,我编写了JEST测试用例,但未按预期显示任何错误或成功消息。

it("Login User Success on API call ", async () => {      
        const dispatch = jest.fn();
        const postDataResult = {
            200: 'success',
            400: 'error'
        };
        let url = "https://api/users/authenticate";
        let reqBody = {
          companyEmailAddress: "abcd@hotmail.com",
          password: "pass"
        };
        jest.spyOn(serviceFactory, "postData").mockReturnValue(postDataResult);
        userActions.userLogin("abcd@hotmail.com", "pass")(dispatch);
        expect(serviceFactory.postData).toBeCalledWith(
          url,
          false,
          reqBody,
          expect.any(Function)
        );  

        expect(dispatch).toBeCalledWith(postDataResult);
});

0 个答案:

没有答案