File tree Expand file tree Collapse file tree 2 files changed +65
-6
lines changed Expand file tree Collapse file tree 2 files changed +65
-6
lines changed Original file line number Diff line number Diff line change
1
+ // @flow
2
+ import React from 'react' ;
3
+ import { Text , Image } from 'react-native' ;
4
+ import { render } from '..' ;
5
+
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
+ } ) ;
16
+
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
+ } ) ;
39
+
40
+ test ( 'queryByText not found' , ( ) => {
41
+ expect (
42
+ render (
43
+ < Text >
44
+ Hello
45
+ < Image source = { { } } />
46
+ </ Text >
47
+ ) . queryByText ( 'SomethingElse' )
48
+ ) . toBeFalsy ( ) ;
49
+ } ) ;
Original file line number Diff line number Diff line change @@ -18,14 +18,10 @@ const filterNodeByName = (node, name) =>
18
18
const getNodeByText = ( node , text ) => {
19
19
try {
20
20
// eslint-disable-next-line
21
- const { Text, TextInput } = require ( 'react-native' ) ;
21
+ const { Text } = require ( 'react-native' ) ;
22
22
const isTextComponent = filterNodeByType ( node , Text ) ;
23
23
if ( isTextComponent ) {
24
- const textChildren = React . Children . map (
25
- node . props . children ,
26
- // In some cases child might be undefined or null
27
- child => ( child !== undefined && child !== null ? child . toString ( ) : '' )
28
- ) ;
24
+ const textChildren = getChildrenAsText ( node . props . children ) ;
29
25
if ( textChildren ) {
30
26
const textToTest = textChildren . join ( '' ) ;
31
27
return typeof text === 'string'
@@ -39,6 +35,20 @@ const getNodeByText = (node, text) => {
39
35
}
40
36
} ;
41
37
38
+ const getChildrenAsText = children => {
39
+ return React . Children . map ( children , child => {
40
+ if ( typeof child === 'string' ) {
41
+ return child ;
42
+ }
43
+
44
+ if ( typeof child === 'number' ) {
45
+ return child . toString ( ) ;
46
+ }
47
+
48
+ return '' ;
49
+ } ) ;
50
+ } ;
51
+
42
52
const getTextInputNodeByPlaceholder = ( node , placeholder ) => {
43
53
try {
44
54
// eslint-disable-next-line
You can’t perform that action at this time.
0 commit comments