我是react-native的新手,我试图在react-native项目中使用Jest编写单元测试用例。但是我得到的错误是fetch未定义,这里我正在使用fetch并在FlatList中显示响应。我将重定向到带有路由器参数的新屏幕名称“ Details”。我无法通过测试。请帮助我执行测试用例。
以下是我的组件:
class List extends Component {
constructor(props) {
super(props);
this.state = {
data: '',
};
}
componentDidMount = () => {
fetch(URL, {
method: 'GET',
})
.then(response => response.json())
.then(responseJson => {
this.setState({
data: responseJson,
});
})
.catch(error => {
console.error(error);
});
};
render() {
const { navigation } = this.props;
return (
<View style={styles.container}>
<FlatList
style={styles.list}
contentContainerStyle={styles.listContainer}
data={this.state.data}
keyExtractor={item => {
return item.id;
}}
renderItem={({ item }) => {
return (
<TouchableOpacity style={styles.card}>
<View style={styles.cardFooter}>
<View
<TouchableOpacity
style={styles.followButton}
onPress={() => {
/* 1. Navigate to the Details route with params */
navigation.navigate('Details', {
itemId: item.id,
});
}}>
<Text style={styles.followButtonText}>View</Text>
</TouchableOpacity>
</View>
</View>
</TouchableOpacity>
);
}}
/>
</View>
);
}
}
export default List;
List.test.js:
test('List Component Should be present', () => {
const snap = renderer.create(<List />).toJSON();
expect(snap).toMatchSnapshot()
})