【问题标题】:Writing React TestUtils test case for function without parameters为没有参数的函数编写 React TestUtils 测试用例
【发布时间】:2016-08-22 20:22:55
【问题描述】:

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

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

另外,如果有人能分享一些文档/文章来测试不同的前端场景,那就太好了。

【问题讨论】:

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


    【解决方案1】:

    所以 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');
            });
        });
    

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

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多