代码之家  ›  专栏  ›  技术社区  ›  Mauro74

酶找不到要使用mount单击的元素

  •  1
  • Mauro74  · 技术社区  · 8 年前

    我有一个tab React组件,我正在用酶和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. 知道为什么找不到节点吗?我试着找到 li 但得到了相同的错误。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   Krasimir    8 年前

    是不是因为你在渲染 <TabbedArea> 没有孩子。 tabNodes 方法循环 this.props.children 在你的测试中是空的。