【问题标题】:Action don't change the state on reducer test with jest操作不要用玩笑改变减速器测试的状态
【发布时间】:2020-01-11 14:20:50
【问题描述】:

我正在尝试测试我的 reducer,但是当我在测试中分派操作时,我的初始状态没有改变。

那是我的减速机:

    export default function login(state = INITIAL_STATE, action) {
  switch (action.type) {
    case Types.SET_ERROR:
      return {
        ...state,
        error: action.error,
      };
    case Types.SET_LOGIN:
      return {
        ...state,
        user: {email: action.user.email, password: action.user.password},
      };
    default:
      return state;
  }
}

这是我的测试:

it('should handle SET_ERROR', () => {
      const setErrorAction = {type: 'SET_ERROR', error: 'a'};
      expect(login(INITIAL_STATE, setErrorAction)).toEqual({
        ...INITIAL_STATE,
        error: 'a',
      });
});

这是我的测试结果:

 expect(received).toEqual(expected) // deep equality

    - Expected
    + Received

      Object {
    -   "error": "a",
    +   "error": "",
        "user": Object {
          "email": "",
          "password": "",
        },
      }

【问题讨论】:

  • 你确定Types.SET_ERROR === 'SET_ERROR' 吗?
  • 你是对的!感谢您帮助我,我没有注意到。
  • 欢迎使用解决此问题的确切解决方案来回答您的问题!

标签: reactjs react-native testing redux jestjs


【解决方案1】:

我没有意识到 Types.SET_ERROR === 'SET_ERROR' 不正确。

@segFault 帮助我意识到我的操作名称不同,因此我的操作没有被分派。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2021-12-10
    • 2021-02-10
    • 2020-07-31
    相关资源
    最近更新 更多