【问题标题】:Jest - Redux action creator with React Native AnimatedJest - 使用 React Native Animated 的 Redux 动作创建者
【发布时间】:2017-09-17 19:48:40
【问题描述】:

我正在尝试使用 Jest 测试一个 redux 操作。在使用 React Native 模拟 Animated 库时,我遇到了一个问题。这是我的代码(精简):

// action.js
const loginAction = () => {
  Animated.timing(animation, {
    toValue: 0,
    duration: 600
  }).start(() => {
    dispatch({ type: 'LOGIN_SUCCESS' }); 
  });
}

// mock-setup.js
jest.mock('Animated', () => {
  return {
    timing: jest.fn(() => {
      return {
        start: jest.fn()
      };
    }),
    Value: jest.fn(() => {
      return {
        interpolate: jest.fn()
      };
    })
  };
});

在我的测试中,我试图比较分派的动作,但我似乎无法让我的测试进入start 函数的回调中。有什么想法吗?

【问题讨论】:

    标签: reactjs react-native jestjs


    【解决方案1】:

    找到我的答案!我的 Animated 模拟需要看起来像这样:

    jest.mock('Animated', () => {
      return {
        timing: jest.fn(() => {
          return {
            start: callback => callback()
          };
        }),
        Value: jest.fn(() => {
          return {
            interpolate: jest.fn()
          };
        })
      };
    });
    

    start 函数被传递了一个函数,所以我只需要为我的模拟版本提供一种运行该函数的方法。

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 2018-04-27
      • 2017-08-02
      • 2018-03-17
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多