English 中文(简体)
how to fix TestingLibraryElementError: Unable to find an element with the text: "insert text here" issue?
原标题:

Cannot fix this issue that is failing tests. The test seems to only return a fail with the first prop (title)?

//PropertyCard.js
import React from  react ;

const PropertyCard = (
  {
    title, type, bathrooms, bedrooms, price, city, email,
  },
) => {
  <div className="property-card">
    <div className="property-card-title">{title}</div>
    <div className="property-card-type">{type}</div>
    <div className="property-card-city">{city}</div>
    <div className="property-card-bathrooms">
      {bathrooms}
    </div>
    <div className="property-card-bedrooms">
      {bedrooms}
    </div>
    <div className="property-card-price">
      {price}
    </div>
    <a href={`mailto:${email}`} className="property-card-email">
      Email
    </a>
  </div>;
};
//Properties.js
import React, { useEffect, useState } from  react ;
import  ../styles/properties.css ;
import PropertyCard from  ./PropertyCard ;
import axios from  axios ;
import Alert from  ./Alert ;

const Properties = () => {
  const initialState = {
    properties: [],
  };

  const [properties, setProperties] = useState(initialState.properties);
  const [alert, setAlert] = useState({ message:    });

  useEffect(() => {
    axios
      .get( http://localhost:3000/api/v1/PropertyListing )
      .then((res) => setProperties(res.data))
      .catch(() => setAlert({
        message:  An error occured whilst trying to retrieve properties. Please try again later. ,
      }));
  }, []);

  return (
    <div className="properties">
      <h1>View Properties Page</h1>
      <Alert message={alert.message} />
      {properties.map((property) => (
        <div key={property._id} className="item">
          <PropertyCard {...property} />
        </div>
      ))}
    </div>
  );
};
//PropertyCard.test.js
import React from  react ;
import { render, screen } from  @testing-library/react ;
import PropertyCard from  ../components/PropertyCard ;

describe( PropertyCard , () => {
  const validProps = {
    title:  2 bedroom flat ,
    type:  Flat ,
    bedrooms:  2 ,
    bathrooms:  1 ,
    price:  1000 ,
    city:  Manchester ,
    email:  testing@surreal-estate.com ,
  };
  it( renders the correct props for each property , () => {
    render(<PropertyCard fields={validProps} />);
    expect(screen.getByText( 2 bedroom flat )).toHaveClass( property-card-title );
    expect(screen.getByText( Flat )).toHaveClass( property-card-type );
    expect(screen.getByText( 2 )).toHaveClass( property-card-bedrooms );
    expect(screen.getByText( 1 )).toHaveClass( property-card-bathrooms );
    expect(screen.getByText( 1000 )).toHaveClass( property-card-price );
    expect(screen.getByText( Manchester )).toHaveClass( property-card-city );
    expect(screen.getByText( Email )).toHaveClass( property-card-email );
  });
});

Screenshot of error

I ve searched and read similar issues with this error - but cannot seem to find a fix for my issue. Does anyone have any ideas why it s failing?

Seems to be to do with the useEffect() hook in Properties.js - tests were passing before I added this in to the project.

问题回答

PropertyCard component has no fields prop. It should be

render(<PropertyCard {...validProps} />);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签