Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

component.find('a).prop('href') returns undefined instead of the href value

I have a navigation component that uses Link, IndexLink to render the navigation bar:

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;

Now I want to test that my links render correcttly:

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'));
  });
});

But this just logs out undefined. How am I able to test that the correct href value is getting passed to my anchors?

like image 332
Miha Šušteršič Avatar asked Jan 19 '17 13:01

Miha Šušteršič


People also ask

Can I use a href In React?

This href attribute contains the URL or path to the destination page. It may be a relative URL or an absolute URL. In React, relative URLs should always be handled by the link tag provided by the React Router, and pure anchor tags should only be used for absolute paths.

How do I add a hyperlink in React?

To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.

What is in href in HTML?

What is the HTML a href attribute? In HTML, the inline a (anchor) element denotes a hyperlink from one web address to another. All functional a elements must contain the href (hypertext reference) attribute inside the opening a tag. The href attribute indicates the destination of the hyperlink.

How add href attribute in jQuery?

Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.


1 Answers

try this: wrapper.find(Foo).at(0).props().href

like image 129
Nate Ben Avatar answered Sep 29 '22 12:09

Nate Ben