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

Fix null labels on hidden inputs #804

Merged
merged 2 commits into from
Nov 3, 2020
Merged

Fix null labels on hidden inputs #804

merged 2 commits into from
Nov 3, 2020

Conversation

thomasmarshall
Copy link
Contributor

What:

This PR ensures *LabelText helpers do not throw an error when there are hidden inputs.

Why:

The labels property on input elements of type hidden is null rather than NodeList. This meant the getRealLabels function would return null causing queryAllByLabelText to throw an error when it tried to call length on null.

I noticed this issue when using cy.findByLabelText (from @testing-library/cypress) – which fails with the following exception on pages with hidden inputs:

Timed out retrying: Cannot read property 'length' of null

How:

This fixes the issue by ensuring the element is labelable before calling labels on it. There was already an isLabelable guard clause in place, but it was only checked after returning element.labels (which is null for hidden inputs). Alternative solutions might be to add a different guard clause to return an empty array if element.labels is null, or to check the return value from getRealLabels before calling length on it.

Checklist:

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

The `labels` property on `input` elements of type `hidden` is `null`
rather than `NodeList` [1]. This meant the `getRealLabels` function
would return `null` causing `queryAllByLabelText` to throw an error when
it tried to call `length` on `null` [2].

This commit fixes the issue by ensuring the element is labelable before
calling `labels` on it, and adds a test case for this specific scenario.

[1]: https://html.spec.whatwg.org/multipage/forms.html#dom-lfe-labels
[2]: https://github.com/testing-library/dom-testing-library/blob/62f4e5e09a4b81ef66679560b540523edccdef98/src/queries/label-text.js#L52
@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 3, 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.

@codecov
Copy link

codecov bot commented Nov 3, 2020

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #804   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           26        26           
  Lines          719       719           
  Branches       184       184           
=========================================
  Hits           719       719           
Impacted Files Coverage Δ
src/label-helpers.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 62f4e5e...a6605d1. 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.

Thanks for the contribution! One question please.

test('hidden inputs are not labelable', () => {
const element = document.createElement('input')
element.type = 'hidden'
expect(getRealLabels(element)).toEqual([])
Copy link
Member

Choose a reason for hiding this comment

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

What was the result before your changes? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before the change it returns null:

Screenshot 2020-11-03 at 17 44 01

Copy link
Member

Choose a reason for hiding this comment

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

I didn't see the PR and I have opened a PR on the same behaviour because I saw an issue about it.

Copy link
Member

@marcosvega91 marcosvega91 left a comment

Choose a reason for hiding this comment

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

Thanks! :) I have added a small request

Comment on lines 39 to 40
if (element.labels !== undefined) return element.labels

Copy link
Member

Choose a reason for hiding this comment

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

Is better to do in this way instead of swap the two functions?

Suggested change
if (element.labels !== undefined) return element.labels
if (element.labels !== undefined) return element.labels ?? []

Copy link
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 squash commit with your suggestion – thanks @marcosvega91.

This commit fixes the issue by retuning an empty array if the `labels`
property is `null`, and adds a test case for this specific scenario.
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.

Thank you very much! I apologize for not reading your original post more carefully. Had I done that, I wouldn't have needed you to take the time to make the screenshot 🤦‍♂️

I blame covid.

Thanks!

@kentcdodds kentcdodds merged commit 6ef63b7 into testing-library:master Nov 3, 2020
@kentcdodds
Copy link
Member

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

@allcontributors
Copy link
Contributor

@kentcdodds

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

@thomasmarshall
Copy link
Contributor Author

No worries, thanks!

@testing-library-bot
Copy link

🎉 This PR is included in version 7.26.5 🎉

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.

4 participants