【问题标题】:React-redux: How to write an integration testReact-redux:如何编写集成测试
【发布时间】:2016-12-24 18:42:53
【问题描述】:

我正在使用 Enzyme 来测试我的 react 和 redux 部分。我已经阅读并发现为组件编写集成测试也是一个好习惯。 所以,这很简单,因为我必须调用动作创建者并根据商店检查他们的更新值,但我有一些返回 dispatch 动作的异步动作。

login.actions.js

export function loginUser(credentials) {
  return dispatch => {
    dispatch(action.loginRequest());
    return axios({
      method: 'post',
      url: `${api}/login`,
      data: {
        email: _.trim(_.get(credentials, 'email')),
        password: _.get(credentials, 'password')
      }
    })
      .then(response => {
        const { data } = response;

        if (_.isEqual(_.get(response, 'status'), 200)) {
          dispatch(action.loginSuccess(data));
        }
      })
      .catch(err => {
        dispatch(action.loginError(err));
      });
  };
} 

login.actionCreators.js

export function loginRequest() {
  return {
    type: types.LOGIN_REQUEST
  };
}
export function loginSuccess(payload) {
  return {
    type: types.LOGIN_SUCCESS,
    payload
  };
}
export function loginError(payload) {
  let error;
  switch (_.get(payload, 'response.status')) {
    case 422: {
      error = 'Invalid Credentials';
      break;
    }
    case 401: {
      error = 'Invalid user';
      break;
    }
    case 403: {
      error = 'Account not confirmed';
      break;
    }
    default:
      error = 'Something went wrong';
  }
  return {
    type: types.LOGIN_ERROR,
    error
  };
}

因此,为了执行完整的集成测试,我还必须测试login.actions.js,但由于调度通常返回普通对象,在我的情况下它们返回调度函数。如何测试它们?

【问题讨论】:

    标签: unit-testing reactjs integration-testing redux


    【解决方案1】:

    测试异步操作很简单。

    我正在使用 moxiossinon。您基本上可以将其扩展到所有不同的情况并以相同的模式进行测试

    import moxios from 'moxios';
    import configureMockStore from 'redux-mock-store';
    import thunk from 'redux-thunk';
    import { spy } from 'sinon';
    
    const middlewares = [thunk];
    const mockStore = configureMockStore(middlewares);
    
    describe('async actions', () => {
      beforeEach(() => {
        moxios.install();
      });
    
      afterEach(() => {
        moxios.uninstall();
      });
      it(`should create action LOGIN_SUCCESS after successful login`, () => {
        moxios.wait(() => {
          const request = moxios.requests.mostRecent();
          request.respondWith({
            status: 200,
            response: { message: 'success', status: '200' },
          });
        });
        const expectedActions = [
          { type: types.LOGIN_REQUEST },
          { type: types.LOGIN_SUCCESS, data: { message: 'success', status: '200' } },
        ];
        const store = mockStore({ auth: {} });
        return store.dispatch(loginUser('abcd', '1234'))
        .then(() => {
          expect(store.getActions()).to.eql(expectedActions);
        });
      });
    });
    

    【讨论】:

    • 如果按照相同的场景,我要测试商店中的状态是否更新?
    • 你在这里用商店测试,看这里使用的mockstore
    • 非常感谢!这确实是一个清晰且一流的解决方案。
    • stackoverflow.com/questions/39008033/… 这很相似,但我做错了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 2020-01-16
    • 1970-01-01
    相关资源
    最近更新 更多