1

I am running some tests on components for a small project, and I keep getting the same error for the one component. When I run the project, everything operates as intended, however when I test I can't get rid of this TypeError: undefined is not an object(this.props.searchResults.map). I am confused by this because, as I said, it runs fine. Is it a problem with the way I'm writing my tests or is there an error in my code? The component is below:

class Results extends React.Component {
render(){
    const { handleEvent, searchResults } = this.props;
    return(
        <ul className="the-list">
            {this.props.searchResults.map((result, idx) => 
                <ResultItem 
                    key={`${result.trackId}-${idx}`} 
                    trackName={result.trackName} 
                    track={result}
                    handleClick={handleEvent} />
                )};
        </ul>
    );
  }
}

2 Answers 2

1

It seems like this.props.searchResults is undefined in your test. There are two options here:

1: Define a default prop for searchResults:

Results.defaultProps = {
    searchResults: []
}

2: Define searchResults in your test:

<Results searchResults={[]} />
Sign up to request clarification or add additional context in comments.

Comments

0

I think it could be because you forget the constructor

class Results extends React.Component {

  constructor(props) {
    super(props);
  }


render(){
    const { handleEvent, searchResults } = this.props;
    return(
        <ul className="the-list">
            {this.props.searchResults.map((result, idx) => 
                <ResultItem 
                    key={`${result.trackId}-${idx}`} 
                    trackName={result.trackName} 
                    track={result}
                    handleClick={handleEvent} />
                )};
        </ul>
    );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.