Arun
Arun

Reputation: 97

React Native Jest Unit test:Unable to execute testcase "cannot read property params of undefined'

i am trying to write unit test case using Jest in react-native project.But i am getting error cannot read property params of undefined,here i am receving itemId through route parameters from other component.Following is my Component:

Imgview.js:
    class Imgview extends Component {
      constructor(props) {
        super(props);
        this.state = {
          data: '',
        };
      }

      render() {
        const { itemId } = this.props.route.params;
        return (
          <View style={styles.container}>
              <Image style={styles.userImage} source={{ uri: itemId }} />
          </View>
        );
      }
    }
    Here is my test
    test('Imgview Component Should be present', () => {
      const snap = renderer.create(<Imgview />).toJSON();
      expect(snap).toMatchSnapshot()
    })

Upvotes: 0

Views: 475

Answers (1)

Joe Lloyd
Joe Lloyd

Reputation: 22323

Wrap your test component in a router or mock

Route is undefined so you need to add

renderer.create(<Imgview route={{params: 'some param'}} />).toJSON();

you're making your component with no props so it can't reference them in the test. They are just undefined.

Upvotes: 2

Related Questions