1

我找到了一个将文本转换为 HTML 的 JavaScript 函数。这是功能

export default function ToText(node) {
  let tag = document.createElement("div");
  tag.innerHTML = node;
  node = tag.innerText;
  return node;
} 

我试图测试该功能,但它无法正常工作。这是我的测试脚本

import ToText from '../ToText';

it('check whether ToText function is working or not', () => {
  const a = "<P>This is a mock test for this function.</P>";
  const b = `This is a mock test for this function`;

  expect(ToText(a)).toBe(b);
});

不工作的原因可能是什么,请帮忙?

4

3 回答 3

0

变量 a 在字符串末尾有一个点 (.)。变量 b 没有它。

于 2019-12-12T13:19:13.890 回答
0

这样做可能会解决

node= tag.firstChild.innerText
于 2019-12-12T14:59:54.883 回答
0

It should work with the plain text and not with the HTML. The ToText function is returning the innerText. If you want to change the changes you did then the ToText function needs to be changed.

import ToText from '../ToText';

it('check whether ToText function is working or not', () => {
  const a = "This is a mock test for this function";

  expect(ToText(a)).toBe(a);
});
于 2019-12-14T12:30:16.753 回答