【问题标题】:How to use document.getElementById() in React Test cases (JEST+ ENZYME)如何在 React 测试用例中使用 document.getElementById() (JEST+ ENZYME)
【发布时间】:2019-12-31 18:10:44
【问题描述】:

我有一份文件如下。

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

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

</body>
</html>

我在我的 React 应用程序中使用以下代码

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】:

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

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 2020-08-07
      • 1970-01-01
      • 2019-07-24
      • 2018-10-31
      • 2021-07-29
      • 2017-04-13
      • 2020-01-18
      • 2017-02-12
      相关资源
      最近更新 更多