单元测试图像标签与图像对象

时间:2017-12-06 13:42:24

标签: reactjs enzyme

我有以下reactjs代码段:

import lionIcon from 'assets/images/lion.svg';
import dogIcon from 'assets/images/dog.svg';

<img
    className="animal-btn-img"
    src={isLion ? lionIcon : dogIcon}
  />

如何基于'isLion'道具进行单元测试,在img标签上设置适当的动物对象。?

let img = wrapper.find('.animal-btn-img');
console.log(img.prop('src'));

上面的console.log只返回空对象{}

1 个答案:

答案 0 :(得分:0)

shallow尝试enzyme,对我来说效果很好

import React from 'react'
import {shallow} from 'enzyme'
import Testor from '../components/Testor'

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });


it('should see image source', () => {
    const container = shallow(<Testor/>)
    expect(container.find('img').props().src).toEqual('http://google.com/')
})