为 javascript 测试创建 HTML 个元素

Creating HTML elements for javascript testing

所以我有一个特别的问题,在我的反应组件中我使用了这样的命令:

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();
});

在您的酶测试中创建 DOM 个元素:

伙计们,要在测试中创建 DOM 元素,实际上非常简单,只需像在原版 javascript 中一样输入即可,使用全局关键字:

it('Should hide the modal', function() {
        var wrapper, instance;
        var modalsContainer = global.document.createElement('div');
        var recognitionProfileZoom = global.document.createElement('div');
        var categoryZoom = global.document.createElement('div');

        modalsContainer.id = 'modalsContainer';
        recognitionProfileZoom.id = 'recognitionProfileZoom';
        categoryZoom.id = 'categoryZoom';
        global.document.body.appendChild(modalsContainer);
        global.document.body.appendChild(recognitionProfileZoom);
        global.document.body.appendChild(categoryZoom);  

完成后,您可以期待值,并正常使用 DOM 元素。