From a3a13e2194ecd3fdfdb479011ac33775f9499837 Mon Sep 17 00:00:00 2001 From: Lachlan Tweedie <30222060+lachiet@users.noreply.github.com> Date: Fri, 5 Feb 2021 19:11:43 +1100 Subject: [PATCH 1/2] add fragment support for matching text --- src/__tests__/queryByApi.test.js | 14 ++++++++++++++ src/helpers/getByAPI.js | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/__tests__/queryByApi.test.js b/src/__tests__/queryByApi.test.js index 82e89bf6e..c8314ba40 100644 --- a/src/__tests__/queryByApi.test.js +++ b/src/__tests__/queryByApi.test.js @@ -114,3 +114,17 @@ test('queryByText nested deep in ', () => { ).queryByText('Hello World!') ).toBeTruthy(); }); + +test('queryByText nested in ', () => { + const CustomText = () => { + return Hello World; + }; + + expect( + render( + + + + ).queryByText('Hello World') + ).toBeTruthy(); +}); diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index 41e74bdd5..923219455 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -56,6 +56,14 @@ const getNodeByText = (node, text) => { : text.test(textToTest); } } + + // Fragments + 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); From 2acd5fd6d21d63485601403dad397fb329d0cd64 Mon Sep 17 00:00:00 2001 From: Lachlan Tweedie <30222060+lachiet@users.noreply.github.com> Date: Fri, 5 Feb 2021 20:21:31 +1100 Subject: [PATCH 2/2] filter down to children of text elements --- src/__tests__/queryByApi.test.js | 16 +++++++++++++++- src/helpers/getByAPI.js | 17 ++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/__tests__/queryByApi.test.js b/src/__tests__/queryByApi.test.js index c8314ba40..e6241833d 100644 --- a/src/__tests__/queryByApi.test.js +++ b/src/__tests__/queryByApi.test.js @@ -1,6 +1,6 @@ // @flow import * as React from 'react'; -import { Text, Image } from 'react-native'; +import { Text, Image, View } from 'react-native'; import { render } from '..'; test('queryByText nested in at start', () => { @@ -128,3 +128,17 @@ test('queryByText nested in ', () => { ).queryByText('Hello World') ).toBeTruthy(); }); + +test('queryByText nested in ', () => { + const CustomText = () => { + return Hello World; + }; + + expect( + render( + + + + ).queryByText('Hello World') + ).toBeFalsy(); +}); diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index 923219455..69884187d 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -41,7 +41,7 @@ export type GetByAPI = {| getAllByPlaceholder: () => void, |}; -const filterNodeByType = (node, type) => node.type === type; +const filterNodeByType = (node, type) => node?.type === type; const getNodeByText = (node, text) => { try { @@ -57,13 +57,16 @@ const getNodeByText = (node, text) => { } } - // Fragments - if (typeof node.children?.[0] === 'string') { - const textToTest = node.children.join(''); - return typeof text === 'string' - ? text === textToTest - : text.test(textToTest); + 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);