TypeError:无法读取未定义的属性“ navigate”

时间:2019-10-07 04:49:30

标签: react-native jestjs expo

我为名为NewUser.js的屏幕编写了一个快照测试,这是测试代码:

import React from "react";
import NewUser from "../app/screens/NewUser/NewUser"
import renderer from "react-test-renderer"

describe("<NewUser/>", ()=>{
  it("snapshot", ()=>{
    expect(renderer.create(<NewUser/>).toJSON()).toMatchSnapshot();
  });
});

这是NewUser.js

import React, { Component } from 'react';
import { StyleSheet, Text, View, Button} from 'react-native';

import styles from './Styles'

export default class NewUser extends Component {
   static navigationOptions = () => {
    return {
        headerStyle: styles.headerStyle,
        headerTintColor: '#fff',
        headerTitle: 'JetSet',
        headerTitleStyle: {
            flex: 1,
        }
    };
};

render() {

    const { navigate } = this.props.navigation;

    return (
        <View style={styles.background}>
            <View style={styles.container}>
                <Button
                    onPress={() => navigate('Survey', { survey: 'NewUserQuestions' })}
                    title="New User"
                />
            </View>
        </View>
    );
}
}

运行测试时出现错误。我应该怎么做才能使测试可运行?谢谢。

1 个答案:

答案 0 :(得分:0)

我通过某种方式解决了这个问题

{ navigation } = this.props
...navigation.navigate()

我仍然不确定导致该问题的原因,解决方案并不完美,但是它保存了我的代码。