How can I simulate window.focus() with react testing-library?

4.2k Views Asked by At

I've been doing a lot of research, and I can't find a way to successfully simulate a window focus event.

I looked through "testing-library/user-event" and still can't find a way to do this.

In my component, we have window.addEventListener('focus', doSomething); but this is never getting invoked via the test runner.

1

There are 1 best solutions below

1
neaumusic On

I have verified that fireEvent.focus(window); does actually trigger the handler, and that the callback just isn't behaving as expected.