1
所以我有一个特殊的问题创建HTML元素,在我的反应组分i中使用这样的命令:的JavaScript测试
document.getElementById('modalsContainer').appendChild(recognitionProfileZoom);
document.getElementById('modalsContainer').appendChild(categoryZoom);
和:
document.getElementById('cddoccategoryzoom').value;
但是通过ID中指定的这些元素不存在于我的组件中。
如何在测试中创建这些对象?
下面的代码将使用这些元素,而是因为他们不存在,则失败(的hideModal功能利用从之前的document.append)
describe("CaptureProfileModal functions tests", function() {
it('Should hide the modal', function() {
var wrapper, instance;
wrapper = mount(
<CaptureProfileModal
gridAction={1}
captureSettingCode={15}
fields={mockedFields}/>
);
wrapper.setState({
showModal: true
});
instance = wrapper.component.getInstance();
instance.hideModal();
});
对于初学者来说,所有ID都是小写字母,如果您希望它可以与所有浏览器一起使用,则不应在ID中使用大写字母。也可以使用获取元素并检查getElementByID的返回值,但不要认为它会成功。 – SPlatten
HTML元素ID [区分大小写](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById)。自IE8以来的所有浏览器都支持'getElementById'中的区分大小写的字符串。 –
为什么你不只是嘲笑他们? –