question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cursor position is changed while using userEvent.keyboard

See original GitHub issue

Hello, 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. Screen Shot 2021-04-26 at 3 45 09 PM

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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ph-fritschecommented, Apr 29, 2021
0reactions
github-actions[bot]commented, Apr 29, 2021

🎉 This issue has been resolved in version 13.1.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found