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: Add check for container type on queries #604

Conversation

Jnforja
Copy link
Contributor

@Jnforja Jnforja commented Jun 4, 2020

What:
Add check for container type on queries

Why:
So users of this library get an informative warning when
the first argument of a base-query is not an appropriate container.

How:

  1. Created a helper function that does a runtime check on the type of a container.
  2. On every queryAllBy* variant called the new helper function with the given container.

Checklist:

  • Documentation added to the
    docs site N/A
  • I've prepared a PR for types targeting
    DefinitelyTyped N/A
  • Tests
  • Ready to be merged

Some decisions I'd like to point out for review:

  • As the base-queries are part of the public API, and to not risk breaking clients, I took a more relaxed approach to type checking containers and only check for the properties querySelector and querySelectorAll, instead of going for something more restrictive like nodeType.
  • Although I'm not 100% sure, from the code I read, the types that seem appropriate to be containers are Element, Document, and DocumentFragment. So those were the ones that I referenced in the error message.
  • Initially, I added the helper function to the query-helpers.js file as query related functions are the only ones to use it. But then I remembered that query-helpers.js is part of the public API, so I ended up moving the helper function to helpers.js as it seemed like the best place yet.

Please let me know if there's anything that I should change in this PR and I'll get to it :)

Ref: #537

This is intended to give an informative warning when
a base-query is called with a container that's neitheir
an Element, Document or DocumentFragment.

Ref: testing-library#537
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 4, 2020

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 19f1acd:

Sandbox Source
damp-moon-k4kw7 Configuration

@codecov
Copy link

codecov bot commented Jun 4, 2020

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #604   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines          537       550   +13     
  Branches       134       137    +3     
=========================================
+ Hits           537       550   +13     
Impacted Files Coverage Δ
src/helpers.js 100.00% <100.00%> (ø)
src/queries/alt-text.js 100.00% <100.00%> (ø)
src/queries/display-value.js 100.00% <100.00%> (ø)
src/queries/label-text.js 100.00% <100.00%> (ø)
src/queries/placeholder-text.js 100.00% <100.00%> (ø)
src/queries/role.js 100.00% <100.00%> (ø)
src/queries/test-id.js 100.00% <100.00%> (ø)
src/queries/text.js 100.00% <100.00%> (ø)
src/queries/title.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 af7392f...19f1acd. 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.

Super 🤘 thanks!

@kentcdodds kentcdodds merged commit 16e41ac into testing-library:master Jun 5, 2020
@kentcdodds
Copy link
Member

@all-contributors please add @Jnforja for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

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

@kentcdodds
Copy link
Member

🎉 This PR is included in version 7.10.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.

None yet

2 participants