Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何模拟webview或iframe等html元素?_Javascript_Reactjs_Jestjs - Fatal编程技术网

Javascript 如何模拟webview或iframe等html元素?

Javascript 如何模拟webview或iframe等html元素?,javascript,reactjs,jestjs,Javascript,Reactjs,Jestjs,我试图为呈现的React组件编写一些带有Jest的单元测试。 Electron的webview标签基于Chromium的webview,这是一种iframe 每次运行测试时,当代码使用事件侦听器方法时,我都会出错。下面是我的代码: const TestComponent = (source, iframeError) => { const webviewTag = document.querySelector('webview'); webviewTag.addEventListe

我试图为呈现
的React组件编写一些带有Jest的单元测试。 Electron的webview标签基于Chromium的webview,这是一种iframe

每次运行测试时,当代码使用事件侦听器方法时,我都会出错。下面是我的代码:

const TestComponent = (source, iframeError) => {
  const webviewTag = document.querySelector('webview');
  webviewTag.addEventListener('plugin-crashed', (name, version) => {
    console.log(`plugin-crashed: ${name}, ${version}`);
  });

  return (
    <div className="content-body">
      {iframeError
      ? <IframeError />
      : <webivew
          id="webview"
          name="webivew"
          src={source}
          autosize
          allowpopups
          {...optionalAttributes}
          plugins
      />
    }
    </div>
  );
};

我如何开玩笑地嘲笑这种行为?

既然没有人给出答案,我就这么做。因为我找到了一个方法。 因此,可以在
jest
中模拟
global
对象,如下所示:

beforeEach(() => {
  jest.resetModules();
  global.document = {
    querySelector: function querySelector() {
      return webviewTag = {
        addEventListener: jest.fn(),
      };
    }
  };
});
让我知道,如果你可能会看到一些改进或其他方式来处理它。谢谢

beforeEach(() => {
  jest.resetModules();
  global.document = {
    querySelector: function querySelector() {
      return webviewTag = {
        addEventListener: jest.fn(),
      };
    }
  };
});