enzymejs/enzyme

.ref on mounted component returns HTML-Element instead of ReactWrapper

Open

#1,459 建立於 2018年1月2日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)JavaScript (19,979 star) (2,016 fork)batch import
docshelp wantedv3 expected difference

描述

Current behavior

Imagine a component that renders a div with 3 anchor elements inside, each with their ref:

<div>
  <a ref="element1" onClick="..."></a>
  <a ref="element2" onClick="..."></a>
  <a ref="element3" onClick="..."></a>
</div>

And you want to simulate an onclick behavior in a jest test.

So I mount the component: var sut = mount(<Component>);

And then when I get the element wit hthe '.ref()' function, it does not return a ReactWrapper, but an HTMLAnchorElement, which does not have a .simulate or .prop?

However, the following does return a nice ReactWrapper and works as I wanted, but is not nice: sut.find('a').at(0).simulate('click');

My best guess is that I do something wrong, but please forgive me if it is something silly.

Expected behavior

As the documentation says, return a ReactWrapper so I can act on it?

Your environment

?

API

  • shallow
  • mount
  • render

Version

library version
Enzyme 3.3.0
React 16.2.0
Jest 16.2.0
enzyme-adapter-react-16 1.1.1

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

貢獻者指南