编程语言
首页 > 编程语言> > 如何检查JavaScript对象是否是新的DOM元素?

如何检查JavaScript对象是否是新的DOM元素?

作者:互联网

我有一个功能

mutationToDom: function() {
    var container = document.createElement( 'mutation' );

    container.setAttribute( 'string', 'test' );

    return container;
}

容器是< mutation string =“test”>< / mutation>

我在单元测试中调用此函数后如何检查?

QUnit.test( 'check function mutationToDom', function( assert ) {
    var container = testBlock.mutationToDom();
    assert.ok( container === ???, 'mutation is created with correct value' );
});

试试#1

我尝试使用XMLSerializer()将XML转换为字符串

var containerString = new XMLSerializer().serializeToString(container);

但是,containerString是

“< mutation xmlns =”http://www.w3.org/1999/xhtml“string =”test“>< / mutation>”

代替“< mutation string =”test“>< / mutation>”

解决方法:

如何检查outerHTML?

var testBlock = {
  mutationToDom: function() {
    var container = document.createElement('mutation');
    container.setAttribute('string', 'test');
    return container;
  }
};

QUnit.test( 'check function mutationToDom', function( assert ) {
    var container = testBlock.mutationToDom();
    assert.ok( container.outerHTML === '<mutation string="test"></mutation>', 'mutation is created with correct value' );
});
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.0.css">
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.6.0.js"></script>

标签:javascript,qunit,html,google-blockly
来源: https://codeday.me/bug/20190701/1347106.html