Skip to content
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

Inputs are not focused when clicked (v14) #1002

Open
IanVS opened this issue Jul 28, 2022 · 19 comments
Open

Inputs are not focused when clicked (v14) #1002

IanVS opened this issue Jul 28, 2022 · 19 comments
Labels
bug Something isn't working environment Issue with the environment needs investigation Someone has to do research on this

Comments

@IanVS
Copy link

IanVS commented Jul 28, 2022

Reproduction example

https://github.com/IanVS/storybook-testing-user-event-click-focus

Prerequisites

  1. Using V14, try to click an input field: await userEvent.click(getByRole('textbox')); for example

Expected behavior

The input element should be fully focused, ready to accept user input (flashing cursor).

Actual behavior

The input is half-way focused, with a focus ring, but no flashing cursor.

User-event version

14.3.0

Environment

Testing Library framework: @testing-library/[email protected]

JS framework: [email protected]

Test environment: [email protected]

DOM implementation: browser

Additional context

This was working correctly in V13. I have instructions in the README of the reproduction of how to verify.

@IanVS IanVS added bug Something isn't working needs assessment This needs to be looked at by a team member labels Jul 28, 2022
@ph-fritsche
Copy link
Member

Are you sure this is an issue with user-event ?
If yes, please break it down to a reproduction example that doesn't wrap user-event in third-party code.
If not, please raise this with https://github.com/storybookjs/testing-library. I don't know if their integration is correct and it seems they reverted and postponed the upgrade to v14.

@IanVS
Copy link
Author

IanVS commented Jul 28, 2022

I did also experience the issue when using user-event directly, and as I describe in the readme, forcing npm to download v13 causes user-event to work correctly. But I can modify the reproduction if that's helpful.

@IanVS
Copy link
Author

IanVS commented Jul 28, 2022

I've pushed an update to the reproduction to import from @testing-library/user-event directly, with the same behavior.

@ph-fritsche
Copy link
Member

Thanks for the report.

This is odd:
https://codesandbox.io/s/young-https-oghigy?file=/src/App.js

In Firefox the focus is set into the element per .click API, in Chrome it isn't.
But calling .focus() directly on the element works in both. Although this is how the target is focused by the click API internally - resulting in the outline that indicates that the element gained focus 🤔

paste() doesn't work in either browser.

@ph-fritsche ph-fritsche added needs investigation Someone has to do research on this environment Issue with the environment and removed needs assessment This needs to be looked at by a team member labels Jul 28, 2022
@jaworek
Copy link

jaworek commented Aug 4, 2022

I'm currently working on updating @testing-library/user-event in my project and it seems that I've stumbled upon the same bug. At first I thought that this is related to #1018, but this seems to be actual reason. Downgrading to v13 fixes the issue for the time being.

@ph-fritsche
Copy link
Member

ph-fritsche commented Aug 18, 2022

With v14.4 both the .click() and .paste() yield the expected result with one exception:
In Chrome the cursor is missing and .selectionStart/.selectionEnd are always 0 after the click although they should be at value.length

https://codesandbox.io/s/intelligent-zhukovsky-8z89zj?file=/src/App.js

@aaron-nance
Copy link

If I use the convenience method (field.click()) I have this issue, but if I use user.click(field), the text field gets focus properly. This happens when I'm using 14.4 with Jest 27.5 and testEnvironment: "jest-environment-jsdom"

@IanVS
Copy link
Author

IanVS commented Sep 20, 2022

@aaron-nance:

the text field gets focus properly.

It does get focus, but in Chrome, it does not get a cursor, so typing into the field after the click does not work.

@chawax
Copy link

chawax commented Mar 11, 2023

I still have this problem, focus is not given to the input when using click(). Is there a workaround for this ?

@chawax
Copy link

chawax commented Mar 11, 2023

I could make it work using focus() to give focus to the field, then using clear and type.

For example :


await screen.getByRole("textbox").focus();
await user.clear(screen.getByRole("textbox"));
await user.type(screen.getByRole("textbox"), "something");

@chawax
Copy link

chawax commented Mar 13, 2023

Actually the focus() solution doesn't work, it is unstable :( So anyone has a reliable workaround for that ?

@IanVS
Copy link
Author

IanVS commented Aug 25, 2023

Hi, has anyone found a workaround to this issue? It's still blocking my migration to user-event 14, and I'd love to update.

@IanVS
Copy link
Author

IanVS commented Aug 29, 2023

Actually, I tried upgrading anyway, and my tests are passing, though the codesandbox reproduction above still fails. Not sure what's going on, TBH, but I'll take it!

@mryechkin
Copy link

Just ran into this issue while upgrading to v14 - I'm on 14.5.2 to be exact. None of the above solutions work for me. No component or test code changes other than upgrading the library. Has anyone gotten around this issue in recent time?

@zemacnica
Copy link

zemacnica commented Jun 28, 2024

I have the same problem with focus.
"@testing-library/jest-dom": "^6.4.6",
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.5.2",

I downgraded to version 13.5.0 and focus after click is working

@unional
Copy link

unional commented Jul 13, 2024

I have the same problem.

To me, it is working on storybook (chrome/chromium) but not on jest (jsdom)

@bodograumann

This comment was marked as off-topic.

@IanVS
Copy link
Author

IanVS commented Jul 18, 2024

I do notice that focus-visible is sometimes applied to elements that are clicked, but not always. It results in snapshot flake, which is pretty annoying. But that seems like a separate issue from the core of this one.

@bodograumann

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working environment Issue with the environment needs investigation Someone has to do research on this
Projects
None yet
Development

No branches or pull requests

9 participants