-
Notifications
You must be signed in to change notification settings - Fork 147
New Rule: Disallow DOM exploration properties #62
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
This is a really good idea! I guess this rule should prevent some of Node properties from being used but we need to agree which ones (I guess those returning another Node). I love the idea of restricting level of traversing them, but I think we need to think about how we want to config this: for all the properties? by individual properties? both? The rule could be named like |
That's a good name. I'm not familiar with rule creation but I have seen that rule configuration can become problematic. What if the rule begins with no configuration and then configuration is added based on input after release? I think starting with a default list of properties that return another node is safe. I don't think traverse level should be set by property. |
I'm happy with restricting all the rules returning another node for first approach (without enabling the rule by default in any shareable config) and the we see what we do. How should the traverse level work then? |
The traverse level can be an integer property, similar to complexity. This could help with adoption as the level is gradually decreased while making fixes in a codebase. Maybe we are looking at two different rules? One that handles traverse level at a minimum of one and another that disallows all properties but also allows whitelisting? |
Mmm I like the idea about splitting this in two different rules. What do you think @emmenko @thomlom ? |
Sure we can give it a try. I'm still trying to wrap my head around the workings of the rule but maybe when I see some concrete examples things will be clearer. |
That sounds good to me too but I would go with one rule only. Having two rules would be more confusing as they're closely related. Configuration-wise, we could have something like:
|
@thomlom Fair enough, your proposal seems best approach for now. @jsphstls do you want to give this rule a try? |
I cannot promise enough time or expertise, so it is open to anyone really. This got more momentum than I expected 😅 |
Anyone for this issue? 🙂 |
Hey guys! So, if I've understood correctly, this rule should throw an error message when the user tries to select a DOM Node by traversing through multiple Nodes. The config would be set as
Some examplesincorrect
correct
Did I get it right? Are there any other considerations regarding the implementation of this rule or anyone already working on it? If not, I'd like to give it a try :) |
Wouldn't this rule be caught with the no-container rule (#177)? Besides this, I think you nailed it @thebinaryfelix |
@thebinaryfelix you are right, but keep in mind this applies to every element returned from a Testing Library query, not just the The idea here then is to report things like: const buttonText = screen.getByText('submit');
const button = buttonText.closest('button'); That's why we discussed about how many chained levels we should allow. |
This one is probably the trickiest and complex one in the plugin, as the are tons of things to keep in mind:
const buttonText = screen.getByText('submit');
const button = buttonText.closest('button');
// or
const button = screen.getByText('submit').closest('button');
// or many others you probably can think of now
const buttons = screen.getAllByRole('button');
// this should be reported
buttons[0].firstChild;
// this should be reported too
const buttonA = button[1];
expect(buttonA.lastChild).toBeInTheDocument();
|
Yes! The "container" I mentioned was a poor example name, sorry about that, but what I meant was a Node in general. Basically we would have to:
I think that with this concept in mind we can get to those variations of ways of accessing a Node. |
@thebinaryfelix That sounds perfect, I've assigned it to you then. In the original discussion we decided to:
I think these 2 details will simplify the first approach. Let me know if you want to clarify something else or write down the specific methods the rule should report. |
@Belco90 great, thanks! I think it's all well explained now and these two details will definitely simplify the approach. If I have any questions after starting the implementation I'll let you know. |
This will be released together with v4 |
🎉 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 📦🚀 |
This is not a great way to select elements, but is allowed:
parentElement.parentElement.parentElement
It would be great if there were an optional rule that discouraged the use of properties such as
parentElement
,previousSibling
, and the rest.The rule could be highly configurable by allowing each property key to be enabled or disabled. It could also allow a certain level of traversing so that one use of a property is okay, but a chain is not.
The text was updated successfully, but these errors were encountered: