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

type causes InvalidStateError with <input type="email"> #316

Closed
kelvinlzhang opened this issue Jun 5, 2020 · 16 comments · Fixed by #322
Closed

type causes InvalidStateError with <input type="email"> #316

kelvinlzhang opened this issue Jun 5, 2020 · 16 comments · Fixed by #322
Labels

Comments

@kelvinlzhang
Copy link

kelvinlzhang commented Jun 5, 2020

Occurs in master of @testing-library/user-event version: 11.2.1

Here's an example failing test, not really up to this library's standards but I wanted to get as close to the source as possible.

STR:

  1. Add the following block to src/__tests__/type.js
const create = () => {
    const utils = render(<input aria-label="input" type="email"/>)
    const input = utils.getByLabelText('input')
    return {
        input,
        ...utils,
    }
}

it('types text in email input', async () => {
    const { input } = create()
    await userEvent.type(input, 'testing@gmail.com')
    expect(input.value).toBe('testing@gmail.com')
})
  1. Run test
  2. InvalidStateError

What happened: I wanted to follow up on this comment and reproduce: #307 (comment). Sorry @kentcdodds, the type= was abstracted away and it was pretty silly of me to assume it was text. Seems like I ran into 2 completely different issues now, but hopefully I'm just missing something obvious.

Test fails on InvalidStateError, adding an onChange handler with a console.log(event.target.value) shows that type only enters in the first char before breaking.

@tpict
Copy link

tpict commented Jun 5, 2020

I'm seeing the same behavior for type="number" with valid input

@kelvinlzhang
Copy link
Author

kelvinlzhang commented Jun 5, 2020

Yeah, something funky going on with validated character input types.

@kentcdodds
Copy link
Member

Thanks for this @kelvinlzhang! Would you be interested in digging in and figuring out the issue?

@kentcdodds
Copy link
Member

@tpict you're welcome to try as well 😁

@kelvinlzhang
Copy link
Author

kelvinlzhang commented Jun 5, 2020

@kentcdodds error is caused by setSelectionRange on line 32

https://stackoverflow.com/questions/21177489/selectionstart-selectionend-on-input-type-number-no-longer-allowed-in-chrome

Seems like a lot of people aren't too happy about this :(

@tpict
Copy link

tpict commented Jun 5, 2020

Looks like user-event handles this elsewhere by simply switching out the type of the input for the function's lifetime. Would that be a reasonable fix here, or is it off the table on account of type being asynchronous?

@kentcdodds
Copy link
Member

That's what I was thinking. Want to give it a try?

@Meemaw
Copy link
Contributor

Meemaw commented Jun 7, 2020

This is the same problem as fixed in #262 for the "clear" function. The fix function should probably be extracted and reused.

@nvh95
Copy link

nvh95 commented Jun 7, 2020

While making the dev tools to automate some tasks for my application, I'm seeing the same problem while trying to use userEvent.type on an input with type="email".
Bellow is my element

<div>
  <label>Email</label>
  <input
    type="email"
    data-testid="input-email"
  />
</div>

And my script to fill the email

await userEvent.type(
  await screen.findByTestId('input-email'),
  'myemail@example.com',
);

Only one character is inserted
image

And in Chrome Console I get this error
image

The problem faded away when I switch type from 'email' to 'text'

@kentcdodds
Copy link
Member

@all-contributors please add @kelvinlzhang for bugs

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @kelvinlzhang! 🎉

@kentcdodds
Copy link
Member

@all-contributors please add @tpict for bugs, code, and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @tpict! 🎉

@kentcdodds
Copy link
Member

@all-contributors please add @nvh95 for bugs

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @nvh95! 🎉

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 11.3.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

5 participants