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: added new rule prefer-in-document #95

Merged
merged 18 commits into from
Nov 24, 2020
Merged

feat: added new rule prefer-in-document #95

merged 18 commits into from
Nov 24, 2020

Conversation

AntonNiklasson
Copy link
Contributor

@AntonNiklasson AntonNiklasson commented Nov 15, 2020

What:

Create a new rule: prefer-in-document

Why:

Instead of asserting that a node exists in the DOM using .toHaveLength, we could suggest to the user that they use .toBeInTheDocument(). This rule was requested in #75.

Checklist:

  • Documentation
  • Tests
  • Ready to be merged

@AntonNiklasson
Copy link
Contributor Author

I'll keep on working on this to add fixability, improve some edge cases etc. Just didn't have enough time today.

README.md Outdated Show resolved Hide resolved
src/queryNames.js Outdated Show resolved Hide resolved
@AntonNiklasson
Copy link
Contributor Author

Thanks a lot for the feedback, Ben! I'll get back to you as soon as possible. I haven't written a rule before, so let me know about any best-practices, ideas etc that I might be missing.

@benmonro
Copy link
Member

@AntonNiklasson no worries you're doing great. Appreciate your contributions

},
],
invalid: [
...Object.keys(queries)
Copy link
Member

Choose a reason for hiding this comment

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

actually thinking about this there's a couple changes I think we should make here.

  1. split out the get, query and find queries.
  2. find queries should use await
  3. "query" queries should be checking that .toHaveLength(0) gets reported and fixed to .not.toBeInTheDocument()

Copy link
Contributor Author

@AntonNiklasson AntonNiklasson Nov 18, 2020

Choose a reason for hiding this comment

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

Yeah, I was just looking into the await case. Ah, right. Since the other ones throw instead? I'll look into it 👍

Copy link
Member

Choose a reason for hiding this comment

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

@AntonNiklasson I thought of another couple of use cases: expect(screen.queryByText()).toBeNull() should be changed to .toBeInTheDocument() and .not.toBeNull() should be changed to .toBeInTheDocument() and conversely we should check .toBeDefined() as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good catch! I've just pushed a few changes to split the queries. I've also added some failing test cases for queryBy* and queryAllBy*. I'll get back to passing the tests in a day or two.

Copy link
Contributor Author

@AntonNiklasson AntonNiklasson Nov 22, 2020

Choose a reason for hiding this comment

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

I've implemented some more logic to support more matchers than just toHaveLength now. Also the negation flipping from .not.toBeNull to toBeInTheDocument. Perhaps there's more matchers I haven't thought of?

@AntonNiklasson
Copy link
Contributor Author

Please have another look @benmonro . I've made the logic a bit more generic to support more cases than just toHaveLength. We could expand it even further if needed of course.

@AntonNiklasson AntonNiklasson marked this pull request as ready for review November 22, 2020 20:03
@codecov
Copy link

codecov bot commented Nov 23, 2020

Codecov Report

Merging #95 (d182646) into master (aeb80dc) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #95   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        12    +2     
  Lines          186       218   +32     
  Branches        22        27    +5     
=========================================
+ Hits           186       218   +32     
Impacted Files Coverage Δ
src/queries.js 100.00% <100.00%> (ø)
src/rules/prefer-in-document.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 aeb80dc...d182646. Read the comment docs.

@benmonro
Copy link
Member

@all-contributors please add @AntonNiklasson for code, tests and docs

@allcontributors
Copy link
Contributor

@benmonro

I've put up a pull request to add @AntonNiklasson! 🎉

package.json Outdated Show resolved Hide resolved
@benmonro benmonro changed the title feat: Add prefer-in-document feat: added new rule prefer-in-document Nov 24, 2020
@benmonro benmonro merged commit f12acee into testing-library:master Nov 24, 2020
@github-actions
Copy link

🎉 This PR is included in version 3.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@AntonNiklasson
Copy link
Contributor Author

I was sleeping, didn't see your comments until now 😴 Thanks a lot for merging this, Ben! I've really enjoyed working on this. Let me know if I can help with something else.

@benmonro
Copy link
Member

benmonro commented Nov 24, 2020 via email

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