Skip to content

Fix getByTestId nested custom text edge case #516

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
51 changes: 51 additions & 0 deletions src/__tests__/getByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,54 @@ test('getByTestId returns only native elements', () => {
'No instances found with testID: myComponent'
);
});

test('getByText with nested native Text component get closest Text', () => {
const NestedText = () => {
return (
<Text testID="outer">
<Text testID="inner">Test</Text>
</Text>
);
};

const { getByText } = render(<NestedText />);

expect(getByText('Test').props.testID).toBe('inner');
});

test('getByText with nested multiple custom Text component get closest Text', () => {
const CustomText = ({ children, ...rest }) => (
<Text {...rest}>{children}</Text>
);

const NestedText = () => {
return (
<CustomText testID="outer">
<CustomText testID="inner1">Test1</CustomText>
<CustomText testID="inner2">Test2</CustomText>
</CustomText>
);
};

const { getByText } = render(<NestedText />);

expect(getByText('Test1').props.testID).toBe('inner1');
});

test('getByText with nested custom Text component get closest Text', () => {
const CustomText = ({ children, ...rest }) => (
<Text {...rest}>{children}</Text>
);

const NestedText = () => {
return (
<CustomText testID="outer">
<CustomText testID="inner">Test</CustomText>
</CustomText>
);
};

const { getByText } = render(<NestedText />);

expect(getByText('Test').props.testID).toBe('inner');
});
14 changes: 14 additions & 0 deletions src/__tests__/queryByApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ test('queryByText nested deep <CustomText> in <Text>', () => {
).queryByText('Hello World!')
).toBeTruthy();
});

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

expect(
render(
<CustomText>
<CustomText>Hello</CustomText> <CustomText>World!</CustomText>
</CustomText>
).queryByText('Hello World!')
).toBeTruthy();
});
23 changes: 14 additions & 9 deletions src/helpers/getByAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ const getNodeByText = (node, text) => {
}
};

const isTextContentType = (candidate) =>
['string', 'number'].includes(typeof candidate);

const getChildrenAsText = (children, TextComponent, textContent = []) => {
React.Children.forEach(children, (child) => {
if (typeof child === 'string') {
textContent.push(child);
return;
}
if (
typeof children === 'object' &&
isTextContentType(children.props?.children)
) {
return textContent;
}

if (typeof child === 'number') {
textContent.push(child.toString());
return;
React.Children.forEach(children, (child) => {
if (isTextContentType(child)) {
textContent.push(child + '');
return textContent;
}

if (child?.props?.children) {
Expand All @@ -49,7 +54,7 @@ const getChildrenAsText = (children, TextComponent, textContent = []) => {
// 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;
return textContent;
}

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