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

440
Visualizações
React Testing Library fireEvent.mouseOver/mouseEnter not detecting css :hover changes

I have a button that changes colour and and the children scale on hover. When I try to trigger this interaction with rtl/jest, my test fails with the following message: Compared values have no visual difference.

My button is styled as follows:

    &:hover,
    &:focus {
      * {
        cursor: pointer;
      }

      background: ${(props) => lighten(0.1, props.theme.primary)}; // lighten function from polished
      transition: background 0.25s;

      figure {
        transform: scale(0.9);
        transition: transform 0.25s ease;
      }
    }

The JSX:

      <StyledFABButton aria-label="Open window" onClick={handleClickFAB}>
        <StyledIconWrapper data-testid="breifcase-icon-wrapper">
          <BriefcaseIcon />
        </StyledIconWrapper>
      </StyledFABButton>

And my tests:

  it("should change button appearance if FAB is hovered", () => {
    jest.useFakeTimers();
    jest.spyOn(global, "setTimeout");
    customRender(<FAB />, {});
    const button = screen.getByLabelText(/Open window/i);
    fireEvent.mouseEnter(button);
    expect(button).not.toHaveStyle("background: #31c04d");
  });

  it("should change icon appearance if FAB is hovered", () => {
    jest.useFakeTimers();
    jest.spyOn(global, "setTimeout");
    customRender(<FAB />, {});
    const button = screen.getByLabelText(/Open Recruitbot/i);
    fireEvent.mouseEnter(button);
    const iconWrapper = screen.getByTestId(/breifcase-icon-wrapper/i);
    expect(iconWrapper).toHaveStyle("transform: scale(0.9)");
  });

I've had previous issues with polished functions, but this hasn't been an issue since I incorporated my customRender, which simply wraps the component in a ThemeProvider and ContextProvider.

EDIT: Here's my customRender:

// contextProps is just a fake context object
export const customRender = (
  ui: any,
  { contextProps = baseMockContext, ...renderOptions }: any
) => {
  return render(
    <ThemeProvider theme={theme}>
      <AppContext.Provider value={contextProps}>{ui}</AppContext.Provider>
    </ThemeProvider>,
    renderOptions
  );
};

export * from "@testing-library/react";
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