Skip to content

feat(ByAltText): Always include custom elements#1049

Merged
eps1lon merged 3 commits into
testing-library:mainfrom
SantoJambit:pr/alt-tag
Oct 6, 2021
Merged

feat(ByAltText): Always include custom elements#1049
eps1lon merged 3 commits into
testing-library:mainfrom
SantoJambit:pr/alt-tag

Conversation

@SantoJambit

@SantoJambit SantoJambit commented Oct 5, 2021

Copy link
Copy Markdown
Contributor

What: Made the ByAltText methods tag-agnostic

Why: ByAltText does currently not support <amp-img> (and possibly other custom-elements).

How: by using the existing queryAllByAttribute function and thus dropping the limitation to only 'img,input,area' tags.

Checklist:

Note:
queryAllByAltText and queryAllByAttribute are mostly identical, so I chose to just call queryAllByAttribute.
However queryAllByAltText had an additional call to checkContainerType, which I left in place. Not sure why queryAllByAttribute does not have this check.

Alternatively, I could leave the old logic in place and simply add the tag amp-img to the allowed list, but I think this way is better.

I will create an update PR for the docs when it's clear, that this will be merged.

@codesandbox-ci

codesandbox-ci Bot commented Oct 5, 2021

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 3536716:

Sandbox Source
react-testing-library-examples Configuration

@SantoJambit

Copy link
Copy Markdown
Contributor Author

Sorry, there was a mixup on who was supposed to create a PR for this issue.. feel free to choose between this and #1048

Comment thread src/queries/alt-text.ts Outdated
).filter(node =>
matcher(node.getAttribute('alt'), node, alt, matchNormalizer),
)
return queryAllByAttribute('alt', container, alt, options)

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.

If this PR only targets custom elements then let's restrict it to known and custom elements. Otherwise <p alt /> becomes queryable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added a filter.

@codecov

codecov Bot commented Oct 5, 2021

Copy link
Copy Markdown

Codecov Report

Merging #1049 (3536716) into main (b28586e) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #1049   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines          920       919    -1     
  Branches       284       283    -1     
=========================================
- Hits           920       919    -1     
Flag Coverage Δ
node-12 100.00% <100.00%> (ø)
node-14 100.00% <100.00%> (ø)
node-16.9.1 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/queries/alt-text.ts 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 b28586e...3536716. Read the comment docs.

@eps1lon eps1lon 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.

Do we have a test that e.g. <div alt /> is not queried? If not then we should add one now that the behavior is not trivial anymore.

@eps1lon eps1lon 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.

Looks clean, great job.

Comment thread src/queries/alt-text.ts
@eps1lon

eps1lon commented Oct 6, 2021

Copy link
Copy Markdown
Member

@SantoJambit Could you review the docs change testing-library/testing-library-docs#946 ?

@eps1lon eps1lon added the enhancement New feature or request label Oct 6, 2021
@eps1lon eps1lon changed the title feat: Support all tags for alt-text feat(ByAltText): Always include custom elements Oct 6, 2021
@SantoJambit

Copy link
Copy Markdown
Contributor Author

@eps1lon thanks for the review and the docs PR. I've reviewed the docs PR.

@eps1lon eps1lon merged commit 87d2682 into testing-library:main Oct 6, 2021
@eps1lon

eps1lon commented Oct 6, 2021

Copy link
Copy Markdown
Member

@all-contributors add @SantoJambit for code

@allcontributors

Copy link
Copy Markdown
Contributor

@eps1lon

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

@SantoJambit SantoJambit deleted the pr/alt-tag branch October 7, 2021 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants