Skip to content

fix: Return closest Text matching a query #489

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

Merged
merged 7 commits into from
Aug 16, 2020
Merged
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
55 changes: 47 additions & 8 deletions src/__tests__/queryByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,68 @@ test('queryByText not found', () => {
).toBeFalsy();
});

test('queryByText nested multiple <Text> in <Text>', () => {
test('queryByText nested text across multiple <Text> in <Text>', () => {
const { queryByText } = render(
<Text nativeID="1">
Hello{' '}
<Text nativeID="2">
World
<Text>!{true}</Text>
</Text>
</Text>
);

expect(queryByText('Hello World!')?.props.nativeID).toBe('1');
});

test('queryByText with nested Text components return the closest Text', () => {
const NestedTexts = () => (
<Text nativeID="1">
<Text nativeID="2">My text</Text>
</Text>
);

const { queryByText } = render(<NestedTexts />);

expect(queryByText('My text')?.props.nativeID).toBe('2');
});

test('queryByText with nested Text components each with text return the lowest one', () => {
const NestedTexts = () => (
<Text nativeID="1">
bob
<Text nativeID="2">My text</Text>
</Text>
);

const { queryByText } = render(<NestedTexts />);

expect(queryByText('My text')?.props.nativeID).toBe('2');
});

test('queryByText nested <CustomText> in <Text>', () => {
const CustomText = ({ children }) => {
return <Text>{children}</Text>;
};

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

test('queryByText nested <CustomText> in <Text>', () => {
test('queryByText nested deep <CustomText> in <Text>', () => {
const CustomText = ({ children }) => {
return <Text>{children}</Text>;
};

expect(
render(
<Text>
Hello <CustomText>World!</CustomText>
<CustomText>Hello</CustomText> <CustomText>World!</CustomText>
</Text>
).queryByText('Hello World!')
).toBeTruthy();
Expand Down
8 changes: 8 additions & 0 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const getChildrenAsText = (children, TextComponent, textContent = []) => {
}

if (child?.props?.children) {
// Bail on traversing text children down the tree if current node (child)
// has no text. In such situations, react-test-renderer will traverse down
// this tree in a separate call and run this query again. As a result, the
// query will match the deepest text node that matches requested text.
if (filterNodeByType(child, TextComponent) && textContent.length === 0) {
return;
}

getChildrenAsText(child.props.children, TextComponent, textContent);
}
});
Expand Down