how to check the actual DOM node using react enzyme

后端 未结 6 481
孤街浪徒
孤街浪徒 2020-12-20 11:29

Is there a way to get the actual DOM node so I can the query it with the Dom api as opposed to being required to use enzyme\'s api, it\'s just for edge cases where for examp

6条回答
  •  一向
    一向 (楼主)
    2020-12-20 12:10

    Perhaps you are looking for enzyme's instance()?

    const wrapper = mount()
    console.log(wrapper.instance().value); // 'hello'
    

    PS:

    instance() should give you a ReactComponent, from which you can use ReactDOM.findDOMNode(ReactComponent) to get a DOMNode. However, when I did that, like the following, it was the exact same object as wrapper.instance():

    import ReactDOM from 'react-dom'
    const wrapper = mount()
    console.log(ReactDOM.findDOMNode(wrapper.instance()) == wrapper.instance()) // true
    

    I don't understand why that is. If you console.log() either one of those, you'll see a HTMLInputElement, but it will contain lots of non-native DOM node looking stuff:

    HTMLInputElement {
      '__reactInternalInstance$yt1y6akr6yldi': 
       ReactDOMComponent {
         _currentElement: 
          { '$$typeof': Symbol(react.element),
            type: 'input',
            key: null,
            ref: null,
            props: [Object],
            _owner: [Object],
            _store: {} },
    

提交回复
热议问题