-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
strange toHaveFocus() behaviour #53
Comments
Hi @artooras, sorry for the lack of attention in SO. I'll be able to take a deeper look at this tomorrow. At first sight I'm not sure what it is. It'd be good if you can provide a small reproducible example in codesandbox.io. Although one final note: You do not need to simulate the click and the focus before This of course does not solve the underlying problem, but my point it, you can go forward with the test without those steps. Unless they're there for some reason. Are they? |
Hi @artooras, the I believe |
Thanks @smacpherson64. Why doesn't @gnapse, as for the |
Hey @artooras, I did some testing on this and think it is because a manual click in the browser couples a focus event with it, where firing the click event only fires a click event: Exploration using
|
Hahaha, sorry @artooras I didn't fully answer the question. Since the tooling has different behavior than the browser it is necessary in this case to work with the tooling (or change it for the better :-)!). I think you will run into the same with the change event as well. I do not believe, I think you bring up really good points, the points may have already been discussed but I am not sure. A good avenue for this would be to create a bug report on react-testing-library describing the discrepancies you are experiencing between the browser and I hope this helps! |
@artooras I think the problem is related to the fact that when an element looses focus, the next element to acquire it actually gets it on the next tick, and briefly, during a tick, the focus is placed on the document body. Source: https://stackoverflow.com/a/11299876 Can you please wrap your expectation // import `wait` from react-testing-library
import { render, fireEvent, cleanup, wait } from 'react-testing-library';
// ...
fireEvent.click(titleInput)
await wait(() => {
expect(titleInput).toHaveFocus()
}); When you programmatically give it focus directly, instead of with a click, I think it happens immediately. But with a click (or when acquired via pressing the tab key in a web page) it happens in this manner described above. Agreed is weird, because even if it's async, I would expect to pass focus directly from the current active element to the new one, without briefly giving focus to the body. But that's how it is, apparently. Let us know if this helps. Update: BTW, this is not a jsdom thing. I actually realized this was the trouble you might be facing, when I encountered it precisely today in a UI I've been working on. It happens in the browser too. That's where I encountered first, not in tests. |
Hey @gnapse, I’ve updated the exploration in codesandbox to use wait but the test is failing: (The wait takes about 5 seconds to fail because of the timeout.) https://codesandbox.io/s/l9j2zzny9l Can you check to make sure It matches what you said above or explain further what you mean (if you have a shareable example that would be awesome!). I have not heard of that behavior before! |
Ok, I'll dig up a little bit more. I did not know about this behavior I commented before. I just found out recently when it actually helped me solve a problem unrelated to jest-dom. I thought it could explain this too. |
Hmmm, I wonder if A couple of tests I'm doing seem like it's just about triggering event handlers. But if what I suspect is wrong, then I'm not sure what are we (me and @artooras who raised this issue above) doing wrong. The code for I checked our own tests of @kentcdodds care to chime in? |
The best I can come up with is that you'd need to call At most, it may be conceivable (?) to suggest dom-testing-library to do this ad-hoc action specifically for the |
Huh... Interesting. I think that between this and #59, we're starting to see that what people want is something more like a For example, this would focus the input and dispatch user.type(inputNode, 'hello') Why don't we open something up in |
Sorry for posting the question here, but I have tried spectrum.chat and SO without much success so far...
I'm having trouble understanding how
toHaveFocus()
works exactly. Here's my setup:MyComponent.js
MyComponent.test.js
When I do this:
The console logs the following:
That
textarea
element is the one I am targeting. But then, when I do this:I get this error:
I don't quite understand why when I'm trying to assert a
textarea
elementtoHaveFocus()
, I'm receiving an error that references the entire DOM tree underbody
...The text was updated successfully, but these errors were encountered: