Skip to content

.type doesn't work with vue-testing-library #119

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

Closed
afontcu opened this issue May 13, 2019 · 18 comments
Closed

.type doesn't work with vue-testing-library #119

afontcu opened this issue May 13, 2019 · 18 comments

Comments

@afontcu
Copy link
Member

afontcu commented May 13, 2019

Hi! First of all, thank you for your work on this repo!

I wanted to highlight that the current implementation of .type() isn't working when used with vue-testing-library.

Not quite sure why yet thou, I believe it has something to do with using fireEvent.change instead of fireEvent.input here. I have passing tests using fireEvent.input, and the minute I change them to use .change, they start failing. Is there any reason to prefer change event over input?

Thanks!

@Gpx
Copy link
Member

Gpx commented May 15, 2019

Hi, user-event is only tested with React at the moment for the simple reason that it's what I use at work. I would be keen to add tests that cover other frameworks.

I think the difference between input and change is that in React you have two separate handlers for them: onInput and onChange. I also never seen anyone using onInput.

I don't really know how it works in Vue-land. Is it possible that you have this distinction too?

@afontcu
Copy link
Member Author

afontcu commented May 15, 2019

Hi! Thanks for answering.

Vue also maps native events such as Input and Change. However, input is the most widely used in Vue world. It is even used in the built-in v-model feature, which is just syntax sugar to create a two-way binding of an input value.

@Gpx
Copy link
Member

Gpx commented May 17, 2019

Since we're trying to simulate a browser I think we should fire both events instead of replacing it.
Right now type is firing keydown -> change -> keyup. We should change it to be keydown -> input -> change -> keyup

@afontcu
Copy link
Member Author

afontcu commented May 17, 2019

AFAIK (and correct me if I'm wrong), change should only be emitted after moving away from the input.

Note: The input event is fired every time the value of the element changes. This is unlike the change event, which only fires when the value is committed, such as by pressing the enter key, selecting a value from a list of options, and the like. (source)

If that's the case, would it make sense send an input event for every char, and then send a change event with the whole string?

keydown
input "h"
keyup
keydown
input "i"
keyup
change "hi"

@Gpx
Copy link
Member

Gpx commented May 17, 2019

AFAIK (and correct me if I'm wrong), change should only be emitted after moving away from the input.

That's what happens for native HTML but not in React. I think in React change and input are behaving the same except React will fire both of them

@afontcu
Copy link
Member Author

afontcu commented May 17, 2019

Oh wow you're right, I didn't know that, thanks.

I feel Vue works similary to native HTML (see demo) so yeah, an input event would do the trick :)

I'll happily submit a PR if you feel that that's the apropiate change (no pun intended).

@Gpx
Copy link
Member

Gpx commented May 17, 2019

You man a PR to replace the vent or to add it?

@afontcu
Copy link
Member Author

afontcu commented May 28, 2019

(sorry for the late response!)

Whatever works best in both frameworks. I guess we could add it, so the workflow would be keydown -> input -> change -> keyup.

@Gpx
Copy link
Member

Gpx commented Jun 6, 2019

Do you think you could add tests for Vue? Basically, porting the ones we have but render Vue components instead of React. I think we need to have those in place before we start thinking about cross-library support

@afontcu
Copy link
Member Author

afontcu commented Jun 6, 2019

Makes total sense! Will work on that :)

@Gpx
Copy link
Member

Gpx commented Jun 6, 2019

Thank you 👍

@calebeby
Copy link
Contributor

calebeby commented Jul 5, 2019

Whatever works best in both frameworks. I guess we could add it, so the workflow would be keydown -> input -> change -> keyup.

Can I open a PR to add this? It doesn't look like anyone else got to it yet 👀

@afontcu
Copy link
Member Author

afontcu commented Jul 5, 2019

Hi @calebeby! As @Gpx suggested, I opened a (draft) PR to add Vue tests #129 to user-event, which will create a solid starting point to develop cross-library features :)

@calebeby
Copy link
Contributor

calebeby commented Jul 5, 2019

@afontcu Awesome! I have local changes that make the input and change events fire like they do when a user types, so I'll pull in your new tests soon!

I might also add tests for vanilla JS. I'm using Preact, which, unlike React, actually adds a change event listener when you use onChange. So AFAIK Preact will work the same as Vue does ✔️

I think we need to have those in place before we start thinking about cross-library support

LOL I missed this line while I was skimming through this issue 🙈

I was able to modify this library locally to make onInput work correctly in Preact (tests on master still pass ✅):

  • Replace fireEvent.change references with fireEvent.input within the type function
  • At the end of the type method after all the letters are typed in, attach a blur listener to the element. When that fires, call fireEvent.change. This slightly differs from native browser behavior because it fires regardless of whether the contents are different.

@mxstbr
Copy link

mxstbr commented Jul 19, 2019

@calebeby I am also using Preact, any chance that PR is somewhere? 🙏

@calebeby
Copy link
Contributor

@Gpx do you think you could review my 4 open PR's pretty soon? I'd love to continue moving forward with making user-event compatible with all libraries 🎉

@mxstbr Opened PR for that just now ✔️ #141

@afontcu
Copy link
Member Author

afontcu commented Aug 27, 2019

I guess this can be closed now :)

@afontcu afontcu closed this as completed Aug 27, 2019
@dilraj-vidyard
Copy link

I am still having issues typing things into inputs and editiblecontents.

I made a comment here: #141 (comment)

These are the packages I'm using to do typing and to render my component:

import userEvent from '@testing-library/user-event';
import { mount } from '@vue/test-utils';

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

No branches or pull requests

5 participants