@@ -15,13 +15,13 @@ describe('reactTreeWalker', () => {
15
15
constructor ( props ) {
16
16
super ( props )
17
17
// $FlowIgnore
18
- this . getSomething = this . getSomething . bind ( this )
18
+ this . getData = this . getData . bind ( this )
19
19
}
20
20
21
- getSomething ( ) {
22
- return typeof this . props . something === 'function'
23
- ? this . props . something ( )
24
- : this . props . something
21
+ getData ( ) {
22
+ return typeof this . props . data === 'function'
23
+ ? this . props . data ( )
24
+ : this . props . data
25
25
}
26
26
27
27
render ( ) {
@@ -39,19 +39,19 @@ describe('reactTreeWalker', () => {
39
39
const createTree = async => (
40
40
< div >
41
41
< h1 > Hello World!</ h1 >
42
- < Foo something = { async ? ( ) => resolveLater ( 1 ) : 1 } />
43
- < Foo something = { async ? ( ) => resolveLater ( 2 ) : 2 } >
42
+ < Foo data = { async ? ( ) => resolveLater ( 1 ) : 1 } />
43
+ < Foo data = { async ? ( ) => resolveLater ( 2 ) : 2 } >
44
44
< div >
45
45
< Bob >
46
- < Foo something = { async ? ( ) => resolveLater ( 4 ) : 4 } >
47
- < Foo something = { async ? ( ) => resolveLater ( 5 ) : 5 } />
48
- < Foo something = { async ? ( ) => resolveLater ( 6 ) : 6 } />
46
+ < Foo data = { async ? ( ) => resolveLater ( 4 ) : 4 } >
47
+ < Foo data = { async ? ( ) => resolveLater ( 5 ) : 5 } />
48
+ < Foo data = { async ? ( ) => resolveLater ( 6 ) : 6 } />
49
49
</ Foo >
50
50
</ Bob >
51
51
< div > hi!</ div >
52
52
</ div >
53
53
</ Foo >
54
- < Foo something = { async ? ( ) => resolveLater ( 3 ) : 3 } />
54
+ < Foo data = { async ? ( ) => resolveLater ( 3 ) : 3 } />
55
55
</ div >
56
56
)
57
57
@@ -60,9 +60,9 @@ describe('reactTreeWalker', () => {
60
60
const actual = [ ]
61
61
// eslint-disable-next-line no-unused-vars
62
62
const visitor = ( element , instance , context ) => {
63
- if ( instance && typeof instance . getSomething === 'function' ) {
64
- const something = instance . getSomething ( )
65
- actual . push ( something )
63
+ if ( instance && typeof instance . getData === 'function' ) {
64
+ const data = instance . getData ( )
65
+ actual . push ( data )
66
66
}
67
67
}
68
68
return reactTreeWalker ( tree , visitor ) . then ( ( ) => {
@@ -76,9 +76,9 @@ describe('reactTreeWalker', () => {
76
76
const actual = [ ]
77
77
// eslint-disable-next-line no-unused-vars
78
78
const visitor = ( element , instance , context ) => {
79
- if ( instance && typeof instance . getSomething === 'function' ) {
80
- return instance . getSomething ( ) . then ( something => {
81
- actual . push ( something )
79
+ if ( instance && typeof instance . getData === 'function' ) {
80
+ return instance . getData ( ) . then ( data => {
81
+ actual . push ( data )
82
82
return true
83
83
} )
84
84
}
@@ -95,10 +95,10 @@ describe('reactTreeWalker', () => {
95
95
const actual = [ ]
96
96
// eslint-disable-next-line no-unused-vars
97
97
const visitor = ( element , instance , context ) => {
98
- if ( instance && typeof instance . getSomething === 'function' ) {
99
- return instance . getSomething ( ) . then ( something => {
100
- actual . push ( something )
101
- return something !== 4
98
+ if ( instance && typeof instance . getData === 'function' ) {
99
+ return instance . getData ( ) . then ( data => {
100
+ actual . push ( data )
101
+ return data !== 4
102
102
} )
103
103
}
104
104
return true
@@ -193,8 +193,8 @@ describe('reactTreeWalker', () => {
193
193
render ( ) {
194
194
return (
195
195
< div >
196
- < Foo something = { 1 } />
197
- < Foo something = { 2 } />
196
+ < Foo data = { 1 } />
197
+ < Foo data = { 2 } />
198
198
</ div >
199
199
)
200
200
}
@@ -208,14 +208,68 @@ describe('reactTreeWalker', () => {
208
208
const actual = [ ]
209
209
// eslint-disable-next-line no-unused-vars
210
210
const visitor = ( element , instance , context ) => {
211
- if ( instance && typeof instance . getSomething === 'function' ) {
212
- const something = instance . getSomething ( )
213
- actual . push ( something )
211
+ if ( instance && typeof instance . getData === 'function' ) {
212
+ const data = instance . getData ( )
213
+ actual . push ( data )
214
214
}
215
215
}
216
216
return reactTreeWalker ( tree , visitor ) . then ( ( ) => {
217
217
const expected = [ 1 , 2 ]
218
218
expect ( actual ) . toEqual ( expected )
219
219
} )
220
220
} )
221
+
222
+ describe ( 'error handling' , ( ) => {
223
+ it ( 'throws async visitor errors' , ( ) => {
224
+ const tree = createTree ( true )
225
+ const actual = [ ]
226
+ // eslint-disable-next-line no-unused-vars
227
+ const visitor = ( element , instance , context ) => {
228
+ if ( instance && typeof instance . getData === 'function' ) {
229
+ return instance . getData ( ) . then ( data => {
230
+ actual . push ( data )
231
+ if ( data === 4 ) {
232
+ return Promise . reject ( new Error ( 'Visitor made 💩' ) )
233
+ }
234
+ return true
235
+ } )
236
+ }
237
+ return true
238
+ }
239
+ return reactTreeWalker ( tree , visitor ) . then (
240
+ ( ) => {
241
+ throw new Error ( 'Expected error was not thrown' )
242
+ } ,
243
+ err => {
244
+ expect ( err ) . toMatchObject ( new Error ( 'Visitor made 💩' ) )
245
+ expect ( actual ) . toEqual ( [ 1 , 2 , 4 ] )
246
+ } ,
247
+ )
248
+ } )
249
+
250
+ it ( 'throws sync visitor errors' , ( ) => {
251
+ const tree = createTree ( false )
252
+ const actual = [ ]
253
+ // eslint-disable-next-line no-unused-vars
254
+ const visitor = ( element , instance , context ) => {
255
+ if ( instance && typeof instance . getData === 'function' ) {
256
+ const data = instance . getData ( )
257
+ actual . push ( data )
258
+ if ( data === 4 ) {
259
+ throw new Error ( 'Visitor made 💩' )
260
+ }
261
+ }
262
+ return true
263
+ }
264
+ return reactTreeWalker ( tree , visitor ) . then (
265
+ ( ) => {
266
+ throw new Error ( 'Expected error was not thrown' )
267
+ } ,
268
+ err => {
269
+ expect ( err ) . toMatchObject ( new Error ( 'Visitor made 💩' ) )
270
+ expect ( actual ) . toEqual ( [ 1 , 2 , 4 ] )
271
+ } ,
272
+ )
273
+ } )
274
+ } )
221
275
} )
0 commit comments