-
Notifications
You must be signed in to change notification settings - Fork 147
New rule: prefer-user-event #162
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
Comments
Nice proposal Tim.
|
I'd go for a single rule to prefer one over the other. Kent mentions some stuff that you might only be allowed to do with the primitive, perhaps we could let the user configure for those methods that are present in both which ones should be allowed to be used with |
We should keep am eye on this issue for having more info/context testing-library/dom-testing-library#616 (comment) |
That's a good idea @Belco90 |
The merge into |
@nickmccurdy I've read about the recommendation of using I like the idea of the rule, but could you point out some docs or relevant information to further understand the difference between these two? I am not sure if there are stuff that can only be achieved with either, or if the rule should just go with a simple "if using click on fireEvent, recommend using userEvent instead". Thanks |
For the most part, it's adding related event calls from browsers to make tests realistic, along with adding some convenience APIs to make calling these events easier. Overall the goal is to make testing like a user both easier and more accurate than calling low level
For the most part, we should be recommending any rules in |
@Belco90 I added this to the v4 milestone, do you think it's a good idea or would you rather wait until more events are added to |
I'd like to include this, but I'm afraid this could delay v4. Do we have clear then about what we want to include in this rule? If so, I'd include it in v4 as optional. If we have everything but this ready for v4 I'd release it and then include this in a potential v4.1. Sounds good? |
I thought we agreed on adding one new rule that would check all events that should be used with
👍 |
LGTM |
I can take this one during the week if no one else is working on it. I will probably come back with questions as I implement it😅 |
Assigned to you then. Thanks :) |
I was thinking that the dev could configure the methods that they want to allow as an array of strings, and eslint would allow using those methods from both apis( What are your thoughts? |
I may have misunderstood you, but would this mean the user should add all the events they want to use? If that's the case, I don't think that will work because there are overlapping events, e.g. |
My idea was that, by default, I will map some methods from Now, in addition to disabling the rule inline, I was thinking the user could register methods from |
Aaah, got it 👍 |
I think the key point of this PR is how we map methods from This is my first draft. I'm posting it so I can gather feedback about it while I continue developing the whole rule. the export const MappingToUserEvent: Record<string, UserEventMethodsType[]> = {
// blur: ['blur'],
click: ['click', 'type', 'selectOptions', 'deselectOptions'],
change: ['upload', 'type', 'clear', 'selectOptions', 'deselectOptions'],
dblClick: ['dblClick'],
// focus: ['focus'],
// focusin: ['focus'],
// focusout: ['blur'],
input: ['type', 'upload', 'selectOptions', 'deselectOptions', 'paste'],
keyDown: ['type', 'tab'],
keyPress: ['type'],
keyUp: ['type', 'tab'],
mouseDown: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
mouseEnter: ['hover', 'selectOptions', 'deselectOptions'],
mouseLeave: ['unhover'],
mouseMove: ['hover', 'unhover', 'selectOptions', 'deselectOptions'],
mouseOut: ['unhover'],
mouseOver: ['hover', 'selectOptions', 'deselectOptions'],
mouseUp: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
paste: ['paste'],
pointerDown: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
pointerEnter: ['hover', 'selectOptions', 'deselectOptions'],
pointerLeave: ['unhover'],
pointerMove: ['hover', 'unhover', 'selectOptions', 'deselectOptions'],
pointerOut: ['unhover'],
pointerOver: ['hover', 'selectOptions', 'deselectOptions'],
pointerUp: ['click', 'dblClick', 'selectOptions', 'deselectOptions'],
} Those events that do not appear as a key is because they are allowed / don't have a matching method Thoughts? As of how I came up with this list, I basically checked the implementation of each There are other methods such as Let me know your thoughts. Thanks! |
I believe The content of this looks okay to me, but I feel like we could use a more flexible data structure. There's a bit of repetition here where events has the same or very similar dependencies. Perhaps we could use a Map with some arrays of multiple events as keys, or a more normalized data structure. Worst case if it's not possible to change the structure, we could still make the source easier to understand by extracting common arrays into variables. |
The thing about extracting common arrays into variables is naming - I am not sure how to name them, while being next to the method they override makes it easier to spot where do they belong to. I was hoping the TS compiler to compensate for the lack of normalization, and I think this way it's more readable than combining different atomic sub-arrays The userEvent methods are typed like this const UserEventMethods = ['click', 'dblClick', 'type', 'upload', 'clear', 'selectOptions', 'deselectOptions', 'tab', 'hover', 'unhover', 'paste', 'blur', 'focus'] as const
type UserEventMethodsType = typeof UserEventMethods[number] so even though it's not normalized, TS compiler will warn us whenever a name change from the original source, as well as prevent adding values that don't belong there By using it as a map (like I've posted) it also makes it easy to access the value once I found the line that has a I am open to suggestions like the one you mentioned if the rest think it's better. |
Thoughts on the error message? Given we can map the possible events, my first thought was to construct the error message with the possible values to use. For instance
I think that one looks nice. However, for cases with multiple options, it'd look like this
Thoughts? |
Good point about the keys. I guess we could still abstract variables to make the values easier to read. Also if TypeScript widens the array type to something like |
🎉 This issue has been resolved in version 4.0.0-beta.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 4.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
When testing-library/dom-testing-library#616 gets merged,
userEvent
will be part of DOM testing library.We could create rules for events on
userEvent
that are preferred over theirfireEvent
counterparts.Before we start implementing rules, there are some thing we need to clarify first:
Kent already mentioned
click
would be a good candidate, are there any others that could benefit from this rule? I thinkhover
,unhover
could also be included in within this rule.Should it be one rule for all events, or a rule per event? The latter would allow us to disable/enable specific events... but we could also create a config for it...
The text was updated successfully, but these errors were encountered: