-
-
Notifications
You must be signed in to change notification settings - Fork 180
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
SpectatorHost.focus() is broken in a few ways when using Jest #373
Comments
I'm happy to provide a PR for this and add some tests, but I'd like guidance on whether you're ok with removing patchElementFocus(), or if you'd prefer a jest-only fix. |
We use the same code from Angular CDK. You can have a look and see what's the issue - https://github.com/angular/components/blob/master/src/cdk/testing/testbed/fake-events/element-focus.ts |
Damn you're fast! :) I'll take a look. |
Given what I can tell from the CDK comments, and given that no one else has complained about it outside of jest, it's probably safest to leave it as-is for browsers. But given that I've spent a bunch of time digging into where this is going wrong, I'm quite confident in saying it should be avoided for jest. I can pretty easily put the same tests in karma and test in a handful of browsers to see which versions of the test work in which browsers. I'll report back. |
Any news here? |
Yes: I apologize for the delay wrapping this up. I've tested this informally in karma, and the conclusion is:
I've had this on my TODO list to figure out how to fix; lmk if you want a PR or if you'd prefer to fix. |
You can create a PR. |
In Jest, don't patch HTML Element focus() or blur() methods, b/c they break correct jsdom behavior. In browsers, improve HTML Element focus() patch so it triggers a blur on the activeElement, and updates the activeElement.
In browsers, HTMLElement.focus() always sets `document.activeElement`, but focus + blur events may or may not be sent depending on focus. This ensures that focus + blur events are always sent if appropriate, and `document.activeElement` is set.
Also, fix potentially problematic blur check.
One thing I should point out now that Jest tests use the native (jsdom) Only legitimate focusable elements can be For example, elements that aren't normally focusable (eg a This is proper behavior, but this could conceivably break some Jest tests if the tests were written with the previous behavior, where any element can be focus()ed or blur()ed. Karma/browser tests can still focus() or blur() any element. |
I'm submitting a...
Current behavior
When using jest, calls to
SpectatorHost.focus()
do dispatch a (fake)focus
event, but the current implementation prevents automaticblur
events from occurring, and also causes thetoBeFocused()
matcher to not work. This is due to the call topatchElementFocus()
inDomSpectator.focus()
, which overwrites valid and more useful jsdomfocus()
methods with versions that don't work as well.The following test shows the issue, and a workaround:
Expected behavior
I think the best fix would be to remove
patchElementFocus()
from all platforms, but I'm not 100% sure why it's there (the comment says something about IE 11). My thinking in recommending this is that we want to test browser or platform capabilities as much as possible (eg if running karma on IE11, I would want the browser implementation to be used, b/c I'm testing browser compatibility).Alternatively,
patchElementFocus()
could be left as is for non-jest, and overridden for jest only, and only patch thefocus()
andblur()
methods when they're not present.Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Make
host.focus()
work as expected.Environment
The text was updated successfully, but these errors were encountered: