为什么使用 ReactTestUtils 将 iframe 渲染到 jsdom 中不提供 contentWindow?

Why does rendering an iframe into jsdom with ReactTestUtils not provide a contentWindow?

我正在尝试测试呈现 iframe 并将标记直接注入该 iframe 的 React 组件。 (我不是要在 iframe 中加载 URL。)

该组件实际上在浏览器中工作得很好,但到目前为止为它编写测试似乎是不可能的。我不知道为什么。

这是一个证明失败的简短测试用例。



    // Set up DOM
    var jsdom = require( 'jsdom' ).jsdom;
    global.document = jsdom( '' );
    global.window = document.defaultView;
    global.navigator = document.defaultView.navigator;

    // Prepare React
    var ReactTestUtils = require( 'react-addons-test-utils' );
    var React = require( 'react' );

    var Frame = React.createClass( {
      componentDidMount() {
        console.log( 'iframe loaded. adding content' );
        const iframe = this.refs.iframe;
        console.log( 'adding content to', iframe.contentWindow ); // Should not be null
        iframe.contentWindow.document.open();
        iframe.contentWindow.document.write( 'Hello World!' );
        iframe.contentWindow.document.close();
      },

      render() {
        console.log( 'rendering iframe' );
        return React.createElement( 'iframe', { ref: 'iframe' } );
      }
    } );

    // Render the component
    ReactTestUtils.renderIntoDocument( React.createElement( Frame ) );

为此,您需要安装以下 npm 包:jsdom、react、react-addons-test-utils。然后,您应该能够 运行 使用 node.

上面的代码

在 contentWindow 可用之前,您需要将 iframe 附加到您的文档。

检查此代码:

var iframe = document.createElement('iframe');
console.log(iframe.contentWindow);

document.body.appendChild(iframe);

console.log(iframe.contentWindow);

我非常彻底地测试了您的代码,最终发现问题是 ReactTestUtils.renderIntoDocument

ReactTestUtils.renderIntoDocument 为组件创建一个容器,但不再附加到 DOM,因此 contentWindowiframe 元素为空的原因.如果您阅读 ReactTestUtils 来源中的评论,您会注意到他们正在考虑重命名 renderIntoDocument,因为从技术上讲它不会! 解决方案,似乎是直接使用 ReactDOM 代替。

这是来自 ReactTestUtils 来源的一些代码:

var ReactTestUtils = {
  renderIntoDocument: function (instance) {
    var div = document.createElement('div');
    // None of our tests actually require attaching the container to the
    // DOM, and doing so creates a mess that we rely on test isolation to
    // clean up, so we're going to stop honoring the name of this method
    // (and probably rename it eventually) if no problems arise.
    // document.documentElement.appendChild(div);
    return ReactDOM.render(instance, div);
  },
 }

ReactDOM 与您的测试用例一起使用有效:

// Set up DOM
var jsdom = require( 'jsdom' ).jsdom;
global.document = jsdom( '' );
global.window = document.defaultView;
global.navigator = document.defaultView.navigator;

// Prepare React
var ReactTestUtils = require( 'react-addons-test-utils' );
var React = require( 'react' ); 
var ReactDOM = require('react-dom')

var Frame = React.createClass( {
  componentDidMount() {
    console.log( 'iframe loaded. adding content' );
    const iframe = this.refs.iframe;

    console.log( 'adding content to', iframe.contentWindow ); // Should not be null
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write( 'Hello World!' );
    iframe.contentWindow.document.close();

    // Should log "Hello World!"
    console.log(iframe.contentWindow.document.body.innerHTML);
  },

  render() {
    console.log( 'rendering iframe' );
    return React.createElement( 'iframe', { ref: 'iframe' } );
  }
} );

// Render the component

// NOPE 
//ReactTestUtils.renderIntoDocument( React.createElement( Frame ) );

// YUP
ReactDOM.render(React.createElement(Frame), document.body)

遗憾的是 ReactTestUtils 的文档中没有提到这一点。