12
12
13
13
const React = require ( 'react' ) ;
14
14
let ReactTestRenderer ;
15
+ let Context ;
15
16
16
17
const RCTView = 'RCTView' ;
17
18
const View = props => < RCTView { ...props } /> ;
@@ -20,6 +21,7 @@ describe('ReactTestRendererTraversal', () => {
20
21
beforeEach ( ( ) => {
21
22
jest . resetModules ( ) ;
22
23
ReactTestRenderer = require ( 'react-test-renderer' ) ;
24
+ Context = React . createContext ( null ) ;
23
25
} ) ;
24
26
25
27
class Example extends React . Component {
@@ -40,6 +42,17 @@ describe('ReactTestRendererTraversal', () => {
40
42
< React . unstable_Profiler id = "test" onRender = { ( ) => { } } >
41
43
< ExampleForwardRef qux = "qux" />
42
44
</ React . unstable_Profiler >
45
+ < React . Fragment >
46
+ < React . Fragment >
47
+ < Context . Provider value = { null } >
48
+ < Context . Consumer >
49
+ { ( ) => < View nested = { true } /> }
50
+ </ Context . Consumer >
51
+ </ Context . Provider >
52
+ </ React . Fragment >
53
+ < View nested = { true } />
54
+ < View nested = { true } />
55
+ </ React . Fragment >
43
56
</ View >
44
57
</ View >
45
58
) ;
@@ -61,7 +74,7 @@ describe('ReactTestRendererTraversal', () => {
61
74
62
75
// assert .props, .type and .parent attributes
63
76
const foo = render . root . find ( hasFooProp ) ;
64
- expect ( foo . props . children ) . toHaveLength ( 8 ) ;
77
+ expect ( foo . props . children ) . toHaveLength ( 9 ) ;
65
78
expect ( foo . type ) . toBe ( View ) ;
66
79
expect ( render . root . parent ) . toBe ( null ) ;
67
80
expect ( foo . children [ 0 ] . parent ) . toBe ( foo ) ;
@@ -76,13 +89,15 @@ describe('ReactTestRendererTraversal', () => {
76
89
const hasNullProp = node => node . props . hasOwnProperty ( 'null' ) ;
77
90
const hasVoidProp = node => node . props . hasOwnProperty ( 'void' ) ;
78
91
const hasItselfProp = node => node . props . hasOwnProperty ( 'itself' ) ;
92
+ const hasNestedProp = node => node . props . hasOwnProperty ( 'nested' ) ;
79
93
80
94
expect ( ( ) => render . root . find ( hasFooProp ) ) . not . toThrow ( ) ; // 1 match
81
95
expect ( ( ) => render . root . find ( hasBarProp ) ) . toThrow ( ) ; // >1 matches
82
96
expect ( ( ) => render . root . find ( hasBazProp ) ) . toThrow ( ) ; // >1 matches
83
97
expect ( ( ) => render . root . find ( hasBingProp ) ) . not . toThrow ( ) ; // 1 match
84
98
expect ( ( ) => render . root . find ( hasNullProp ) ) . not . toThrow ( ) ; // 1 match
85
99
expect ( ( ) => render . root . find ( hasVoidProp ) ) . toThrow ( ) ; // 0 matches
100
+ expect ( ( ) => render . root . find ( hasNestedProp ) ) . toThrow ( ) ; // >1 matches
86
101
87
102
// same assertion as .find(), but confirm length
88
103
expect ( render . root . findAll ( hasFooProp , { deep : false } ) ) . toHaveLength ( 1 ) ;
@@ -91,6 +106,7 @@ describe('ReactTestRendererTraversal', () => {
91
106
expect ( render . root . findAll ( hasBingProp , { deep : false } ) ) . toHaveLength ( 1 ) ;
92
107
expect ( render . root . findAll ( hasNullProp , { deep : false } ) ) . toHaveLength ( 1 ) ;
93
108
expect ( render . root . findAll ( hasVoidProp , { deep : false } ) ) . toHaveLength ( 0 ) ;
109
+ expect ( render . root . findAll ( hasNestedProp , { deep : false } ) ) . toHaveLength ( 3 ) ;
94
110
95
111
// note: with {deep: true}, .findAll() will continue to
96
112
// search children, even after finding a match
@@ -100,6 +116,7 @@ describe('ReactTestRendererTraversal', () => {
100
116
expect ( render . root . findAll ( hasBingProp ) ) . toHaveLength ( 1 ) ; // no spread
101
117
expect ( render . root . findAll ( hasNullProp ) ) . toHaveLength ( 1 ) ; // no spread
102
118
expect ( render . root . findAll ( hasVoidProp ) ) . toHaveLength ( 0 ) ;
119
+ expect ( render . root . findAll ( hasNestedProp , { deep : false } ) ) . toHaveLength ( 3 ) ;
103
120
104
121
const bing = render . root . find ( hasBingProp ) ;
105
122
expect ( bing . find ( hasBarProp ) ) . toBe ( bing ) ;
@@ -130,7 +147,7 @@ describe('ReactTestRendererTraversal', () => {
130
147
131
148
expect ( render . root . findAllByType ( ExampleFn ) ) . toHaveLength ( 1 ) ;
132
149
expect ( render . root . findAllByType ( View , { deep : false } ) ) . toHaveLength ( 1 ) ;
133
- expect ( render . root . findAllByType ( View ) ) . toHaveLength ( 8 ) ;
150
+ expect ( render . root . findAllByType ( View ) ) . toHaveLength ( 11 ) ;
134
151
expect ( render . root . findAllByType ( ExampleNull ) ) . toHaveLength ( 2 ) ;
135
152
expect ( render . root . findAllByType ( ExampleForwardRef ) ) . toHaveLength ( 1 ) ;
136
153
@@ -164,4 +181,22 @@ describe('ReactTestRendererTraversal', () => {
164
181
expect ( render . root . findAllByProps ( { baz} ) ) . toHaveLength ( 4 ) ;
165
182
expect ( render . root . findAllByProps ( { qux} ) ) . toHaveLength ( 3 ) ;
166
183
} ) ;
184
+
185
+ it ( 'skips special nodes' , ( ) => {
186
+ const render = ReactTestRenderer . create ( < Example /> ) ;
187
+ expect ( render . root . findAllByType ( React . Fragment ) ) . toHaveLength ( 0 ) ;
188
+ expect ( render . root . findAllByType ( Context . Consumer ) ) . toHaveLength ( 0 ) ;
189
+ expect ( render . root . findAllByType ( Context . Provider ) ) . toHaveLength ( 0 ) ;
190
+
191
+ const expectedParent = render . root . findByProps ( { foo : 'foo' } , { deep : false } )
192
+ . children [ 0 ] ;
193
+ const nestedViews = render . root . findAllByProps (
194
+ { nested : true } ,
195
+ { deep : false } ,
196
+ ) ;
197
+ expect ( nestedViews . length ) . toBe ( 3 ) ;
198
+ expect ( nestedViews [ 0 ] . parent ) . toBe ( expectedParent ) ;
199
+ expect ( nestedViews [ 1 ] . parent ) . toBe ( expectedParent ) ;
200
+ expect ( nestedViews [ 2 ] . parent ) . toBe ( expectedParent ) ;
201
+ } ) ;
167
202
} ) ;
0 commit comments