Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  182] [ 1]  / answers: 1 / hits: 22652  / 7 Years ago, thu, march 16, 2017, 12:00:00

I have the following component:



import React, { Component } from 'react';
import {Link, IndexLink} from 'react-router';

class Navbar extends Component {

renderLinks = (linksData) => {
return linksData.map((linkData) => {
if(linkData.to === '/') {
return(
<div className=navbar-link-container key={linkData.to}>
<IndexLink activeClassName=navbar-active-link to={linkData.to}>
<i className=navbar-icon material-icons>{linkData.icon}</i>
<span className=navbar-link-text>{linkData.text}</span>
</IndexLink>
</div>
)
}
else {
return(
<div className=navbar-link-container key={linkData.to}>
<Link activeClassName=navbar-active-link to={linkData.to}>
<i className=navbar-icon material-icons>{linkData.icon}</i>
<span className=navbar-link-text>{linkData.text}</span>
</Link>
</div>
)
}
})
};

render() {
return (
<div className={`navbar navbar-${this.props.linksData.length}`}>
{this.renderLinks(this.props.linksData)}
</div>
)
}
}

Navbar.propTypes = {
linksData: React.PropTypes.array.isRequired,
};

export default Navbar;


Now I am trying to write a unit test that will check the if condition (returning IndexLink or Link depending on the .to property):



But I can't seem to test for the exact jsx return of the function, since when I console.log one of the returns I get this:




{ '$$typeof': Symbol(react.element),
type: 'div',
key: '/',
ref: null,
props:
{ className: 'navbar-link-container',
children:
{ '$$typeof': Symbol(react.element),
type: [Object],
key: null,
ref: null,
props: [Object],
_owner: null,
_store: {} } },
_owner: null,
_store: {} }




This is the test I have written so far:



it('renderLinks should return a IndexLink', () => {
const wrapper = shallow(<Navbar linksData={mockLinksData}/>);
const renderLinksReturn = wrapper.instance().renderLinks(mockLinksData);
let foundIndexLink = false;
renderLinksReturn.map((linkHtml) => {
console.log(linkHtml);
});
expect(foundIndexLink).toBe(true);
})


Now I do not know what to test against to see if the function is running correctly. Is there a way to 'mount' the return of the function like a component? Or is there a simple method to return a html string of the actual return that I can check against?


More From » reactjs

 Answers
25

I think you don't need to call



const renderLinksReturn = wrapper.instance().renderLinks(mockLinksData);


as it will be called when Navbar will be rendered.



Your solution is correct but in case you want some alternative robust ways to test it.



Since this test specifically tests for IndexLink and assumes that mockLinksData contains to = /



it('renderLinks should return a IndexLink when passed a link with to:/', () => {
const wrapper = shallow(<Navbar linksData={mockLinksData}/>);

// You can use both component name or component displayname
// IndexLink or IndexLink
expect(wrapper.find('IndexLink')).to.have.length(1);

// In case you want to test whether indexLink has appropriate props or classes.
const indexLink = wrapper.find(IndexLink).first();

// Check whether indexLink has pass prop
expect(indexLink.props().to).to.equal(/);

// Check whether indexLink has correct classes set.
expect(indexLink.hasClass('navbar-active-link')).to.equal(true);

// Check whether indexLink displays the link test correctly
expect(indexLink.find('navbar-link-text').text()).to.equal(mockLinksData.text);


});

[#58517] Wednesday, March 15, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reed

Total Points: 725
Total Questions: 85
Total Answers: 89

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;