-
Notifications
You must be signed in to change notification settings - Fork 470
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
Fix null labels on hidden inputs #804
Conversation
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
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 Report
@@ Coverage Diff @@
## master #804 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 26
Lines 719 719
Branches 184 184
=========================================
Hits 719 719
Continue to review full report at Codecov.
|
There was a problem hiding this 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([]) |
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this 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
src/label-helpers.js
Outdated
if (element.labels !== undefined) return element.labels | ||
|
There was a problem hiding this comment.
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?
if (element.labels !== undefined) return element.labels | |
if (element.labels !== undefined) return element.labels ?? [] | |
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
@all-contributors please add @thomasmarshall for code and tests |
I've put up a pull request to add @thomasmarshall! 🎉 |
No worries, thanks! |
🎉 This PR is included in version 7.26.5 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
What:
This PR ensures
*LabelText
helpers do not throw an error when there are hidden inputs.Why:
The
labels
property oninput
elements of typehidden
isnull
rather thanNodeList
. This meant thegetRealLabels
function would returnnull
causingqueryAllByLabelText
to throw an error when it tried to calllength
onnull
.I noticed this issue when using
cy.findByLabelText
(from @testing-library/cypress) – which fails with the following exception on pages with hidden inputs:How:
This fixes the issue by ensuring the element is labelable before calling
labels
on it. There was already anisLabelable
guard clause in place, but it was only checked after returningelement.labels
(which isnull
for hidden inputs). Alternative solutions might be to add a different guard clause to return an empty array ifelement.labels
isnull
, or to check the return value fromgetRealLabels
before callinglength
on it.Checklist:
docs site N/A