Skip to content

feat: Add check for container type on queries#604

Merged
kentcdodds merged 1 commit into
testing-library:masterfrom
Jnforja:pr/base-queries-check-container-type
Jun 5, 2020
Merged

feat: Add check for container type on queries#604
kentcdodds merged 1 commit into
testing-library:masterfrom
Jnforja:pr/base-queries-check-container-type

Conversation

@Jnforja

@Jnforja Jnforja commented Jun 4, 2020

Copy link
Copy Markdown
Contributor

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

codesandbox-ci Bot commented Jun 4, 2020

Copy link
Copy Markdown

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

codecov Bot commented Jun 4, 2020

Copy link
Copy Markdown

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.

@kentcdodds kentcdodds left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

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

@allcontributors

Copy link
Copy Markdown
Contributor

@kentcdodds

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

@kentcdodds

Copy link
Copy Markdown
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.

2 participants