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

TypeScript-使用酶调用React prop

  •  2
  • lobati  · 技术社区  · 6 年前

    Cannot invoke an expression whose type lacks a call signature. Type '{}' has no compatible call signatures.
    
    85     component.find(Link).at(0).prop('onNavigate')();
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    我如何克服这个错误?不确定是否有用,但更多的测试背景:

    it('does not hide the sidebar after a link is clicked', () => {
      const component = shallow(<Sidebar />);
    
      component.find(Link).at(0).prop('onNavigate')();
      component.update();
    
      expect(component.find(Link)).toHaveLength(3);
    });
    

    以及来自 Sidebar 组件:

    class Sidebar extends React.Component<any, any> {
    
      ...
    
      hideIfMobile() {
        const {mobile} = this.state;
    
        if (mobile) { this.setState({visible: false}); }
      }
    
      render() {
        const {visible} = this.state;
    
        if (!visible) {
          return (
            <div className='sidebar sidebar--hidden'>
              {this.sidebarToggle()}
            </div>
          );
        }
    
        const linkProps = {
          baseClass: 'sidebar__link',
          onNavigate: this.hideIfMobile,
        };
    
        return (
          <div className='sidebar sidebar--visible'>
            <h2 className='sidebar__header'>{'Menu'}{this.sidebarToggle()}</h2>
            <hr className='sidebar__divider' />
            <Link to='root' {...linkProps}><h2>{'FOCUS'}</h2></Link>
            <Link to='tasks' {...linkProps}><h2>{'ALL TASKS'}</h2></Link>
            <Link to='timeframes' {...linkProps}><h2>{'TIMEFRAMES'}</h2></Link>
          </div>
        );
      }
    }
    

    链接组件被包装在 react-redux :

    import {connect} from 'react-redux';
    
    import Link from 'src/route/components/link';
    import {getRouteName} from 'src/route/selectors';
    import {setRoute} from 'src/route/action_creators';
    
    function mapStateToProps(state) {
      return {routeName: getRouteName(state)};
    }
    
    export default connect(mapStateToProps, {setRoute})(Link);
    

    以及实际组件:

    class Link extends React.Component<any, any> {
      navigate(event) {
        event.preventDefault();
    
        const {onNavigate, params, setRoute, to} = this.props;
    
        setRoute({name: to, ...params});
    
        if (onNavigate) { onNavigate(); }
      }
    
      path() {
        const {params, to} = this.props;
        const pathParams = mapValues(params, value => value.toString());
    
        return findRoute(to).toPath(pathParams);
      }
    
      className() {
        const {baseClass, className, to, routeName} = this.props;
    
        return classnames(
          baseClass,
          {[`${baseClass}--active`]: baseClass && routeName === to},
          className,
        );
      }
    
      render() {
        const {children} = this.props;
    
        return (
          <a
            href={this.path()}
            className={this.className()}
            onClick={this.navigate}
          >
            {children}
          </a>
        );
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   lobati    6 年前

    结果证明 Link 在这种情况下,在我的测试文件中定义为:

    const Link = 'Connect(Link)';
    

    import Link from 'src/route/containers/link';