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

fix: add message if type/paste function is called with an invalid element #375

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__tests__/type.js
Expand Up @@ -706,3 +706,12 @@ test('typing an invalid input value', () => {
// but the badInput should actually be "true" if the user types "3-3"
expect(element.validity.badInput).toBe(false)
})

test('should give error if we are trying to call type on an invalid element', async () => {
const {element} = setup('<div />')
await expect(() =>
userEvent.type(element, "I'm only a div :("),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"the current element is of type BODY and doesn't have a valid value"`,
)
})
5 changes: 5 additions & 0 deletions src/utils.js
Expand Up @@ -94,6 +94,11 @@ function getActiveElement(document) {

function calculateNewValue(newEntry, element) {
const {selectionStart, selectionEnd, value} = element
if (typeof value === 'undefined') {
throw new TypeError(
`the current element is of type ${element.tagName} and doesn't have a valid value`,
)
}
// can't use .maxLength property because of a jsdom bug:
// https://github.com/jsdom/jsdom/issues/2927
const maxLength = Number(element.getAttribute('maxlength') ?? -1)
Expand Down