使用特定于平台的模块为组件创建快照

时间:2017-07-12 16:20:59

标签: javascript android react-native jestjs

在测试不同平台的组件时,我会执行2次快照测试(iOS和Android)。在测试Android时,我首先将Platform.OS设置为"android",然后再执行其余测试。当被测试的组件依赖于Android模块(如TouchableNativeFeedback)时,生成的快照是一条错误消息:

<View ...>
  <Text ...>
    TouchableNativeFeadback is not supported on this platform!
  </Text>
</View>

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:0)

我发现的唯一解决方案是模拟它。

__mocks__/react-native.js文件夹中,模拟结果如下:

export * from 'react-native';

export const TouchableNativeFeedback = (touchableNativeFeedback) => {
  const { children, ...rest } = touchableNativeFeedback;
  const combinedComponent = Object.assign(rest, children);
  return combinedComponent;
};
TouchableNativeFeedback.SelectableBackground = jest.fn(
  () => 'SelectableBackground'
);
TouchableNativeFeedback.SelectableBackgroundBorderless = jest.fn(
  () => 'SelectableBackgroundBorderless'
);
TouchableNativeFeedback.Ripple = jest.fn(() => 'Ripple');