Javascript Jest测试:将嵌套对象传递给react组件
我有这样一个功能组件:Javascript Jest测试:将嵌套对象传递给react组件,javascript,reactjs,react-native,jestjs,Javascript,Reactjs,React Native,Jestjs,我有这样一个功能组件: const ImageScreen = (props: any) => { const images: object[] = []; props.navigation.state.params.images.forEach((image: any) => //some code ); return ( <View style={CommonStyles.normalPage}> // some code
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>
);
};
const ImageScreen=(道具:任意)=>{
常量图像:对象[]=[];
props.navigation.state.params.images.forEach((image:any)=>
//一些代码
);
返回(
//一些代码
);
};
然后我编写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();
});
test(`renders corrective`,()=>{
常量道具={
导航:{
声明:{
参数:{
图像:[
{url:“../sample images/sample1.jpg”},
{url:“../sample images/sample2.jpg”},
],
索引:1,
},
},
},
};
const component=renderer.create();
让tree=component.toJSON();
expect(tree.toMatchSnapshot();
});
但我总是遇到错误:TypeError:无法读取未定义的属性“state”
我对玩笑和React Native都是新手,所以不确定我是否做错了您已经将一个名为“props”的道具传递到组件中。这可以作为组件函数中的属性“props”访问 您想要做的是将所有(嵌套的)道具添加到组件中,而不是一个名为“道具”的道具 请尝试以下操作:
const component = renderer.create(<ImageScreen {...props} />);
const component=renderer.create();
我按照你的建议做了,但这次Jest给出了错误:“TypeError:success不是一个函数……getSize:Jest.fn((uri,success)=>process.nextTick(()=>success(320240)),”