Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

417
Visualizações
getByTestID not working for custom component

Working on writing a unit test for a large component. I've written one before for a smaller component, but this has several other custom components nested inside. I'm trying to select it so I can then figure out what to do with it, but I can't even get to that point right now.

MainComponent.tsx

This is a part of what's getting rendered by the component. PriceOverrideDateRange is another custom component, and I'm trying to target it with the testID
       <Counter
        />
              <MainComponentRange testID='range' />
        <Counter
        />

MainComponent.test.tsx

This is the part of the test I'm working on
  it('should be able to change the ranfe', () =>{



    //Mock out dependent function with jest
    //Nothing here right now...

    //Render with the props you want
    const { getByTestId } = render(        
          <MainComponent />

);

    //Locate screen components for test
    const range = getByTestId('range');


    //Perform user actions
    fireEvent.changeText(range, "01/22/2022");


    //Measure against expect cases
    expect(dateRange).toBe("1/22/2022");

  });

And this is the error message I'm getting:

  ● Main Component Test › should be able to change the range

    Unable to find an element with testID: dateRange

      143 |
      144 |     //Locate screen components for test
    > 145 |     const dateRange = getByTestId('range');
          |                       ^
      146 |
      147 |
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to pass the testID to the component as props and then add it the the view you are using I will add a simple example.

const App = () => {
  return <PriceOverrideDateRange testID="dateRange" />
}
const PriceOverrideDateRange = ({testID}) => {
  return <View testID="dateRange" ><Text>Hello</Text></View>
} 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda