简体   繁体   中英

How to query native html elements when testing a React application with Jest?

When I render React elements, for example, a Button, then an equivalent HTML button element is created in the DOM.

But, when I query the DOM I'm still getting a wrapped element.

HTMLButtonElement {
      '__reactInternalInstance$gt1haeiqrhv': FiberNode {
        tag: 5,
        key: null,
        ...
}

Why don't I get the native element as if I rendered a normal div using JSDOM? Is there a way to get it?

I suppose you have to query for getDOMNode :

const button = wrapper.find('button').getDOMNode();
// const button = wrapper.getDOMNode(); // <---if wrapper is button element

It will give you the lying dom element in the component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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