Skip to content

fix: add React.Fragment support for matching text #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/__tests__/queryByApi.test.js
Original file line number Diff line number Diff line change
@@ -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 <Image> in <Text> at start', () => {
Expand Down Expand Up @@ -114,3 +114,31 @@ test('queryByText nested deep <CustomText> in <Text>', () => {
).queryByText('Hello World!')
).toBeTruthy();
});

test('queryByText nested <React.Fragment> in <Text>', () => {
const CustomText = () => {
return <React.Fragment>Hello World</React.Fragment>;
};

expect(
render(
<Text>
<CustomText />
</Text>
).queryByText('Hello World')
).toBeTruthy();
});

test('queryByText nested <React.Fragment> in <View>', () => {
const CustomText = () => {
return <React.Fragment>Hello World</React.Fragment>;
};

expect(
render(
<View>
<CustomText />
</View>
).queryByText('Hello World')
).toBeFalsy();
});
13 changes: 12 additions & 1 deletion src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -56,6 +56,17 @@ const getNodeByText = (node, text) => {
: text.test(textToTest);
}
}

const isParentTextComponent = filterNodeByType(node.parent?.parent, Text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking parents non-recursively will result in a failure in e.g. this scenario:

const Wrap = ({ children }) => <>{children}</>;

  const CustomText = () => {
    return <React.Fragment>Hello World</React.Fragment>;
  };

  expect(
    render(
      <Text>
        <Wrap>
          <CustomText />
        </Wrap>
      </Text>
    ).queryByText('Hello World')
  ).toBeTruthy();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check this PR as well: #554 as it's changing the way we're interfacing text to be similar to React Testing Library. Grabbing by text with non-direct string children is tricky and we'll likely need to trade-off some functionality, or come up with a more complex solution.

We want to solve this problem once for good, so if you're willing to help improve the library, please chime in there and see how we can support this scenario with string precision API

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, #554 is now merged. Would you be still interested in pursuing this improvement? 😊

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);
Expand Down