【发布时间】:2021-07-11 13:21:51
【问题描述】:
我有一个这样的功能组件:
const ImageScreen = (props: any) => {
const images: object[] = [];
props.navigation.state.params.images.forEach((image: any) =>
//some code
);
return (
<View style={CommonStyles.normalPage}>
// some code
</View>
);
};
然后我写Jest来测试这个组件:
test(`renders correctly`, () => {
const props = {
navigation: {
state: {
params: {
images: [
{ url: "../sample-images/sample1.jpg" },
{ url: "../sample-images/sample2.jpg" },
],
index: 1,
},
},
},
};
const component = renderer.create(<ImageScreen props={props} />);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
但我总是报错:TypeError: Cannot read property 'state' of undefined
我对 Jest 和 React Native 都是新手,所以不确定我是否做错了
【问题讨论】:
标签: javascript reactjs react-native jestjs