TouchableNativeFeedback

TouchableNativeFeedback

测试平台之间不同的组件时,我执行2个快照测试(iOS和Android)。在针对Android进行测试时,在执行其余测试之前,我首先将Platform.OS设置为"android"。当要测试的组件依赖于Android模块(例如TouchableNativeFeedback)时,生成的快照为错误消息:

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


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

最佳答案

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

这是__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');

关于javascript - 使用平台特定模块的组件的Jest快照,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45063005/

10-10 00:33