簡體   English   中英

酶找不到使用安裝單擊的元素

[英]Enzyme can't find element to click using mount

我有一個選項卡React組件,正在用Enzyme和Jest測試。

這是組件:

class TabbedArea extends React.Component{


  constructor(props) {
    super(props)

    this.handleClick = this.handleClick.bind(this);
    this.state = {
      activeIndex: this.props.activeIndex || 0
    };
  }

  componentWillReceiveProps(nextProps){
    if (this.state.activeIndex !== nextProps.activeIndex) {
      this.setState({
        activeIndex: nextProps.activeIndex
      });
    }
  }

  handleClick(index, e) {
    e.preventDefault();
    this.setState({
      activeIndex: index
    });
  }

  tabNodes() {
    return (_.map(this.props.children, (child, index) => {
        let className = classNames({'active': this.state.activeIndex === index});
          return (
            <li key={index} onClick={(e) => this.handleClick(index, e)}>
              <a className={className} href="#">{child.props.display}</a>
            </li>
          )
        }
      )
    )
  }

  contentNodes() {
    return (
      _.map(this.props.children, (child, index) => {
        if(this.state.activeIndex === index) {
          return (
            <div className="TabPane" key={index}>
              {child.props.children}
            </div>
          )
        }
      }
      )
    )
  }

  render() {

    return (
      <div className={`${styles.ParcelResultsTrackingHistory} col-md-12`}>
        <ul className="nav nav-tabs" id="navTabs">
          {this.tabNodes()}
        </ul>
        <section>
          {this.contentNodes()}
        </section>
      </div>
    );
  }

}

export default TabbedArea;

這是我的測試:

describe('Given the Tabbed Area component is rendered', () => {


  describe('Initial Snapshots', () => {
    let component
    beforeEach(() => {
      component = shallow(<TabbedArea />)
    })
    it('should be as expected', () => {
      expect(shallowToJson(component)).toMatchSnapshot()
    })
  })

  describe('I click on the second tab', () => {
    let component
    let tab2

    component = shallow(<TabbedArea/>)
    tab2 = component.find('ul li').at(1);
    tab2.simulate('click');

    describe('the content of the second tab is rendered', () => {
      component.update();

      it('should match the snapshot', () => {
        expect(shallowToJson(component)).toMatchSnapshot()
      });

    });

  });

  describe('Clicking on the tab', () => {
    const wrapper = mount(<TabbedArea/>)
    const handleClick = jest.spyOn(wrapper.instance(), 'handleClick');
    wrapper.update();
    const tab = wrapper.find('ul#navTabs li').first()
    tab.simulate('click')

    it('will call handleClick', () => {
      expect(handleClick).toBeCalled();
    });
  });

})

快照測試運行正常,但是當我嘗試測試handleClick它失敗並顯示: Method “simulate” is only meant to be run on a single node. 0 found instead. Method “simulate” is only meant to be run on a single node. 0 found instead. 知道為什么找不到該節點嗎? 我嘗試通過id查找li ,但是遇到了相同的錯誤。

謝謝

是不是因為您正在渲染沒有子代的<TabbedArea> tabNodes方法遍歷this.props.children ,在您的測試中該值為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM