diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index bc30089a..266c7c22 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -400,6 +400,13 @@ test('queryAll* matchers return an array for 0 matches', () => { expect(queryAllByRole('nope')).toHaveLength(0) }) +test('queryAllByText can query dom nodes', () => { + const {queryAllByText} = render('hi') + expect(queryAllByText('hi')).toHaveLength(1) + expect(queryAllByText('not here')).toHaveLength(0) + expect(queryAllByText('hi', {selector: 'span'})).toHaveLength(0) +}) + test('using jest helpers to assert element states', () => { const {queryByTestId} = render(`2`) diff --git a/src/queries.js b/src/queries.js index e5393222..15c7233c 100644 --- a/src/queries.js +++ b/src/queries.js @@ -105,7 +105,8 @@ function queryAllByText( ) { const matcher = exact ? matches : fuzzyMatches const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer}) - return Array.from(container.querySelectorAll(selector)) + const baseArray = container.matches(selector) ? [container] : [] + return Array.from([...baseArray, ...container.querySelectorAll(selector)]) .filter(node => !ignore || !node.matches(ignore)) .filter(node => matcher(getNodeText(node), node, text, matchNormalizer)) }