You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Example test - not production quality but enough to show intent/* eslint-env jest */importReactfrom'react'import{render,screen}from'@testing-library/react'importuserEventfrom'@testing-library/user-event'importFooComponentfrom'../FooComponent'import{handleDeleteFoo}from'../store/actions/foo'it('calls a function when the delete key is pressed while a component is active',()=>{render(<FooComponent/>)// Clicking on this component makes it activeuserEvent.click(screen.getByTestId('foo-component'))// Once active, listen for a "delete" key to be presseduserEvent.type(screen.getByTestId('foo-component'),'{del}')expect(handleDeleteFoo).toBeCalled()})
What you did:
Our code has a test that is similar to the example above. We are in the process of porting from fireEvent (imported from @testing-library/react) to userEvent from this library.
Previous, working test code would have only one different line. In place of the line starting with userEvent:
We are updating our tests in accordance with the best practices post here.
What happened:
The test throws an error:
TypeError: Cannot read property 'length' of undefined
The trace log shows the error thrown inside the calculateNewDeleteValue() function of this library:
at calculateNewDeleteValue (node_modules/@testing-library/user-event/dist/type.js:444:41)
at handleDel (node_modules/@testing-library/user-event/dist/type.js:619:33)
at node_modules/@testing-library/user-event/dist/type.js:148:29
Reproduction repository:
I believe I can describe the problem and solution without a reproduction repository, but I am happy to provide one if it will help.
Problem description:
The implementation of userEvent.type(element, '{del}') appears to assume that the user will only ever press the Delete or Backspace keys inside of a text input element, so it attempts to read from a text string and returns a new text string.
However, our situation is one where the Delete or Backspace keys is used as a hotkey for a user interface element which is not a text input element. The idea is that people can click on various items (its construction doesn't really matter, imagine a list of divs). The application state "knows" this is the active "selected" item. When a user presses backspace or delete while this item is active, we trigger other logic that removes the item from the list.
This use case appears to not be covered with userEvent.type(element, '{del}').
Suggested solution:
I can think of three options from here:
Do nothing. We continue to use fireEvent in this case, adding code comments to explain why userEvent is not the appropriate solution for this test.
Update userEvent to handle cases where Delete or Backspace is pressed on elements that are not user-input elements.
It's possible there is another preferred solution that we should be aware of, which already covers this use case. If that's so, please let me know!
The text was updated successfully, but these errors were encountered:
@testing-library/user-event
version: 12.2.2[email protected]
@testing-library/[email protected]
[email protected]
Relevant code or config
What you did:
Our code has a test that is similar to the example above. We are in the process of porting from
fireEvent
(imported from@testing-library/react
) touserEvent
from this library.Previous, working test code would have only one different line. In place of the line starting with
userEvent
:We are updating our tests in accordance with the best practices post here.
What happened:
The test throws an error:
The trace log shows the error thrown inside the
calculateNewDeleteValue()
function of this library:Reproduction repository:
I believe I can describe the problem and solution without a reproduction repository, but I am happy to provide one if it will help.
Problem description:
The implementation of
userEvent.type(element, '{del}')
appears to assume that the user will only ever press the Delete or Backspace keys inside of a text input element, so it attempts to read from a text string and returns a new text string.However, our situation is one where the Delete or Backspace keys is used as a hotkey for a user interface element which is not a text input element. The idea is that people can click on various items (its construction doesn't really matter, imagine a list of divs). The application state "knows" this is the active "selected" item. When a user presses backspace or delete while this item is active, we trigger other logic that removes the item from the list.
This use case appears to not be covered with
userEvent.type(element, '{del}')
.Suggested solution:
I can think of three options from here:
fireEvent
in this case, adding code comments to explain whyuserEvent
is not the appropriate solution for this test.userEvent
to handle cases where Delete or Backspace is pressed on elements that are not user-input elements.The text was updated successfully, but these errors were encountered: