简体   繁体   English

如何在 React Native 中测试图像源

[英]How to test image source in react native

my test result is number 1 instead of the name of the icon.我的测试结果是数字 1 而不是图标的名称。 Do you know how can I test the image source?你知道我怎样才能测试图像源吗?

Code:代码:

<Image
    source={buttonVisibility ? arrowRight : arrowDown}
    style={{ width: 20, height: 20 }}
    testID="image-source"
/>       

My test:我的测试:

  it('icon should be arrowRight', () => {
    const wrapper = shallow(<DetailsPageLastPosition bike={bike} buttonVisibility={true} />);
    expect(wrapper.find({ testID: 'image-source' }).props().source).toEqual('arrowRight');
  });

Test result:测试结果:

● DetailsPageLastPosition component › icon should be arrowRight

   expect(received).toEqual(expected) // deep equality

   Expected: "arrowRight"
   Received: 1

      73 |   it('icon should be arrowRight', () => {
      74 |     const wrapper = shallow(<DetailsPageLastPosition bike={bike} buttonVisibility={true} />);
    > 75 |     expect(wrapper.find({ testID: 'image-source' }).props().source).toEqual('arrowRight');
         |                                                                     ^
      76 |   });
      77 | });
      78 |

Local image assets always resolve to a number ( see here in the docs ).本地图像资产始终解析为数字(请参阅文档中的此处)。 One easy way around this would be to dynamically assign a test ID based on your conditional.解决此问题的一种简单方法是根据您的条件动态分配测试 ID。

<Image
    source={buttonVisibility ? arrowRight : arrowDown}
    style={{ width: 20, height: 20 }}
    testID={`image-source-${buttonVisibilty ? 'arrowRight' : 'arrowDown'}`}
/> 

// in test

expect(wrapper.find({ testID: 'image-source-arrowRight' }) // etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM