在测试平台之间不同的组件时,我执行 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/