gpt4 book ai didi

react-native - 类型错误 : Cannot read property 'params' of undefined Jest Testing - React Native

转载 作者:行者123 更新时间:2023-12-04 13:09:54 25 4
gpt4 key购买 nike

我已经创建了一个测试文件来测试我的以下“BiddingScreen”,

const BiddingScreen = ({ route, navigation }) => {

const { currentBid } = route.params;


return (
<SafeAreaView style={styles.main}>
<Header title="BID Here" navigation={navigation} />
<View style={styles.container}>
<Text style={styles.currentBid}>{CurrentBid}$</Text>
</View>
</SafeAreaView>
)}

我的测试文件如下,

import React from 'react';
import renderer from 'react-test-renderer';
import BiddingScreen from "../../../src/containers/biddingScreen/BiddingScreen";

jest.mock('@react-native-firebase/auth');

jest.mock("react-native/Libraries/EventEmitter/NativeEventEmitter");


test('renders correctly', () => {
const tree = renderer.create(< BiddingScreen />).toJSON();
expect(tree).toMatchSnapshot();
});

当我运行我的测试文件时,我得到了这个错误,

TypeError: Cannot read property 'params' of undefined

谁能帮我解决这个问题,谢谢

最佳答案

只要您的组件 Prop 没有默认值,您就需要模拟它们,例如:

describe('init', () => {
test('renders correctly', () => {
const mockedParams = {
route: { params: { currentBid: 'whatever-id' } },
navigation: ''
};

const tree = renderer.create(<BiddingScreen {...mockedParams} />).toJSON();
expect(tree).toMatchSnapshot();
});
});

关于react-native - 类型错误 : Cannot read property 'params' of undefined Jest Testing - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66563806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com