Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest check if an elements attribute exists

I want to check with jest if the following svg path element contains the attribute d
<path id="TrendLine" fill="none" stroke="black" d="M170,28.76363636363638C170,28.76363636363638,221.46221083573664,189.150059910"></path> How do I use jest to search for specific attribute in an element?

like image 257
Athif Shaffy Avatar asked Dec 06 '22 11:12

Athif Shaffy


1 Answers

You can use enzyme's shallow method to render your component and then to check the props on the path element:

// 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');
});
like image 149
Billy Reilly Avatar answered Dec 08 '22 02:12

Billy Reilly