Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert a div in a React component's data attributes using Enzyme?

I have a React component that creates a <div> with custom data-attributes eg

<div className="video-wrapper" data-video-source="brightcove" />

Using Enzyme I can assert the classes like so

it('creates a div with correct oVideo attributes', () => {
    const videoDiv = component.find('div.video-wrapper')
    expect(videoDiv.hasClass('video-wrapper')).to.equal(true);
});

But how can I assert that the data-video-source attribute has the value brightcove?

like image 902
Simon Legg Avatar asked Aug 16 '16 10:08

Simon Legg


1 Answers

You can achieve this by using .prop() method:

it('creates a div with correct oVideo attributes', () => {
    const videoDiv = component.find('div.video-wrapper')
    expect(videoDiv.hasClass('video-wrapper')).to.equal(true);
    expect(videoDiv.prop('data-video-source')).to.equal('brightcove');
});
like image 147
1ven Avatar answered Oct 04 '22 21:10

1ven