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.

The type event doesn't work on elements without a value property

See original GitHub issue
  • @testing-library/user-event version: 12.1.10
  • Testing Framework and version: jest 24.1.0
  • DOM Environment: react-dom 16.13.1

Relevant code or config

import React from 'react'
import '@testing-library/jest-dom'
import userEvent from '@testing-library/user-event'
import {render, screen} from '@testing-library/react'
import {useHotkeys} from 'react-hotkeys-hook'

function Counter() {
  const [count, setCount] = React.useState(0)
  useHotkeys('ctrl+k', () => setCount(prevCount => prevCount + 1))

  return <>Pressed {count} times.</>
}

test('increment counter', async () => {
  render(<Counter />)
  const count = screen.getByText('Pressed 0 times.')
  userEvent.type(document.body, '{ctrl}k{/ctrl}')

  expect(count).toHaveTextContent('Pressed 1 times.')
})

Reproduction repository

https://codesandbox.io/s/blissful-waterfall-p4fi4?file=/src/index.test.js

Problem description

The library doesn’t seem to support the type event on elements that don’t have a value property. This prevents testing scenarios with global keyboard shortcuts that trigger effects. For instance, in the above sandbox, we increment the counter when hitting ctrl+k (bound to document.body) as you can see in the Browser tab (make sure the document is focused).

However, the test using userEvent.type doesn’t pass (see Tests tab). It does with fireEvent.keyDown.

Is it in the vision of user-event to support this, or should it remain a fireEvent scenario? And if the latter, what is the rationale?

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
kentcdoddscommented, Nov 3, 2020

I think we should support type on non-value elements 👍

1reaction
kentcdoddscommented, Nov 3, 2020

I can’t remember 😅 so I guess we can just go forward.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property 'value' does not exist on type EventTarget in TypeScript
The $event is now a specific KeyboardEvent . Not all elements have a value property so it casts target to an input element....
Read more >
How to fix property not existing on EventTarget in TypeScript
This error occurs when you try to access a property on an event target in TypeScript. Property 'value' does not exist on type...
Read more >
Solved: TS "Property '…' does not exist on type 'EventTarget'"
Assuming that the event target is an HTML input element, we can fix this error like so: // Property 'form' does not exist...
Read more >
Property 'value' does not exist on type 'EventTarget' Error in ...
This error is flagged by the TypeScript compiler. The $event in both examples are of type HTMLElement . It can represent any HTML...
Read more >
Property 'value' does not exist on type 'EventTarget'. #6879
The error says event.target doesn't have a property named value , which is weird.
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