user-event icon indicating copy to clipboard operation
user-event copied to clipboard

Keyboard: Press and hold for X events does not set the 'repeated' attribute

Open vespasianvs opened this issue 3 years ago • 1 comments

Reproduction example

https://codesandbox.io/s/xenodochial-ully-ujn1y1?file=/src/App.test.js

Prerequisites

Just run the tests in code sandbox. The for a single Enter Key works (as expected), but the test which should catch repeated events fails.

If you use the keyboard it works, but shows the correct 'repeat' value

Expected behavior

When the notation

userEvent.keyboard("{Enter>10/}")

is used, the first event should have repeat as false, the next 9 should have repeat as true as per the browser

Actual behavior

All of the fired events have repeat set to false.

User-event version

14.1.1

Environment

Testing Library framework:

JS framework:

Test environment:

DOM implementation:

Additional context

No response

vespasianvs avatar Feb 13 '23 13:02 vespasianvs

I was also expecting in having the event.repeat value set to true when using the > operator for simulating a key kept pressed.

A workaround I did was to keep a counter that only increments on keydown and then keyup resets it. Then a condition that checks for event.repeat to be true can be replaced with keyHoldTriggersCount > 1

In the tests a key followed by >2 is enough to simulate the key kept pressed behavior, when testing manually the experience is identical as when using event.repeat, not sure how much reliable this is but at least the tests can now simulate better the user action for this scenario

let keyHoldTriggersCount = 0

document.addEventListener('keydown', () => {
    keyHoldTriggersCount++

    if (keyHoldTriggersCount > 1) {
        // do your thing
    }
})

document.addEventListener('keyup', () => {
    keyHoldTriggersCount = 0
})

LuanScudeler avatar Jul 14 '23 17:07 LuanScudeler