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

130
Visualizações
How to test if a react component was clicked

I have a react component called HelpButton, which looks like this:

import helpLogo from '../Resources/helplogo.svg';

function HelpButton(props) {

    const [isOpen, setisOpen] = React.useState(false)

    function toggle() {
        setisOpen(prevIsOpen => !isOpen)
        if (isOpen)
            console.log("Help Open")
        else
            console.log("Help Closed")
    }

    return (
        <div className="helpIconBgrnd" onClick={toggle} data-testid="helpIconBgrnd">
            <img className="help-icon" src={helpLogo} alt='help-icon' />
        </div>
    )
}

export default HelpButton

I want to test if this component was clicked and I know there is an approach where you use expect(mockCallbackFunction).toHaveBeenCalledTimes(number). But the problem is that I do not pass the toggle function as a prop to HelpButton, instead it is declared inside the HelpButton component, so I don't think I can use this approach. How can I test if the HelpButton component was clicked?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try This. It's re-render while isOpen property is changed.

useEffect(()=>{
toggle();
},[isOpen]);
about 4 years ago · Juan Pablo Isaza Relatório

0

So you want to check in 'Some Component' whether HelpButton Component is open or not?

Try this:

import { useState } from 'react';

const Parent = () => {
  const [isOpen, setisOpen] = useState(false);

  //now you know if the component is open, you can do whatever you need with it

  const handleToggle = () => setisOpen(prevState => !prevState);

  return (
    <HelpButton onToggle={handleToggle}/>
  )
}  

export default Parent;

const HelpButton ({ onToggle }) {
  return (
    <div className="helpIconBgrnd" onClick={onToggle} data-testid="helpIconBgrnd">
      <img className="help-icon" src={helpLogo} alt='help-icon' />
    </div>
  );
}

export default HelpButton;
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