Test styles triggered by mouse over events
Asked Answered
L

2

9

I have a button that displays different styles when mouse moves over it:

background-color: green;
&:hover {
  background-color: red;
}

Here is my test:

fireEvent.mouseOver(button);

expect(button).toHaveStyle(`
  background-color: red;
`);

However, the test complained that the background color is green instead of red. I tried fireEvent.mouseEnter before calling mouseOver. Didn't make any difference. What did I miss?

Lariat answered 18/9, 2018 at 0:25 Comment(2)
No solution, but question also asked here: spectrum.chat/thread/108403f4-915b-4243-974c-c41af826b91dPliske
mouseOver in react-testing-library is asynchronous, you need to wait for it. See #64787289Tarp
E
0

You need to wait for the event to be fired and the style to be applied. You can try

fireEvent.mouseOver(button);

expect(await button).toHaveStyle(`
  background-color: red;
`);
Excisable answered 14/3, 2021 at 13:51 Comment(0)
S
-2

Maybe you can wrap it in act().

Sublapsarianism answered 4/12, 2022 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.