我正在尝试测试将 HTML 元素附加到 dom 的函数。 我开玩笑地从测试中得到一个错误:
TypeError: Cannot read property 'appendChild' of null
let parentElementVideo = document.getElementById('adContentSectionVideo'); parentElementVideo.appendChild(widget);
parentElementVideo = null
it('test dom creation', () => {
document.body.innerText = '<div class="widget">' +
' <div class="adContentTitle">AD CONTENT</div>' +
' <div class="separator"></div>' +
' <div id="adContentSectionVideo"></div>' +
' <div class="adContentTitle">AD CONTENT</div>' +
' <div class="separator"></div>' +
' <div id="adContentSectionView1"></div>' +
' <div class="adContentTitle">AD CONTENT</div>' +
' <div class="separator"></div>' +
' <div id="adContentSectionView2"></div>' +
'</div>'
CreateAndDisplayWidgets.run([]);
}
如您所见,我已将“parentElementVideo”添加到 dom 中...因此它不应该为 null
运行函数:
let parentElementVideo = document.getElementById('adContentSectionVideo');
parentElementVideo.appendChild(widget);