Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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/23.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 测试an的含量<;iframe>;使成分与酶发生反应_Javascript_Reactjs_Iframe_Sinon_Enzyme - Fatal编程技术网

Javascript 测试an的含量<;iframe>;使成分与酶发生反应

Javascript 测试an的含量<;iframe>;使成分与酶发生反应,javascript,reactjs,iframe,sinon,enzyme,Javascript,Reactjs,Iframe,Sinon,Enzyme,我编写了一个简单的React组件,它呈现一个: export class Iframe extends React.component { render() { return <iframe src={ this.props.src } />; } } 以及: constsrc='数据:text/html;charset=utf-8,神奇的独角兽'; 容器=安装(); 但在这两种情况下,在酶试验中: container = mount(<Ifr

我编写了一个简单的React组件,它呈现一个

export class Iframe extends React.component {
   render() {
        return <iframe src={ this.props.src } />;
    }
}
以及:

constsrc='数据:text/html;charset=utf-8,神奇的独角兽';
容器=安装();
但在这两种情况下,在酶试验中:

container = mount(<Iframe src='...' />);
container.instance().contentWindow // equals 'undefined'
container.find('iframe').contentWindow // equals 'undefined'
container=mount();
container.instance().contentWindow//等于“未定义”
container.find('iframe')。contentWindow//等于“未定义”

当提供有效的
src
属性时,该组件在浏览器上按预期工作和呈现。在React tests with Ezyme(或任何其他测试框架)中,是否有任何方法可以访问
contentWindow

如果您正在编写单元测试(我假设这就是您正在做的),您应该测试行为,而不是实现细节

如果我必须为这样的组件编写测试,我会像这样使用smth:

  • 正面场景:组件使用传递的
    src
  • 负面场景:如果没有
    src
    传入道具,组件将呈现null(取决于业务逻辑)
此测试同时处理正确和不正确的行为。有关更多详细信息,请参阅


如果我们讨论的是测试实现,我会使用and
toMatchSnapshot
方法来检查两种场景的渲染结果。

问题仍然存在。答案是jsdom只为附加的iframe提供contentWindow/contentDocument,默认情况下Ezyme不附加节点。有一个用于装载的选项,用于指示酶连接节点:

el = document.createElement('div');
document.body.appendChild(el);
wrapper = mount(<MyReactNode />, { attachTo: el });
el=document.createElement('div');
文件.正文.附件(el);
包装器=mount(,{attachTo:el});

我在测试时遇到了同样的问题,您找到解决方案了吗?您必须使用“工作”浏览器设置Ezyme。我不知道这是否可能。但是,为什么要测试这个呢?我认为这个测试毫无意义,因为您不会测试代码,而是会做出反应。我认为您应该测试prop“src”是否被正确地传递,因为iframe的“src”属性在getDOMNode()并检查属性contentWindow之后,enzyme.contentWindow实际上将为null而不是未定义。您检查的是酶包装器上的属性contentWindow,而不是iframe本身
container = mount(<Iframe src='...' />);
container.instance().contentWindow // equals 'undefined'
container.find('iframe').contentWindow // equals 'undefined'
el = document.createElement('div');
document.body.appendChild(el);
wrapper = mount(<MyReactNode />, { attachTo: el });