From 5812652dfe0a20440c9b7ff5392421fed977f6e7 Mon Sep 17 00:00:00 2001 From: Tyler Haas Date: Tue, 12 Feb 2019 17:33:54 -0700 Subject: [PATCH 1/2] allow queryAllByText to select text nodes text content --- src/__tests__/element-queries.js | 6 ++++++ src/queries.js | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index bc30089a..0fc71317 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -400,6 +400,12 @@ 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) +}) + 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)) } From cd4079a1e872bf95b7767311807ed89589ae178c Mon Sep 17 00:00:00 2001 From: Tyler Haas Date: Tue, 12 Feb 2019 17:46:22 -0700 Subject: [PATCH 2/2] write tests to cover all branches --- src/__tests__/element-queries.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index 0fc71317..266c7c22 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -404,6 +404,7 @@ 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', () => {