为没有参数的函数编写 React TestUtils 测试用例

Writing React TestUtils test case for function without parameters

所以我有一个功能可以从特定页面删除所有标签。这个函数中声明了一个变量来完成这项工作。我需要将虚拟值传递给此变量,以便传递该值并执行函数。

如何使用 ReactJS TestUtils 为以下内容编写测试用例。

_removeAllTabbing() {
const accordionTitleAnchors = [
    document.getElementById('accordion-panel-1').firstChild,
    document.getElementById('accordion-panel-2').firstChild,
    document.getElementById('accordion-panel-3').firstChild,
    document.getElementById('accordion-panel-4').firstChild,
    document.getElementById('accordion-panel-5').firstChild
];

_.each(accordionTitleAnchors, accordionTitleAnchor => {
    this._removeTabbing(accordionTitleAnchor);
});

}

到目前为止我有这个

xit('should call _removeAllTabbing function', () => {
    const educate = new EducateAccordion();
    const accordionTitleAnchors = TestUtils.scryRenderedDOMComponentsWithClass(this.component, 'panel-title');;

    educate._removeAllTabbing(accordionTitleAnchors);
});

此外,如果有人可以分享一些 docs/articles 用于测试不同的前端场景,那就太好了。

所以 renderIntoDocument 没有工作。 :( 不知道为什么?

这是适合我的代码。

it('should validate _removeAllTabbing function', () => {
        const educate = new EducateAccordion();
        const activeKey = 1;

        const dummyElement = document.createElement('div');

        for (let i = 1; i <= 5; i++) {
            const temp = document.createElement('div');

            temp.setAttribute('id', 'accordion-panel-' + i);

            const temp2 = document.createElement('div');

            temp2.setAttribute('class', 'panel-title');
            temp2.setAttribute('role', 'tablist');

            temp.appendChild(temp2);
            dummyElement.appendChild(temp);
        }

        document.body.appendChild(dummyElement);

        educate._removeAllTabbing();

        this.accordionDummy = ReactDOM.findDOMNode(dummyElement);

        this.accordionTitleAnchors = this.accordionDummy.getElementsByClassName('panel-title');

        _.each(this.accordionTitleAnchors, (item) => {
            expect(item.getAttribute('tabIndex')).to.equal('-1');
        });
    });

需要想办法测试应用程序前端的导航部分