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

feat: new rule prefer-presence-queries #98

Merged
merged 10 commits into from
Mar 28, 2020
Merged

Conversation

Belco90
Copy link
Member

@Belco90 Belco90 commented Mar 22, 2020

This closes #90

BREAKING CHANGE: previous rule no-get-by-for-checking-element-not-present has been replaced by this one.

@Belco90 Belco90 self-assigned this Mar 23, 2020
@Belco90 Belco90 added BREAKING CHANGE This change will require a major version bump new rule New rule to be included in the plugin labels Mar 23, 2020
Copy link
Member

@benmonro benmonro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some minor typos and a couple use cases we should probably address, but overall looking good, thank you for doing this!

docs/rules/prefer-presence-queries.md Outdated Show resolved Hide resolved
docs/rules/prefer-presence-queries.md Outdated Show resolved Hide resolved
docs/rules/prefer-presence-queries.md Show resolved Hide resolved
docs/rules/prefer-presence-queries.md Show resolved Hide resolved
lib/rules/prefer-presence-queries.js Outdated Show resolved Hide resolved
docs/rules/prefer-presence-queries.md Show resolved Hide resolved
@Belco90 Belco90 force-pushed the feature/prefer-presence-queries branch from d9f3757 to 7aeab95 Compare March 28, 2020 18:46
@Belco90
Copy link
Member Author

Belco90 commented Mar 28, 2020

@benmonro after thinking about it again, I don't think this case should be reported:

const button = screen.queryByText("button");
expect(button).toBeInTheDocument()

This is not so simple to identify as vars containing the dom elements are updated dynamically. Take a look at this example:
image

There is a really simple component with a text and a button. When you click the button, the text disappear. In the test (which you can see it's passing) I'm getting the message with getByText and saving it within a var to check it's present, this is fine. But then I'm checking with the same var that the element is not present after clicking the button. That element was get with a getBy* query and I'm checking that it's not present so this rule would complain from the case you mentioned above. But same var and query have been used to check is present in a different line. Should this rule complain then? If so, the user would have to switch the query to queryBy* one and then the rule would complain about the opposite: using a queryBy* for checking element is present.

This seems like a vicious circle, so I don't think this rule should check queries values saved in vars for its first implementation as it adds lots of complexity.

The current PR is the final implementation then.

@benmonro
Copy link
Member

@Belco90 hmm yeah I see what you mean. What if we just check for it's use immediately after the query? Reason I ask is that it gives people a way to work around the rule which is what I'm trying to prevent...

@Belco90
Copy link
Member Author

Belco90 commented Mar 28, 2020

@Belco90 hmm yeah I see what you mean. What if we just check for it's use immediately after the query? Reason I ask is that it gives people a way to work around the rule which is what I'm trying to prevent...

I'm pretty sure there are others ways to skip this and other rules. There are different ways it could be solved, but I'm not sure is worth the effort for now. Even checking if it's used immediately after the query has lots of variants. Would you report this?

// they are not used immediately after their declaration
const button = screen.queryByText("button");
const submit = screen.queryByText('submit');
expect(button).toBeInTheDocument();
expect(submit).toBeInTheDocument();

Or this?

// not used immediately either
const button = screen.queryByText("button");
expect(button).toHaveClass('hidden');
expect(button).toBeInTheDocument();

As you can see, tons of variants would take a while to cover. This could be discussed after this first approach and release it as a minor release.

@benmonro
Copy link
Member

@Belco90 ok fair enough this is definitely better so yeah let's ship it and see what happens

@Belco90 Belco90 merged commit a958a3a into v3 Mar 28, 2020
@Belco90 Belco90 deleted the feature/prefer-presence-queries branch March 28, 2020 19:45
Belco90 added a commit that referenced this pull request Mar 29, 2020
feat(await-async-utils): reflect waitFor changes (#89)
feat: new rule no-wait-for-empty-callback (#94)
feat: new rule prefer-wait-for (#88)
feat: new rule prefer-screen-queries (#99)
BREAKING CHANGE: drop support for node v8. Min version allowed is node v10.12 (#96)
BREAKING CHANGE: rule `no-get-by-for-checking-element-not-present` removed in favor of new rule `prefer-presence-queries` (#98)

Closes #85
Closes #86
Closes #90
Closes #92
Closes #95

Co-authored-by: timdeschryver <28659384+timdeschryver@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE This change will require a major version bump new rule New rule to be included in the plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: convert no-get-by-for-checking-element-not-present to be prefer-appropriate-query-for-expect
3 participants