【发布时间】:2020-12-18 23:31:34
【问题描述】:
我创建了一个卡片组件并为此编写了测试用例,但在查看测试覆盖率时,我发现该组件的分支覆盖率为 50%。而在测试用例中缺少的部分是onPress函数中else部分的测试。
第一季度。如何测试这个缺失的部分并增加我的覆盖率?
第二季度。如何单独测试 Card 组件的 onPress 功能?
const Card = (props) => {
const onPress = () => {
if (props.onPress) props.onPress();
};
return (<TouchableWithoutFeedback onPress={onPress}>
<View style={[Style.body, { width: props.width, height: props.height }]}>
<ImageBackground source={{ uri: props.imageUrl }} style={{ width: '100%', height: '100%' }} />
</View>
</TouchableWithoutFeedback>);
};
export default Card;
【问题讨论】:
-
写一个测试,其中
props.onPress是假的。如果您想对onPress进行单元测试,请将其移至顶层并使用组合将 deps 馈入其中。 -
我没听懂你的意思。你能给我示例答案或链接吗?
标签: javascript reactjs react-native jestjs react-test-renderer