0

I wrote a snapshot test for a screen called NewUser.js, here is the test code:

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();
  });
});

And here is the 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>
    );
}
}

I got the error when I run the test. What should I do to make test runnable? Thanks.

skyboyer
  • 22,209
  • 7
  • 57
  • 64

2 Answers2

0

I somehow solved it by doing this

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

I am still not sure the reason caused that issue, and the solution is not perfect, but it saved my code.

Machavity
  • 30,841
  • 27
  • 92
  • 100
0

enter image description here

Add mock function for navigate like const navigate = jest.fn(); "YourComponent navigation={{navigate}} "

pass navigation props in your component like