Skip to content

Commit e9a433d

Browse files
Mariia MarchenkovaMariia Marchenkova
Mariia Marchenkova
authored and
Mariia Marchenkova
committed
test on custom component, refactor
1 parent e6095a3 commit e9a433d

File tree

2 files changed

+61
-57
lines changed

2 files changed

+61
-57
lines changed

src/__tests__/queryByApi.test.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,73 @@ import React from 'react';
33
import { Text, Image } from 'react-native';
44
import { render } from '..';
55

6-
test('queryByText nested <Image> in <Text> at start', () => {
7-
expect(
8-
render(
9-
<Text>
10-
<Image source={{}} />
11-
Hello
12-
</Text>
13-
).queryByText('Hello')
14-
).toBeTruthy();
15-
});
6+
// test('queryByText nested <Image> in <Text> at start', () => {
7+
// expect(
8+
// render(
9+
// <Text>
10+
// <Image source={{}} />
11+
// Hello
12+
// </Text>
13+
// ).queryByText('Hello')
14+
// ).toBeTruthy();
15+
// });
1616

17-
test('queryByText nested <Image> in <Text> at end', () => {
18-
expect(
19-
render(
20-
<Text>
21-
Hello
22-
<Image source={{}} />
23-
</Text>
24-
).queryByText('Hello')
25-
).toBeTruthy();
26-
});
17+
// test('queryByText nested <Image> in <Text> at end', () => {
18+
// expect(
19+
// render(
20+
// <Text>
21+
// Hello
22+
// <Image source={{}} />
23+
// </Text>
24+
// ).queryByText('Hello')
25+
// ).toBeTruthy();
26+
// });
27+
28+
// test('queryByText nested <Image> in <Text> in middle', () => {
29+
// expect(
30+
// render(
31+
// <Text>
32+
// Hello
33+
// <Image source={{}} />
34+
// World
35+
// </Text>
36+
// ).queryByText('HelloWorld')
37+
// ).toBeTruthy();
38+
// });
2739

28-
test('queryByText nested <Image> in <Text> in middle', () => {
40+
// test('queryByText not found', () => {
41+
// expect(
42+
// render(
43+
// <Text>
44+
// Hello
45+
// <Image source={{}} />
46+
// </Text>
47+
// ).queryByText('SomethingElse')
48+
// ).toBeFalsy();
49+
// });
50+
51+
test('queryByText nested multiple <Text> in <Text>', () => {
2952
expect(
3053
render(
3154
<Text>
32-
Hello
33-
<Image source={{}} />
34-
World
55+
Hello{' '}
56+
<Text>
57+
World<Text>!</Text>
58+
</Text>
3559
</Text>
36-
).queryByText('HelloWorld')
60+
).queryByText('Hello World!')
3761
).toBeTruthy();
3862
});
3963

40-
test('queryByText not found', () => {
41-
expect(
42-
render(
43-
<Text>
44-
Hello
45-
<Image source={{}} />
46-
</Text>
47-
).queryByText('SomethingElse')
48-
).toBeFalsy();
49-
});
64+
test('queryByText nested <CustomText> in <Text>', () => {
65+
const CustomText = ({ children }) => {
66+
return <Text>{children}</Text>;
67+
};
5068

51-
test('queryByText nested <Text> in <Text>', () => {
5269
expect(
5370
render(
5471
<Text>
55-
Hello <Text>World!</Text>
72+
Hello <CustomText>World!</CustomText>
5673
</Text>
5774
).queryByText('Hello World!')
5875
).toBeTruthy();

src/helpers/getByAPI.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ const filterNodeByName = (node, name) =>
1515
typeof node.type !== 'string' &&
1616
(node.type.displayName === name || node.type.name === name);
1717

18+
let textContent = [];
19+
1820
const getNodeByText = (node, text) => {
1921
try {
2022
// eslint-disable-next-line
21-
const { Text } = require('react-native');
23+
const { Text } = require('react-native');
24+
textContent = [];
2225
const isTextComponent = filterNodeByType(node, Text);
2326
if (isTextComponent) {
24-
const textChildren = getChildrenAsText(node.props.children);
27+
const textChildren = getChildrenAsText(node.props.children, Text);
2528
if (textChildren) {
2629
const textToTest = textChildren.join('');
2730
return typeof text === 'string'
@@ -35,9 +38,7 @@ const getNodeByText = (node, text) => {
3538
}
3639
};
3740

38-
const getChildrenAsText = children => {
39-
let textContent = [];
40-
41+
const getChildrenAsText = (children, textComponent) => {
4142
React.Children.map(children, child => {
4243
if (typeof child === 'string') {
4344
return textContent.push(child);
@@ -48,21 +49,7 @@ const getChildrenAsText = children => {
4849
}
4950

5051
if (child.props.children) {
51-
const { children } = child.props;
52-
53-
if (children instanceof Array) {
54-
children.forEach(node => {
55-
// eslint-disable-next-line
56-
const { Text } = require('react-native');
57-
if (filterNodeByType(node, Text)) {
58-
return textContent.push(node.props.children);
59-
}
60-
});
61-
} else {
62-
if (typeof children === 'string') {
63-
return textContent.push(children);
64-
}
65-
}
52+
getChildrenAsText(child.props.children, textComponent);
6653
}
6754
});
6855

0 commit comments

Comments
 (0)