如何在React测试用例(JEST + ENZYME)中使用document.getElementById()

问题描述 投票:0回答:1

我有如下文件。

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

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

</body>
</html>

我正在我的React App中使用以下代码

const fieldElement = document.getElementById(fieldError);

现在如何在我的React测试用例场景中测试此特定行

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

有人可以对此提供一些启示。

javascript reactjs react-native jestjs enzyme
1个回答
0
投票

[使用.createRef()获取对DOM元素的引用:https://reactjs.org/docs/refs-and-the-dom.html

© www.soinside.com 2019 - 2024. All rights reserved.