Jest检查元素属性是否存在

时间:2018-05-05 07:09:50

标签: reactjs jestjs chai enzyme

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

1 个答案:

答案 0 :(得分:6)

您可以使用enzyme's shallow method渲染组件,然后检查路径元素上的道具:

// 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');
});