Skip to content

Support textareas with children #344

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

Merged
merged 1 commit into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,10 @@ test('get/query textarea element by current value', () => {
expect(getByDisplayValue('World').id).toEqual('content-textarea')
expect(queryByDisplayValue('World').id).toEqual('content-textarea')
})

test('can get a textarea with children', () => {
const {getByLabelText} = renderIntoDocument(`
<label>Label<textarea>Value</textarea></label>
`)
getByLabelText('Label')
})
14 changes: 11 additions & 3 deletions src/queries/label-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ function queryAllLabelsByText(
) {
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
return Array.from(container.querySelectorAll('label')).filter(label =>
matcher(label.textContent, label, text, matchNormalizer),
)
return Array.from(container.querySelectorAll('label')).filter(label => {
let textToMatch = label.textContent

// The children of a textarea are part of `textContent` as well. We
// need to remove them from the string so we can match it afterwards.
label.querySelectorAll('textarea').forEach(textarea => {
textToMatch = textToMatch.replace(textarea.value, '')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm... What if the label is the same as the text value at that moment?

Copy link
Member

Choose a reason for hiding this comment

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

I think we should be able to use label.childNodes[0].textContent here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexkrolick replace only replaces the first occurence:

'abcabc'.replace('abc', '') // "abc"

Therefore this should be fine.

@timdeschryver Sorry, I can't follow? Generally I think it's good to avoid childNodes so we support arbitrary nesting for both the textarea and the label. There could be some divs and spans wrapped around for example.

})

return matcher(textToMatch, label, text, matchNormalizer)
})
}

function queryAllByLabelText(
Expand Down