File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -436,3 +436,36 @@ describe('Supports normalization', () => {
436
436
expect ( getByText ( 'A text' , { normalizer : normalizerFn } ) ) . toBeTruthy ( ) ;
437
437
} ) ;
438
438
} ) ;
439
+
440
+ test ( 'getByText and queryByText work properly with text nested in React.Fragment' , ( ) => {
441
+ const { getByText, queryByText } = render (
442
+ < Text >
443
+ < > Hello</ >
444
+ </ Text >
445
+ ) ;
446
+ expect ( getByText ( 'Hello' ) ) . toBeTruthy ( ) ;
447
+ expect ( queryByText ( 'Hello' ) ) . not . toBeNull ( ) ;
448
+ } ) ;
449
+
450
+ test ( 'getByText and queryByText work properly with text partially nested in React.Fragment' , ( ) => {
451
+ const { getByText, queryByText } = render (
452
+ < Text >
453
+ He< > llo</ >
454
+ </ Text >
455
+ ) ;
456
+ expect ( getByText ( 'Hello' ) ) . toBeTruthy ( ) ;
457
+ expect ( queryByText ( 'Hello' ) ) . not . toBeNull ( ) ;
458
+ } ) ;
459
+
460
+ test ( 'getByText and queryByText work properly with multiple nested fragments' , ( ) => {
461
+ const { getByText, queryByText } = render (
462
+ < Text >
463
+ He
464
+ < >
465
+ l< > l</ > o
466
+ </ >
467
+ </ Text >
468
+ ) ;
469
+ expect ( getByText ( 'Hello' ) ) . toBeTruthy ( ) ;
470
+ expect ( queryByText ( 'Hello' ) ) . not . toBeNull ( ) ;
471
+ } ) ;
Original file line number Diff line number Diff line change @@ -30,11 +30,15 @@ const getChildrenAsText = (children, TextComponent) => {
30
30
// has no text. In such situations, react-test-renderer will traverse down
31
31
// this tree in a separate call and run this query again. As a result, the
32
32
// query will match the deepest text node that matches requested text.
33
- if ( filterNodeByType ( child , TextComponent ) && textContent . length === 0 ) {
33
+ if ( filterNodeByType ( child , TextComponent ) ) {
34
34
return ;
35
35
}
36
36
37
- getChildrenAsText ( child . props . children , TextComponent ) ;
37
+ if ( filterNodeByType ( child , React . Fragment ) ) {
38
+ textContent . push (
39
+ ...getChildrenAsText ( child . props . children , TextComponent )
40
+ ) ;
41
+ }
38
42
}
39
43
} ) ;
40
44
You can’t perform that action at this time.
0 commit comments