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

chore: add option to get specific query suggestions #627

Merged
merged 1 commit into from Jun 13, 2020

Conversation

smeijer
Copy link
Member

@smeijer smeijer commented Jun 13, 2020

1 - Add support for getting specific query method suggestions

What:
I've added an option to specify the desired method to the getSuggestedQuery method.

Why:
To enable tools (such as https://testing-playground.com) to show specific suggestions instead of only the most recommended one.

How:
getSuggestedQuery accepted the arguments element, variant, I've extended that to element, variant, method.

Method defaults to "best", but if specified, only the requested query will be suggested, or none at all.

This enables us to use it like:

getSuggestedQuery(input, 'get', 'PlaceholderText'));

If that call results in a valid suggestion, the suggestion is returned otherwise it will return undefined. It will not fall back to alternative methods!

The default behavior of the method has been left unchanged. A call like:

getSuggestedQuery(input, 'get'));

Still returns either the best query available, or undefined.

2 - added getByTestId suggestion

Sorry, I should learn to limit the scope of pull-requests. Even when they are tiny.

While working on the above, I noticed that a suggestion for getByTestId wasn't implemented. Testing-playground will render it red! But when there are really no other options, this query should still be suggested. It is part of the library after all.

So, I've also added the getByTestId suggestion, as last option available.

Checklist:

  • Documentation added to the
    docs site
  • Tests
  • Typescript definitions updated
  • Ready to be merged

@smeijer smeijer self-assigned this Jun 13, 2020
@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ae31e42:

Sandbox Source
agitated-margulis-giomk Configuration

@codecov
Copy link

codecov bot commented Jun 13, 2020

Codecov Report

Merging #627 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #627   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines          559       565    +6     
  Branches       140       142    +2     
=========================================
+ Hits           559       565    +6     
Impacted Files Coverage Δ
src/suggestions.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b9900f8...ae31e42. Read the comment docs.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks!

@kentcdodds kentcdodds merged commit 30a1ee8 into master Jun 13, 2020
@kentcdodds kentcdodds deleted the feature/add-suggestion-method-specifier branch June 13, 2020 13:55
@kentcdodds
Copy link
Member

🎉 This PR is included in version 7.14.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nickmccurdy
Copy link
Member

nickmccurdy commented Jun 13, 2020

Great idea, thank you! I'm interested if there's any followup work we can do to provide more suggestions to users, especially in the linter plugins.

@smeijer
Copy link
Member Author

smeijer commented Jun 14, 2020

especially in the linter plugins.

I'm not entirely the right person to answer that question. But as far as I understand linters and understood from a Twitter thread, that will be a hard thing to do. Which is why @testing-library/dom can now throw when it detects better query options.

The thing is, linters verify on a line-by-line basis. They don't eval the code. And @testing-library/dom doesn't show the user all the options, it only suggests the best. Which I believe is a good thing.

So, that's entirely why we have testing-playground (soon to be official! ❤️)

But maybe @benmonro has an idea on how to get more out of the suggestions?

@marcosvega91
Copy link
Member

Maybe we can add a new method that will return a list of suggestions ordered by relevance ?

@smeijer
Copy link
Member Author

smeijer commented Jun 14, 2020

Do you have a specific use case in mind?

I'm not sure if a function like this belongs to @testing-library/dom. As this project only suggests the best query available, and not all queries that are possible.

The base functionality that you describe is also quite trivial for other libraries to build themselves, by just using getSuggestedQuery. The more advanced case (that takes the entire dom context into account) is a bit harder, but also moves more out of the scope of this project.

@marcosvega91
Copy link
Member

I wrote that but I agree with you, testing library shouldn't do this

@benmonro
Copy link
Member

@nickmccurdy what did you have in mind? As Stephan mentioned the lint plugin can only analyze code statically. In order to make a suggestion you need to inspect a live dom.

I am still curious though because I do love writing lint rules. 🙂

@benmonro
Copy link
Member

@kentcdodds @smeijer if we're adding test id to this shouldn't it check config to rather than hard coding to data-testid attribute? Not that big of a deal but our team uses an alternate attribute

@smeijer
Copy link
Member Author

smeijer commented Jun 14, 2020

@benmonro, you're right. We should use the config for that.

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.

None yet

5 participants