Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sarahdayan opened this issue Nov 3, 2020 · 6 comments
Closed
Labels
enhancement New feature or request

Comments

@sarahdayan
Copy link
Contributor

sarahdayan commented Nov 3, 2020

  • @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?

@kentcdodds
Copy link
Member

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

@kentcdodds kentcdodds added enhancement New feature or request help wanted labels Nov 3, 2020
@sarahdayan
Copy link
Contributor Author

Yay! Is there context on why it's currently unsupported? The change looks trivial but if I go there I'd like to make sure I'm not missing something.

@kentcdodds
Copy link
Member

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

@sethreidnz
Copy link

I've been looking for somewhere to make a contrib to one of these @testing-library packages and this looks good! I understand the problem, and I'm starting to get my head around how the code works and how to contribute... Is anyone else working on this or if I have time should I get something up?

My understanding of the problem is that right now the current implementation only really handles an as the element we are typing on. But we want to be able to handle the fact that typing is the way the user does hotkeys right?

Let me know if there is anything else I should know. This is my first time trying to contribute to open source so I may be slow to get going :-)

@kentcdodds
Copy link
Member

@sethreidnz I don't think anyone's working on it. You can feel free to do so.

I think your understanding is correct. I think the easiest way to approach this would be to add a test that does: userEvent.type(document.body, '{alt}t{/alt}{backspace}{delete}') or something like that. This test will fail and making that test pass will probably be sufficient.

When you're finished, it might make sense to refactor the test into a few separate use cases. Whatever feels right to you is probably fine.

Thanks!

@ph-fritsche
Copy link
Member

ph-fritsche commented Mar 16, 2021

Resolved in v13.0.0 🚀

https://codesandbox.io/s/charming-maxwell-0z65j?file=/src/counter.js

The useHotkeys hook still fails because it relies on the deprecated keyCode property.
If you want to use it, PRs extending the defaultKeyMap to support this for legacy code are welcome. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants