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.

`userEvent.type` with selection range behaves unexpectedly when run in a browser

See original GitHub issue

Hello again 👋 I’m seeing inconsistent behavior with userEvent.type being run in the test vs. in the browser outside of the test. For some reason, the typing with selection range example from the docs works in the JS test runner, but doesn’t work in the actual browser.

  • @testing-library/user-event version: 13.1.8
  • @testing-library/react version: 11.2.5

Relevant code or config

import React from “react”;
import { render, screen } from “@testing-library/react”;
import userEvent from “@testing-library/user-event”;

render(
  <div>
    <label htmlFor=“my-input”>Example:</label>
    <input id=“my-input” type=“text” defaultValue=“This is a bad example” />
  </div>
);
const input = screen.getByLabelText(/example/i);
input.setSelectionRange(10, 13);
userEvent.type(input, “good”);

What I did: I took the userEvent.type with selection range example from the docs and ran it in a js file outside a test.

What happened: The resulting input value was supposed to be "this is a good example", but instead it was "doothis is a g example":

Screen Shot 2021-05-06 at 12 03 53 PM

Reproduction repository:

Sandbox link

Suggested solution: Based on stepping through the debugger, I think the issue is related to these lines in setSelectionRange. It seems like selection.addRange(range) might not be working as expected so then selection range is cleared to zero after selection.removeAllRanges(). I’m not sure how to solve this though.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
github-actions[bot]commented, May 12, 2021

🎉 This issue has been resolved in version 13.1.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

1reaction
ph-fritschecommented, May 10, 2021

Thanks a lot for the report. ❤️

It looks like our implementation is based on a false premise. If you select inside the input, the browser sets the selection on the parent <div>.

When you type into the input, the selection range is not changed at all.

https://codesandbox.io/s/rtl-selection-range-example-forked-221xl?file=/src/App.js

We should skip setting the range on Selection for <input> and <textarea>.

Read more comments on GitHub >

github_iconTop Results From Across the Web

userEvent.type doesn't work well when a value is formatted ...
Once I did so for a while I came across the idea that it may be an issue with the selected location returning...
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 >
Reference Manual - CodeMirror
Create a set of changes and a new selection by running the given function for each range in the active selection. The function...
Read more >
9 React Testing Library Tips and Tricks | by Paige Niedringhaus
And write multiple tests to test one component. The user-event library “tries to simulate the real events that would happen in the browser...
Read more >
Alexa.Presentation.APL Interface Reference | Alexa Skills Kit
The Alexa.Presentation.APL interface provides directives and requests that display content on a device with a screen, such as an Echo Show.
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