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?
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.
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 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.
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.
try this:
wrapper.find(Foo).at(0).props().href
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With