2016-04-28 37 views
0

所以我有一個功能,刪除特定頁面的所有標籤。在這個函數中聲明瞭一個變量來完成這項工作。我需要將虛擬值傳遞給此變量,以便值傳遞並執行函數。寫入React 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); 
}); 

而且,這將是巨大的,如果任何人都可以分享一些文檔/文章來測試不同的前端方案。

回答

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

需要想出辦法來測試應用程序前端的導航部分

相關問題