我有以下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只返回空对象{}
答案 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/')
})