Skip to content

Use innerText rather than textContent for label queries. #190

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

Closed
wants to merge 4 commits into from
Closed
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
37 changes: 37 additions & 0 deletions src/__tests__/elements-inside-label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// query utilities:
import {getByLabelText} from '../'
import document from './helpers/document'

function getExampleDOM() {
const div = document.createElement('div')
div.innerHTML = `
<label>Cars
<select>
<option>Volvo</option>
<option>Mercedes</option>
</select>
</label>
<label><span>States</span>
<select>
<option>Damaged</option>
<option>Good</option>
</select>
</label>
<label><input type="checkbox" value="1"> I agree to terms </label>
`
return div
}

test('elements inside label', () => {
const container = getExampleDOM()

const element1 = getByLabelText(container, 'Cars')
expect(element1.tagName).toBe('SELECT')

// this fails because getNodeText function returns incorrect result on JSDOM but it good for browser
// const element2 = getByLabelText(container, 'States');
// expect(element2.tagName).toBe('SELECT');
Copy link
Author

Choose a reason for hiding this comment

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

This lines added temporarily added until switching jest to karma.

Copy link
Member

Choose a reason for hiding this comment

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

This lines added temporarily added until switching jest to karma.

Wait.... Are you attempting to migrate our tests to karma? If so then please don't take the time, it will not be merged.

If you want to add a few tests in karma and leave the existing tests as they are that's fine. The bill of the tests should be in jest, but I'd be happy to have a few tests that run in a real browser with karma.

Just want to make sure we're clear on expectations here.

Copy link
Author

Choose a reason for hiding this comment

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

Ok I understand.


const element3 = getByLabelText(container, 'I agree to terms')
expect(element3.value).toBe('1')
})
20 changes: 12 additions & 8 deletions src/get-node-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ function getNodeText(node) {
const window = node.ownerDocument.defaultView

if (node.matches('input[type=submit], input[type=button]')) {
return node.value;
return node.value
}

return Array.from(node.childNodes)
.filter(
child =>
child.nodeType === window.Node.TEXT_NODE && Boolean(child.textContent),
)
.map(c => c.textContent)
.join('')
return (
node.innerText ||
Array.from(node.childNodes)
.filter(
child =>
child.nodeType === window.Node.TEXT_NODE &&
Boolean(child.textContent),
)
.map(c => c.textContent)
.join('')
)
}

export {getNodeText}
2 changes: 1 addition & 1 deletion src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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),
matcher(getNodeText(label), label, text, matchNormalizer),
Copy link
Member

Choose a reason for hiding this comment

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

Don't forget that you'll need to update the getNodeText function to use innerText 👍

)
}

Expand Down