gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:23:01 25 4
gpt4 key购买 nike

我正在尝试测试呈现 iframe 并将标记直接注入(inject)该 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 运行上述代码。

最佳答案

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

ReactTestUtils.renderIntoDocument 为组件创建一个容器,但不再附加到 DOM,因此 contentWindow 对于 iframe 将为空 元素。如果您阅读了 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 的文档中没有提到这一点。

关于javascript - 为什么使用 ReactTestUtils 将 iframe 渲染到 jsdom 中不提供 contentWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36024664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com