3

我创建了一个测试文件来测试我的以下“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

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

4

1 回答 1

5

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

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

    const tree = renderer.create(<BiddingScreen {...mockedParams} />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
于 2021-03-10T17:52:13.430 回答