Reactjs 如何使用Jest/Ezyme测试硬编码的图像onError属性?
我有以下代码:Reactjs 如何使用Jest/Ezyme测试硬编码的图像onError属性?,reactjs,jestjs,enzyme,Reactjs,Jestjs,Enzyme,我有以下代码: const Avatar = ({imageSrc, imageAlt, imageWidth, imageHeight}) => ( <img src={imageSrc} alt={imageAlt} style={{ width: imageWidth, height: imageHeight }} onError={e => { e.target.src = '/static/images/placeholder/avatar.p
const Avatar = ({imageSrc, imageAlt, imageWidth, imageHeight}) => (
<img
src={imageSrc}
alt={imageAlt}
style={{ width: imageWidth, height: imageHeight }}
onError={e => {
e.target.src = '/static/images/placeholder/avatar.png';
}}
/>)
这是我的组件的最简单版本,只是为了让你们知道这一点。我想用笑话和酶来测试这个onError,但是我找不到一个方法来模仿它,因为它不是道具的一部分
如何做到这一点?下面是一个单元测试解决方案: index.ts: 从“React”导入React; export const Avatar={imageSrc,imageAlt,imageWidth,imageHeight}=> { //@ts忽略 e、 target.src='/static/images/placeholder/avatar.png'; }} /> ; index.spec.ts: 从“React”导入React; 从“/”导入{Avatar}; 从“酶”导入{mount}; 描述“化身”,=>{ 它“应该处理错误”,=>{ 常数mProps={ imageSrc:'http://123.com/avatar.png', imageAlt:'alt', 图像宽度:123, 影像高度:456 }; 常量包装器=装入