user3019647
user3019647

Reputation: 153

How to use document.getElementById() in React Test cases (JEST+ ENZYME)

I have a document as follows.

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p id="para">My first paragraph.</p>

</body>
</html>

I am using below code in my React App

const fieldElement = document.getElementById(fieldError);

Now how do i test this particular line in my React test case scenario

it("should provide fieldElement for the id", async () => {
  let fieldError = "para"
  // Need to write test for document.getElemetById here
  });

Can someone please shed some light on this.

Upvotes: 0

Views: 520

Answers (1)

Jason Y
Jason Y

Reputation: 1001

Look into using .createRef() for getting the reference to the DOM element: https://reactjs.org/docs/refs-and-the-dom.html

Upvotes: 1

Related Questions