You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Created an input element that has a keydown event handler that calls event.preventDefault() if the event.key is ArrowLeft.
Use userEvent.type to enter text into the input element.
Use userEvent.type to enter the {arrowleft} special key.
Attempted to assert that the input's selectionStart was not moved by the arrow left key press.
What happened:
When using userEvent.type to enter the {arrowleft} special key the selectionStart is updated despite preventDefault being called on the event by an event handler attached on the input element.
Minimally it seems like navigationKey method should have support for not updating the selectionStart when the event default is prevented. However, I see that in other cases in type.js there are other special key event handlers that also do not respect event.preventDefault, namely: handleArrowDown, handleArrowUp, handleEsc, handleSelectAll, and handleSpace. It's worth noting that some of these handlers for the special keys utilize multiple key events (keyDown and keyUp as opposed to keyPress). Perhaps in all cases where they are custom event handlers attached to these native events there should be some checking if the default was prevented.
Related and possibly duplicative
Apologies if this issue has already been captured elsewhere in sufficient detail.
Minimal solution would indeed be to get navigationKey to the same standard as the other functions - adding missing events and repeating code.
Best solution would be to do the rewrite proposed in #521 . That would also tackle this issue.
Environment
@testing-library/user-event@^12.8.1
jest@^26.6.3
jsdom@^16.4.0
What you did
event.preventDefault()
if theevent.key
isArrowLeft
.userEvent.type
to enter text into the input element.userEvent.type
to enter the{arrowleft}
special key.selectionStart
was not moved by the arrow left key press.What happened:
When using
userEvent.type
to enter the{arrowleft}
special key theselectionStart
is updated despitepreventDefault
being called on the event by an event handler attached on the input element.Reproduction repository:
https://codesandbox.io/s/react-testing-library-selectionstart-issue-yb15q?file=/src/App.test.js
Problem description:
The
preventDefault
does not appear to be respected with regards to using the arrow keys when determining theselectionStart
.In
type.js
I can see that the arrow keys usesnavigationKey
which does not appear to have any handling for if the event default was prevented.However, I do see that some other characters do have special handling for this use case, for example the backspace key:
Suggested solution:
Minimally it seems like
navigationKey
method should have support for not updating theselectionStart
when the event default is prevented. However, I see that in other cases intype.js
there are other special key event handlers that also do not respectevent.preventDefault
, namely:handleArrowDown
,handleArrowUp
,handleEsc
,handleSelectAll
, andhandleSpace
. It's worth noting that some of these handlers for the special keys utilize multiple key events (keyDown
andkeyUp
as opposed tokeyPress
). Perhaps in all cases where they are custom event handlers attached to these native events there should be some checking if the default was prevented.Related and possibly duplicative
Apologies if this issue has already been captured elsewhere in sufficient detail.
#515
#521
#322
#404
The text was updated successfully, but these errors were encountered: