How do I get an attribute of an element nested in a React component using Jest and/or Enzyme?

After some digging, I found the following. The indicated line in the question:

const src = img.node.props.src;

can be simplified to the following:

const src = img.prop('src');

The documentation can be found here.

If someone knows of a non-Enzyme way of doing this, I'd still be interested in hearing about it.


With React Test Utilities:

it('should have the logo image', () => 
  const app = TestUtils.renderIntoDocument(<App/>);
  var image = TestUtils.findRenderedDOMComponentWithTag(app, 'img');
  expect(image.getDOMNode().getAttribute('src')).toEqual('logo.svg');
});

Enzyme tests looks much cleaner.


For me, it worked as below

expect(companySelect.find('input').props()["disabled"]).toBe(true)

props() returns an object having all the attributes of the selector and then it can be browsed as an object.

Hope this helps too....

https://airbnb.io/enzyme/docs/api/ReactWrapper/props.html