Skip to content

feat(prefer-find-by): make the rule fixable #153

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

Merged

Conversation

gndelia
Copy link
Collaborator

@gndelia gndelia commented Jun 9, 2020

This feature extends prefer-find-by rule to make it fixable

One doubt I had was whether to fix or not this scenario

waitFor(() => query*)

as queryBy* methods do not throw if not found, but findBy* do. At first I thought of not changing the behavior, but then I consider that if null was always returned, eventually waitFor would timeout, throwing as well (just like the findBy* method would)

@Belco90
Copy link
Member

Belco90 commented Jun 10, 2020

waitFor + queryBy* should be the same as findBy too. As far as I know, waitFor will wait until the query returns an element, so null (from queryBy) or error (from getBy) are both considered as still waiting for element, ending up with the same behavior under waitFor.

So I think that scenario should be fixed too.

context.report({
node,
messageId: "preferFindBy",
data: { queryVariant, queryMethod, fullQuery },
fix(fixer) {
const newCode = `${caller ? `${caller}.` : ''}${queryVariant}${queryMethod}(${callArguments.map((node) => sourceCode.getText(node)).join(', ')})`
Copy link
Member

Choose a reason for hiding this comment

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

Damn, even checking the test I can't figure out how this replaces waitFor + getBy with findBy

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

does that mean is it too obscure? 🤣

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry, I just realized queryVariant is the corresponding findBy from getFindByQueryVariant, rather than the original query variant found in the code as I thought. Could you add a simple jsdoc to reportInvalidUsage to clarify what args are expected? Probably would be a good idea to add more jsdoc around the plugin.

Copy link
Member

Choose a reason for hiding this comment

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

Do you want me to write a suggestion for this jsdoc? We need something simple, not elaborated description necessary here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As you want. I can do it, but I will probably do it in the weekend as I have some work to do 😄

Copy link
Member

Choose a reason for hiding this comment

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

No problem, there is no rush around this!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!
Hovering the method shows the docs:

image

Copy link
Member

Choose a reason for hiding this comment

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

Awesome! It wasn't even necessary to re-add the type, but thanks!

@gndelia
Copy link
Collaborator Author

gndelia commented Jun 10, 2020

waitFor + queryBy* should be the same as findBy too. As far as I know, waitFor will wait until the query returns an element, so null (from queryBy) or error (from getBy) are both considered as still waiting for element, ending up with the same behavior under waitFor.

So I think that scenario should be fixed too.

yeah, I felt the same. The PR fixes that scenario as well 🎉

context.report({
node,
messageId: "preferFindBy",
data: { queryVariant, queryMethod, fullQuery },
fix(fixer) {
const newCode = `${caller ? `${caller}.` : ''}${queryVariant}${queryMethod}(${callArguments.map((node) => sourceCode.getText(node)).join(', ')})`
Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry, I just realized queryVariant is the corresponding findBy from getFindByQueryVariant, rather than the original query variant found in the code as I thought. Could you add a simple jsdoc to reportInvalidUsage to clarify what args are expected? Probably would be a good idea to add more jsdoc around the plugin.

@gndelia gndelia force-pushed the feature/prefer-find-by-automatic-fix branch from bee49db to f8050b9 Compare June 12, 2020 21:46
Copy link
Member

@Belco90 Belco90 left a comment

Choose a reason for hiding this comment

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

Ready to go! 🚀

context.report({
node,
messageId: "preferFindBy",
data: { queryVariant, queryMethod, fullQuery },
fix(fixer) {
const newCode = `${caller ? `${caller}.` : ''}${queryVariant}${queryMethod}(${callArguments.map((node) => sourceCode.getText(node)).join(', ')})`
Copy link
Member

Choose a reason for hiding this comment

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

Awesome! It wasn't even necessary to re-add the type, but thanks!

@Belco90 Belco90 changed the title feat: make prefer-find-by rule fixable feat(prefer-find-by): make the rule fixable Jun 13, 2020
@Belco90 Belco90 merged commit 7ab72d1 into testing-library:master Jun 13, 2020
@Belco90
Copy link
Member

Belco90 commented Jun 13, 2020

🎉 This PR is included in version 3.3.0 🎉

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 this pull request may close these issues.

2 participants