Skip to content

Commit 677f4e9

Browse files
Tyler HaasKent C. Dodds
authored andcommitted
fix: allow queryAllByText to select text nodes text content (#209)
* allow queryAllByText to select text nodes text content * write tests to cover all branches
1 parent 7e9a726 commit 677f4e9

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/__tests__/element-queries.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,13 @@ test('queryAll* matchers return an array for 0 matches', () => {
400400
expect(queryAllByRole('nope')).toHaveLength(0)
401401
})
402402

403+
test('queryAllByText can query dom nodes', () => {
404+
const {queryAllByText} = render('hi')
405+
expect(queryAllByText('hi')).toHaveLength(1)
406+
expect(queryAllByText('not here')).toHaveLength(0)
407+
expect(queryAllByText('hi', {selector: 'span'})).toHaveLength(0)
408+
})
409+
403410
test('using jest helpers to assert element states', () => {
404411
const {queryByTestId} = render(`<span data-testid="count-value">2</span>`)
405412

src/queries.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ function queryAllByText(
105105
) {
106106
const matcher = exact ? matches : fuzzyMatches
107107
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
108-
return Array.from(container.querySelectorAll(selector))
108+
const baseArray = container.matches(selector) ? [container] : []
109+
return Array.from([...baseArray, ...container.querySelectorAll(selector)])
109110
.filter(node => !ignore || !node.matches(ignore))
110111
.filter(node => matcher(getNodeText(node), node, text, matchNormalizer))
111112
}

0 commit comments

Comments
 (0)