简体   繁体   English

如何使用jest在redux中模拟异步动作创建者

[英]How to mock an async action creator in redux with jest

I'm trying to write a unit test for a redux async action creator using jest. 我正在尝试使用jest为redux异步动作创建器编写单元测试。

asyncActions.js: asyncActions.js:

const startSignInRequest = () => ({
  type: START_SIGNIN_REQUEST
});

// action creator to dispatch the success of sign In
export const signInSucceded = user => ({
  type: SIGNIN_USER_SUCCEEDED,
  user
});

// action creator to dispatch the failure of the signIn request
export const signInFailed = error => ({
  type: SIGNIN_USER_FAILED,
  error
});

const signInUser = user => dispatch => {
dispatch(startSignInRequest);
  return signInApi(user).then(
    response => {
      const { username, token } = response.data;
      dispatch(signInSucceded(username));
      localStorage.setItem("token", token);
      history.push("/homepage");
    },
    error => {
      let errorMessage = "Internal Server Error";
      if (error.response) {
        errorMessage = error.response.data;
      }
      dispatch(signInFailed(errorMessage));
      dispatch(errorAlert(errorMessage));
    }
  );
};

signInApi.js: signInApi.js:

import axios from "axios";
import { url } from "../../env/config";

const signInApi = async user => {
  const fetchedUser = await axios.post(`${url}/signIn`, {
    email: user.email,
    password: user.password
  });
  return fetchedUser;
};

In the Writing tests of redux's official documentation, they use fetch-mock library. 在redux官方文档的Writing测试中,他们使用fetch-mock库。 However, I think that this library call the real Api. 但是,我认为这个库叫真正的Api。 I tried to mock the axios api using jest mocks . 我试图用jest mocks模拟嘲笑axios api。

/__mocks/signInApi.js: /__mocks/signInApi.js:

const users = [
{
    login: 'user 1',
    password: 'password'
}
];

  export default function signInApi(user) {
    return new Promise((resolve, reject) => {
      const userFound = users.find(u => u.login === user.login);
      process.nextTick(() =>
        userFound
          ? resolve(userFound)
          // eslint-disable-next-line prefer-promise-reject-errors
          : reject({
              error: 'Invalid user credentials',
            }),
      );
    });
  }

__tests/asyncActions.js: __tests / asyncActions.js:

jest.mock('../axiosApis/signInApi');
import * as actions from '../actions/asyncActions';

describe('Async action creators', async () => {
it('Should create SIGN_IN_USER_SUCCEEDED when signIn user has been done', () => {
    const user = {
                    login: 'user 1',
                    password: 'password'
                }
    await expect(actions.signInUser(user)).resolves.toEqual({
        user
    })
})
});

The test failed and I got: 测试失败了,我得到了:

expect(received).resolves.toEqual()

Matcher error: received value must be a promise

Received has type:  function
Received has value: [Function anonymous]

How can I mock this async action creator only with jest? 如何只用jest模拟这个异步动作创建者?

EDIT: I have to edit my answer as the first one pointed to a wrong direction. 编辑:我必须编辑我的答案,因为第一个指向错误的方向。

So from my understanding you want to mock the Action + Return value. 所以根据我的理解,你想要模拟Action + Return值。 In your case I would just immediately return the result of your mock function. 在你的情况下,我会立即返回你的模拟函数的结果。 As you're not mocking axios.post you don't need to wrap everything inside a promise and return that. 因为你没有嘲笑axios.post你不需要将所有内容包装在一个promise中并返回它。 You're not mocking only the HTTP call but the whole action. 你不是只嘲笑HTTP调用而是嘲笑整个行动。

const users = [
{
    login: 'user 1',
    password: 'password'
}
];

  export default function signInApi(user) {
    const userFound = users.find(u => u.login === user.login);
    return (userFound ? userFound : {
      error: 'Invalid user'
    });
  }

Looks like you need to update your mock to resolve to an object like this: 看起来您需要更新您的模拟以解析为这样的对象:

export default function signInApi(user) {
  return new Promise((resolve, reject) => {
    const userFound = users.find(u => u.login === user.login);
    process.nextTick(() =>
      userFound
        ? resolve({  // <= resolve to an object
          data: {
            username: 'the username',
            token: 'the token'
          }
        })
        // eslint-disable-next-line prefer-promise-reject-errors
        : reject({
          error: 'Invalid user credentials',
        }),
    );
  });
}

...then what you are really testing is that actions.signInUser returns a function which can be called with a user ... ...那么你真正测试的是actions.signInUser返回一个可以用user调用的函数 ...

...which then returns another function which can be called with a dispatch which dispatches the proper actions: ...然后返回另一个函数 ,该函数可以使用dispatch来调用,该dispatch调度正确的操作:

jest.mock('./signInApi');
import * as actions from './asyncActions';

describe('Async action creators', () => {
  it('Should create SIGN_IN_USER_SUCCEEDED when signIn user has been done', async () => {
    const user = {
      login: 'user 1',
      password: 'password'
    };
    const dispatch = jest.fn();
    await actions.signInUser(user)(dispatch);  // <= call the function on a user, then call the resulting function on a dispatch
    expect(dispatch).toHaveBeenCalledTimes(2);  // Success!
    expect(dispatch).toHaveBeenNthCalledWith(1, { type: START_SIGNIN_REQUEST });  // Success!
    expect(dispatch).toHaveBeenNthCalledWith(2, { type: SIGNIN_USER_SUCCEEDED, user: 'the username' });  // Success!
  })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM