Cursor position is changed while using userEvent.keyboard
See original GitHub issueHello, thank you to testing-library team in advance for taking a look at my issue here. I believe this is a bug and would appreciate your help in understanding how to move forward on this issue.
Configuration
{
"@testing-library/jest-dom": "^5.11.10",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^13.1.3",
}
Relevant Code
// SimpleTextarea.js
import React, { useEffect, useState } from 'react';
import { propEq } from 'ramda';
const eventIsEnterKey = propEq('key', 'Enter');
const SimpleTextarea = ({
onChange = () => {},
onEnter = () => {},
placeholder,
value: parentValue,
}) => {
const [ value, setValue ] = useState(parentValue);
useEffect(() => {
if (parentValue === value) return
setValue(parentValue)
}, [ parentValue ])
const handleOnEnter = () => {
onEnter(value);
}
return (
<textarea
placeholder={placeholder}
value={value}
onChange={e => {
setValue(e.target.value)
onChange(e.target.value)
}}
onKeyDown={
e => {
if (eventIsEnterKey(e)) {
e.preventDefault()
handleOnEnter()
}
}
}
/>
)
}
export default SimpleTextarea;
// SimpleTextarea.spec.js
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import SimpleTextarea from './SimpleTextarea';
function setup(params = {}) {
const {
placeholder,
value,
} = params;
const mockOnChange = jest.fn();
const mockOnEnter = jest.fn();
const component = render(
<SimpleTextarea
placeholder={placeholder}
value={value}
onChange={mockOnChange}
onEnter={mockOnEnter}
/>
);
return {
component,
mockOnChange,
mockOnEnter,
}
};
describe('SimpleTextarea', () => {
it('does not change the cursor position while user is typing', () => {
const placeholder = 'Write a comment...';
setup({ placeholder })
const commentInput = screen.getByPlaceholderText(placeholder)
expect(commentInput.selectionStart).toBe(0)
userEvent.type(commentInput, 'acd')
expect(commentInput.selectionStart).toBe(3)
commentInput.setSelectionRange(1, 1)
expect(commentInput.selectionStart).toBe(1)
userEvent.keyboard('b')
expect(commentInput).toHaveDisplayValue('abcd')
expect(commentInput.selectionStart).toBe(2)
})
})
Expectation
I expect that the cursor position would be 2 after the user hits the character ‘b’ immediately after the selectionStart had been set to 1.
Actual
The cursor position was set to 4 instead.
Reproduction repository
https://github.com/codepath2019/user-event-bug-report or you can reference this PR commit, https://github.com/codepath2019/user-event-bug-report/commit/2ff6d970d8216a038e68502ee23a228830864024
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Cursor is moving after input #636 - testing-library/user-event
I'm trying to test that cursor is preserved between inputs, ... You should use userEvent.keyboard if you want to just simulate pressing ...
Read more >user-event v13 - Testing Library
Simulates the keyboard events described by text . This is similar to userEvent.type() but without any clicking or changing the selection range.
Read more >How to get text cursor position after keypress event happened?
Is there an event that is fired after the caret position has changed and not only on input? Or do I have to...
Read more >user-event - Bountysource
Cursor position is changed while using userEvent.keyboard $ 0. Created 1 year ago in testing-library/user-event with 1 comments. Hello, thank you ...
Read more >Move Cursor - UiPath Documentation Portal
TerminalMoveCursor Moves the cursor position to a specified location. Properties Position Row - The row position where the cursor will be moved.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@all-contributors add @codepath2019 bug
🎉 This issue has been resolved in version 13.1.8 🎉
The release is available on:
npm package (@latest dist-tag)
Your semantic-release bot 📦🚀