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
// testuserEvent.type(input,"123{arrowleft}{arrowleft}{backspace}");constselectionStartValue=screen.getByTestId("selectionStartValue");expect(selectionStartValue).toHaveTextContent("0");// it fails - the value is 2 instead
What you did:
I discovered it when writing a Jest test for a React component which is an input reacting to onChange and relying heavily on the event.target.selectionStart value passed in this event. I used userEvent.type in the test.
What happened:
I noticed that when the cursor is not at the end of the text in the input and backspace is pressed, the value of event.target.selectionStart provided by userEvent.type is different from the value I get in my browser.
In order to see a more tangible example, please see the CodeSandbox project I created and linked below and follow these steps:
Type "abc" in the input
Hit left arrow button on your keyboard twice
Hit backspace
Check the value of event.target.selectionStart
Result in the browser: 0
Result in the test (App.test.js): 2
Problem description:
I think the problem is related to fireInputEventIfNeeded. The value of event.target.selectionStart in fireEvent is incorrect (at least in my case) but it's correct in the setSelectionRange called at the end of the function (line 319). Shouldn't it be called before the event is fired?
@testing-library/user-event
version: 12.4.0react-scripts
, version: 3.4.3)@testing-library/jest-dom
(5.11.6)Relevant code or config
I'm using
useState
here just to be able to display theevent.target.selectionStart
value and demonstate the issue.What you did:
I discovered it when writing a Jest test for a React component which is an input reacting to
onChange
and relying heavily on theevent.target.selectionStart
value passed in this event. I useduserEvent.type
in the test.What happened:
I noticed that when the cursor is not at the end of the text in the input and backspace is pressed, the value of
event.target.selectionStart
provided byuserEvent.type
is different from the value I get in my browser.In order to see a more tangible example, please see the CodeSandbox project I created and linked below and follow these steps:
event.target.selectionStart
Result in the browser: 0
Result in the test (App.test.js): 2
Reproduction repository:
https://codesandbox.io/s/user-event-and-backspace-selectionstart-nx6ij?file=/src/App.test.js
Problem description:
I think the problem is related to
fireInputEventIfNeeded
. The value ofevent.target.selectionStart
infireEvent
is incorrect (at least in my case) but it's correct in thesetSelectionRange
called at the end of the function (line 319). Shouldn't it be called before the event is fired?user-event/src/type.js
Lines 295 to 324 in e9dd0a6
Suggested solution:
As in the problem description. I'm happy to make a pull request if you confirm my reasoning is correct.
The text was updated successfully, but these errors were encountered: