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

435
Views
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
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!