TypeError: Cannot read property 'params' of undefined Jest Testing - React Native
TypeError: Cannot read property 'params' of undefined Jest Testing - React Native
我创建了一个测试文件来测试我关注的“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
所以谁能帮我解决这个问题,谢谢
只要你的组件道具没有默认值,你就需要模拟它们,比如:
describe('init', () => {
test('renders correctly', () => {
const mockedParams = {
route: { params: { currentBid: 'whatever-id' } },
navigation: ''
};
const tree = renderer.create(<BiddingScreen {...mockedParams} />).toJSON();
expect(tree).toMatchSnapshot();
});
});
我创建了一个测试文件来测试我关注的“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
所以谁能帮我解决这个问题,谢谢
只要你的组件道具没有默认值,你就需要模拟它们,比如:
describe('init', () => {
test('renders correctly', () => {
const mockedParams = {
route: { params: { currentBid: 'whatever-id' } },
navigation: ''
};
const tree = renderer.create(<BiddingScreen {...mockedParams} />).toJSON();
expect(tree).toMatchSnapshot();
});
});