Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
"No se puede encontrar un elemento con el texto..." durante la prueba de reacción

Tengo una pequeña aplicación React/TypeScript/GraphQL que se conecta a una API, obtiene productos y los muestra en la página.

Estoy tratando de crear una prueba que verifique si el nombre del producto está en el documento después de un simulacro de GraphQL, usando getByText , pero sigo recibiendo TestingLibraryElementError: Unable to find an element with the text: iPad 5g. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

También intenté usar getByRole (ver más abajo), pero me da el error TestingLibraryElementError: Unable to find an accessible element with the role "paragraph" and name '/month to month/i'

¿Hay una mejor manera de hacer esto? ¿Cómo puedo verificar si el nombre del producto se muestra correctamente? Agradezco cualquier ayuda.

ProductCard.test.js

 import React from "react"; import { render, screen } from "@testing-library/react"; import TestRenderer from 'react-test-renderer'; import { MockedProvider } from '@apollo/client/testing'; import ProductCard from "../ProductCard"; import { LOAD_PRODUCTS } from "../GraphQL/Queries"; import '@testing-library/jest-dom'; it('renders ProductCard successfully', async () => { const productMock = { request: { query: LOAD_PRODUCTS, }, result: { data: { products: { productName: 'iPad 5g', code: 'i5g', price: 599.99 } }, }, }; const component = TestRenderer.create( <MockedProvider mocks={[productMock]} addTypename={false}> <ProductCard displayName={productMock.result.data.products.productName} price={productMock.result.data.products.price} /> </MockedProvider>, ); await new Promise(resolve => setTimeout(resolve, 0)); expect(screen.getByText(`iPad 5g`)).toBeInTheDocument(); // expect(screen.getByRole('paragraph', { name: /iPad 5g/i })).toBeInTheDocument(); });

TarjetaProducto.tsx

 // imports // ... // ... // TypeScript types // ... // ... const ProductCard: React.FC<Props> = ({ productName, price }) => { return ( <> <Typography>{productName}</Typography> <Typography>{price}</Typography> <Typography><p>Great new features</p></Typography> <Button>Buy Now</Button> </> ); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Parece que no necesita MockedProvider si solo está probando un componente "tonto" (también conocido como uno que solo muestra algo de HTML y no tiene otra lógica). Solo necesita MockedProvider si está probando un componente que usa useQuery o useMutation , en cuyo caso esos ganchos devolverían el valor de mock.result que proporcione. Intente eliminar MockedProvider por completo y simplemente represente el componente con algunos accesorios y vea si eso ayuda.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!