From e3deeb4d58d2e6014cc3e229e8e9be522667383b Mon Sep 17 00:00:00 2001 From: Alvin Delito Date: Fri, 17 Dec 2021 14:13:44 -0800 Subject: [PATCH] fix: add React.Fragment support for matching text --- src/__tests__/byText.test.js | 14 ++++++++++++++ src/helpers/byText.js | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index b64b81c76..516afb09c 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -176,6 +176,20 @@ test.skip('getByText works properly with custom text container', () => { ).toBeTruthy(); }); +test('getByText works properly with React.Fragment nested in ', () => { + function FragmentText({ children }) { + return {children}; + } + + const { getByText, queryByText } = render( + + Hello + + ); + expect(getByText('Hello')).toBeTruthy(); + expect(queryByText('Hello')).not.toBeNull(); +}); + test('queryByText nested in at start', () => { expect( render( diff --git a/src/helpers/byText.js b/src/helpers/byText.js index 11de1113c..1201a8f7e 100644 --- a/src/helpers/byText.js +++ b/src/helpers/byText.js @@ -57,6 +57,18 @@ const getNodeByText = ( return matches(text, textToTest, normalizer, exact); } } + if (node?.parent?.parent) { + const isParentTextComponent = filterNodeByType(node.parent?.parent, Text); + if (isParentTextComponent && !isTextComponent) { + if (typeof node.children?.[0] === 'string') { + const textToTest = node.children.join(''); + return typeof text === 'string' + ? text === textToTest + : text.test(textToTest); + } + } + } + return false; } catch (error) { throw createLibraryNotSupportedError(error);