从Jest模拟方法触发之前,如何创建模拟DOM?

时间:2019-02-15 01:28:00

标签: javascript reactjs jestjs enzyme

模拟的getData()方法单击Test,在模拟后会产生以下结果,

TypeError:无法读取null的属性“值”

  68 |     // 
  69 |     getData(){
> 70 |         const elementName = (document.getElementById("sample-element") ;


// Test Case follows.. 
it('Check for getData', () => {
    getData = sinon.spy(wrapper.instance(), 'getData');
    wrapper.update();
    wrapper.find('#getData').simulate('click');
    expect(getData.called).toBe(true);
    getData.restore();  
});

getData(){
    const elementName = (document.getElementById("sample-element");
    fetch(config.getRowDataAPI+elementName)
        .then(response => response.json())
        .then(allRowData => this.setState({ allRowData },
            () => {
                this.setTable(this.state.allRowData);
            }
        )); 
}

0 个答案:

没有答案