Reputation: 97
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
Reputation: 22323
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