how to use wrapper.find to find the text of div' div element

Viewed 925

I am trying to write enzyme test and would like to access the text of the following div.

<div className="toolbar__contentInformation">
   <div className="text smallfont ellipsis">Alex</div>
   <div className="text smallfont ellipsis">12</div>
</div>

test.js

  let component = React.createElement(App});
  let wrapper = enzyme.enzyme.mount(component);
  let val = wrapper.find('div.toolbar__contentInformation') //how to access text = Alex ???
1 Answers

There are a couple of ways to achieve your goal.

let val = wrapper.find('div.toolbar__contentInformation').chidlren();

expect(wrapper.find('div.toolbar__contentInformation').childAt(0).text()).to.equal('Alex');
expect(wrapper.find('div.toolbar__contentInformation').childAt(1).text()).to.equal('12');

Or you can refer directly to children using this function at():

expect(wrapper.find('.text.smallfont.ellipsis').at(0).text()).to.equal('Alex');
expect(wrapper.find('.text.smallfont.ellipsis').at(1).text()).to.equal('12');

Or use function first and last()

expect(wrapper.find('.text.smallfont.ellipsis').first().text()).to.equal('Alex');
expect(wrapper.find('.text.smallfont.ellipsis').last().text()).to.equal('12');

Alternatively, you can simply refer to the:

wrapper.find('div.toolbar__contentInformation')[index]

Related