component.find('a).prop('href')返回undefined而不是href值

时间:2017-01-19 13:31:47

标签: react-router jestjs enzyme

我有一个导航组件,它使用Link,IndexLink来渲染导航栏:

class Navigation extends Component {

  renderLinks = (linksData) => {
    return linksData.map((link) => {
      if(link.to === '/') {
        return (
          <li key={link.text}>
            <IndexLink to={link.to}>
              <i>{link.icon}</i>
              <span>{link.text}</span>
            </IndexLink>
          </li>
        );
      } else {
        return (
          <li key={link.text}>
            <Link to={link.to}>
              <i>{link.icon}</i>
              <span>{link.text}</span>
            </Link>
          </li>
        );
      }
    });
  };

  render() {
    const {links} = this.props;
    return(
      <div>
        <ul>
          {this.renderLinks(links)}
        </ul>
      </div>
    )
  }
}

Navigation.propTypes = {
  links: React.PropTypes.array.isRequired
}

export default Navigation;

现在我想测试我的链接是否正确呈现:

import React from 'react';
import {Link, IndexLink} from 'react-router';
import {mount} from 'enzyme';
import Navigation from '../components/core/Navigation.component';

describe('<Navigation/>', () => {
  it('should render Navigation links', () => {
    const links = [
      {
        to: '/',
        icon: 'hourglass_empty',
        text: 'Timer'
      }
    ];
    const navigation = mount(<Navigation links={links}/>);
    console.log(navigation.find('a').prop('href'));
  });
});

但这只是注销未定义。我怎么能测试正确的href值是否传递给我的锚点?

1 个答案:

答案 0 :(得分:4)

尝试: wrapper.find(Foo).at(0).props().href