【问题标题】:Why isn´t mock from __mock__ folder invoked when running test for redux action?为什么在为 redux 操作运行测试时不调用来自 __mock__ 文件夹的模拟?
【发布时间】:2019-08-14 14:51:11
【问题描述】:

我在 redux 操作中调用 react-navigation NavigationService。 测试我需要模拟导航功能的操作。

/app/utils/NavigationService.js

import { NavigationActions } from 'react-navigation';

let navigator;

function setTopLevelNavigator(navigatorRef) {
  navigator = navigatorRef;
}

function navigate(routeName, params) {
  navigator.dispatch(NavigationActions.navigate({
    type: NavigationActions.NAVIGATE,
    routeName,
    params,
  }));
}

// add other navigation functions that you need and export them

export default {
  navigate,
  setTopLevelNavigator,
};

我在 NavigationService.js 文件旁边创建了一个 __mock__ 文件夹。

app/utils/__mocks__/NavigationService.js 已更新

const navigate = jest.fn();
const setTopLevelNavigator = jest.fn();

export default {
    navigate,
    setTopLevelNavigator,
};

为什么 jest 在运行测试时不自动模拟 navigate 函数? https://jestjs.io/docs/en/manual-mocks

__tests__/actions/AuthActions.test.js 已更新

jest.mock('../../app/utils/NavigationService'); //at the top directly behind other imports

it('should call firebase on signIn', () => {
    const user = {
      email: 'test@test.com',
      password: 'sign',
    };

    const expected = [
      { type: types.LOGIN_USER },
      { payload: 1, type: types.DB_VERSION },
      { payload: 'prod', type: types.USER_TYPE },
      { payload: { name: 'data' }, type: types.WEEKPLAN_FETCH_SUCCESS },
      { payload: { name: 'data' }, type: types.RECIPELIBRARY_FETCH_SUCCESS },
      {
        payload: { user: { name: 'user' }, userVersionAndType: { dbVersion: 1, userType: 'prod' } },
        type: types.LOGIN_USER_SUCCESS,
      },
    ];

    return store.dispatch(actions.loginUser(user)).then(() => {
      expect(store.getActions()).toEqual(expected);
    });
  });

app/actions/AuthActions.js

export const loginUser = ({ email, password }) => (dispatch) => {
  dispatch({ type: LOGIN_USER });
  return firebase
    .auth()
    .signInWithEmailAndPassword(email, password)
    .catch((signInError) => {
      dispatch({ type: CREATE_USER, payload: signInError.message });
      return firebase
        .auth()
        .createUserWithEmailAndPassword(email, password)
        .then(async (user) => {
          const userVersionAndType = await dispatch(initUser());
          await dispatch(initWeekplan(userVersionAndType));
          await dispatch(initRecipeLibrary(userVersionAndType));
          return user;
        });
    })
    .then(async (user) => {
      saveCredentials(email, password);
      const userVersionAndType = await dispatch(getUserVersionAndType());
      await dispatch(weekplanFetch(userVersionAndType));
      await dispatch(recipeLibraryFetch(userVersionAndType));
      dispatch(loginUserSuccess({ user, userVersionAndType }));
      NavigationService.navigate('Home');
    })
    .catch(error => dispatch(loginUserFail(error.message)));
};

【问题讨论】:

    标签: react-native mocking react-redux jestjs react-navigation


    【解决方案1】:

    您已经创建了一个manual mock for a user module

    为特定测试文件激活用户模块的手动模拟需要调用jest.mock

    对于这种特殊情况,将此行添加到 __tests__/actions/AuthActions.test.js 的顶部,模拟将用于该测试文件中的所有测试:

    jest.mock('../../app/utils/NavigationService');  // use the manual mock in this test file
    

    请注意,用户模块Node核心模块(如fspathutil等)都必须激活手动模拟通过调用jest.mock 来获取特定测试文件,并且此行为不同于自动应用于所有测试的节点模块的手动模拟。

    【讨论】:

    • 感谢您的回复。再次阅读文档,我预计它会起作用。不幸的是它没有。查看更新的AuthActions.test.js 我做错了吗?我得到的错误是Cannot read property 'dispatch' of undefined
    • 我发现了问题。 github.com/reach/router/issues/76#issuecomment-396862351 给了我缺失的部分
    • 我还得调整 NavigationService.js 看看更新
    猜你喜欢
    • 2021-12-28
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多