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

时间:2016-04-28 07:47:39

标签: reactjs mocha chai karma-mocha reactjs-testutils

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

如何使用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);
});

此外,如果有人可以分享一些用于测试不同前端场景的文档/文章,那将会非常棒。

1 个答案:

答案 0 :(得分:0)

因此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');
        });
    });

需要找出测试应用程序前端导航部分的方法