简体   繁体   English

如何使用 wrapper.find 查找 div' div 元素的文本

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

I am trying to write enzyme test and would like to access the text of the following div.我正在尝试编写酶测试并想访问以下 div 的文本。

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

test.js测试.js

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

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() :或者您可以使用此 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()或者使用 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM