开玩笑地检查是否存在elements属性

Ath*_*ffy 1 chai reactjs jestjs enzyme

我想用玩笑来检查以下svg路径元素是否包含该属性d
<path id="TrendLine" fill="none" stroke="black" d="M170,28.76363636363638C170,28.76363636363638,221.46221083573664,189.150059910"></path> 如何使用玩笑来搜索元素中的特定属性?

Bil*_*lly 6

您可以使用酶的浅化方法来渲染组件,然后检查path元素上的道具:

// at the top of your test file file:
import { shallow } from 'enzyme';

...

it('should render path element with the expected d attribute', () => {
  // shallowly render your component:
  const wrapper = shallow(<Component />);

  // find the path element using a css selector
  const trendline = wrapper.find('path#TrendLine');

  // make assertion
  expect(trendline.props()).toHaveProperty('d', 'M170,28.76363636363638C170,28.76363636363638,221.46221083573664,189.150059910');
});
Run Code Online (Sandbox Code Playgroud)