Skip to content

Selection not cleared when input is focused #1204

@nils4cosee

Description

@nils4cosee

Reproduction example

https://codesandbox.io/p/devbox/ykz7x2?file=%2Fsrc%2FrenderApp.test.js

Prerequisites

  1. Render
<div>Some text</div>
<in
put data-test="input" autofocus>

with javascript

document.querySelector('div').addEventListener('click', () => {
    document.querySelector('input').focus()
    console.log('activeElement', document.activeElement)
    console.log(`selection "${window.getSelection()?.toString()}"`)
})
  1. Select some text out of the "Some text" div by clicking and dragging the mouse inside the div
await user.pointer([
    {target: screen.getByText('Some text'), offset: 1, keys: '[MouseLeft>]'},
    {offset: 8},
    {keys: '[/MouseLeft]'},
  ])
  await new Promise(resolve => setTimeout(resolve, 100))

Expected behavior

In a real browser, when I select text in the "Some text" element, the selection is visible until the mouse-button is released. Then, the mouse-release triggers a "click" event which. The event-handler is invoked, the input-field is focused.

This causes the selection to be removed.

text-select-reproduction

Actual behavior

The in the test-code the click is triggered as in the browser. The input-element is focused.

But the selection is not removed. window.getSelection.toString() still returns the selected string "ome tex"

User-event version

14.5.2

Environment

Dependencies of the reproduction example

dependencies:
@testing-library/dom 8.12.0
@testing-library/jest-dom 5.16.3
@testing-library/user-event 14.5.2

devDependencies:
@vitejs/plugin-react 4.2.1
husky 4.3.8
jsdom 24.0.0
lint-staged 10.5.4
prettier 1.19.1
vite 5.1.1
vitest 1.4.0

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions