0

I am mapping an array called tours

   export const tourData = [
  {
    id: 1,
    city: "new york",
    img: "./img/newyork.jpeg",
    name: "new york bridge tour",
    info:
      "Lorem ipsum dolor sit amet consectetur adipisicing elit. Vel,repellendus!"
  },

as shown below

 state={
    tours: tourData
} 
 render(){
    const {tours} = this.state;

    return(
        <section className="tourlist">
          {tours.map(tour => {
              return (
                  <Tour key={tour.id} tour={tour} removeTour={this.removeTour}/>
              )
          })}
        </section>
    )
}

and i pass the tour as a prop into the Tour component.

const {id , city , img, name, info} = this.props.tour;
    const {removeTour} = this.props;

The webapp works well but when i make a test for the Tour component and i pass in values as props

 const props ={
            id : 1,
            city: 'Test City ',
            img: 'Test img',
            name: 'Test name',
            info: 'Test Info'
        }

i get errors like

Cannot read property 'id' of undefined

but the webapp works fine. need some help on this and thanks in advance

6
  • how you tours variable's value looks like ? Commented Sep 18, 2019 at 11:52
  • Did your tours variable get called before you initialize it? Commented Sep 18, 2019 at 11:53
  • @P4uB0rd4 i called it Commented Sep 18, 2019 at 11:55
  • 2
    array called tours That looks like a object Commented Sep 18, 2019 at 11:55
  • @TheMaster it actually an array Commented Sep 18, 2019 at 11:59

1 Answer 1

5

while passing values to the props of Tour component in your tests you should be doing something like this

const props = {
  tour: {
    id : 1,
    city: 'Test City',
    img: 'Test img',
    name: 'Test name',
    info: 'Test Info'
  }
};

since inside your Tour component it is reading from this.props.tour.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help..!!

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.