0

我正在使用 jest 和 react-native 测试进行一些测试,问题是无法识别 TouchableOpacity 并导致错误

Card.test.js

import Card from "../Card"
import React from 'react';
import { TouchableOpacity } from 'react-native-gesture-handler';


const onePost = { title: "random photo", username: "martin", score: "1233", comments: "4322", creationDate: "6 hours ago",  url: "https://m.media-amazon.com/images/I/61FlX89mYGL." }

jest.mock('react-native-gesture-handler', () => {
    dropGestureHandler: jest.fn()
})
afterEach(() => {
    cleanup();

})

test('render title of the post correctly', () => {   
    const component = render(<Card {...onePost} />);
    expect(component.getByText(onePost.title)).not.toBeNull();
});


Card.js

 

    import React from "react";
    import { Text, Image, View, StyleSheet, Linking,Pressable } from "react-native";
    import timeDifference from "../lib/timeDifference";
    import { Touchable } from 'react-native-web';
    import { TouchableOpacity } from 'react-native-gesture-handler';
    
    export default function Card({ title, username, score, comments, creationDate, image, url }) {
        //we render the data from props
        return (
           
             <TouchableOpacity
                            onPress={() => { Linking.openURL('https://www.reddit.com' + url) }}
                            style={styles.card}  >
                    <View  style={{ flexDirection: "row" }}>
                        <View style={styles.cardImage}>
                            <Image
                                style={{ flex: 1 }}
                                source={{ uri: image }}
                            />
                        </View>
                        <View style={{ flex: 1, marginHorizontal: 12 }}>
                            <Text style={styles.cardLocation}>{timeDifference(creationDate)} ago</Text>
                            <Text 
                                style={styles.cardTitle}>{title}</Text>
                        
                        </View>
                    </View>
                    </TouchableOpacity>
            
        );
    }  

当我运行测试时,它显示“无法读取未定义的属性‘TouchableOpacity’”,当我更改 View 的 TouchableOpacity 时,它运行正常

4

0 回答 0