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

117
Visualizações
Jest: Function Unit Test in React Functional Component

I want to test the function inside React Functional Component (not Class Component)

Here is my component:

const Component: React.FC = () => {
  const [openMenu, setOpenMenu] = useState<boolean>(false);

  const toggleMenu = () => setOpenMenu((prev) => !prev);

  return (
    <div>
      <ChildComponent openMenu={openMenu} toggleMenu={toggleMenu} />
    </div>
  );
};

export default Component;

I tried to run simple test and it's failed

  test('should be able to toggle open menu state', async () => {
    const wrapper = shallow(
      <Provider store={store}>
        <MemoryRouter initialEntries={['/']}>
          <DefaultLayout />
        </MemoryRouter>
      </Provider>
    );

    // wrapper.invoke('toggleMenu')();
    expect(wrapper.state('openMenu')).toBe(false);
  });

What I want to test is toggleMenu function update the openMenu correctly. I saw some answer from stackoverflow able to test with instance() or invoke() but that only apply for class component. Is that anyway to test function and state inside component without fire any event?

  • Unit test React functional component's function
about 4 years ago · Juan Pablo Isaza
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