1
- /* @flow */
2
1
/* eslint-disable react/sort-comp */
3
2
/* eslint-disable react/no-multi-comp */
4
3
/* eslint-disable react/prop-types */
@@ -12,8 +11,6 @@ const Bob = ({ children }) => <div>{children}</div>;
12
11
13
12
describe ( 'reactTreeWalker' , ( ) => {
14
13
class Foo extends Component {
15
- props : { children ?: any , something : any }
16
-
17
14
constructor ( props ) {
18
15
super ( props ) ;
19
16
// $FlowIgnore
@@ -53,56 +50,36 @@ describe('reactTreeWalker', () => {
53
50
</ div >
54
51
) ;
55
52
56
- it ( 'simple sync' , ( ) => {
53
+ it ( 'simple sync visitor ' , ( ) => {
57
54
const tree = createTree ( false ) ;
58
55
const actual = [ ] ;
59
56
// eslint-disable-next-line no-unused-vars
60
57
const visitor = ( element , instance , context ) => {
61
- if ( instance && typeof instance . getSomething ) {
58
+ if ( instance && typeof instance . getSomething === 'function' ) {
62
59
const something = instance . getSomething ( ) ;
63
60
actual . push ( something ) ;
64
61
}
65
62
} ;
66
- reactTreeWalker ( tree , visitor ) ;
67
- const expected = [ 1 , 2 , 4 , 5 , 3 ] ;
68
- expect ( actual ) . toEqual ( expected ) ;
63
+ return reactTreeWalker ( tree , visitor ) . then ( ( ) => {
64
+ const expected = [ 1 , 2 , 4 , 5 , 3 ] ;
65
+ expect ( actual ) . toEqual ( expected ) ;
66
+ } ) ;
69
67
} ) ;
70
68
71
- it ( 'complex async ' , ( ) => {
72
- const treeRoot = createTree ( true ) ;
69
+ it ( 'promise based visitor ' , ( ) => {
70
+ const tree = createTree ( true ) ;
73
71
const actual = [ ] ;
74
-
75
- const doWalk = ( el , ctx = { } , fetchRoot = false ) => {
76
- const somethings = [ ] ;
77
-
78
- // eslint-disable-next-line no-unused-vars
79
- const visitor = ( element , instance , context ) => {
80
- const skipRoot = ! fetchRoot && ( element === el ) ;
81
- if ( instance && typeof instance . getSomething === 'function' && ! skipRoot ) {
82
- const something = instance . getSomething ( ) ;
83
- somethings . push ( { something, element, context } ) ;
84
- return false ;
85
- }
86
- return undefined ;
87
- } ;
88
-
89
- reactTreeWalker ( el , visitor , ctx ) ;
90
-
91
- // eslint-disable-next-line arrow-body-style
92
- const promises = somethings . map ( ( { something, element, context } ) => {
93
- return something . then ( ( result ) => {
94
- actual . push ( result ) ;
95
- return doWalk ( element , context ) ;
72
+ // eslint-disable-next-line no-unused-vars
73
+ const visitor = ( element , instance , context ) => {
74
+ if ( instance && typeof instance . getSomething === 'function' ) {
75
+ return instance . getSomething ( ) . then ( ( something ) => {
76
+ actual . push ( something ) ;
96
77
} ) ;
97
- } ) ;
98
-
99
- return promises . length > 0
100
- ? Promise . all ( promises )
101
- : Promise . resolve ( [ ] ) ;
78
+ }
79
+ return true ;
102
80
} ;
103
-
104
- return doWalk ( treeRoot , { } , true ) . then ( ( ) => {
105
- const expected = [ 1 , 2 , 3 , 4 , 5 ] ;
81
+ return reactTreeWalker ( tree , visitor ) . then ( ( ) => {
82
+ const expected = [ 1 , 2 , 4 , 5 , 3 ] ;
106
83
expect ( actual ) . toEqual ( expected ) ;
107
84
} ) ;
108
85
} ) ;
@@ -137,9 +114,10 @@ describe('reactTreeWalker', () => {
137
114
actual = instance . getState ( ) ;
138
115
}
139
116
} ;
140
- reactTreeWalker ( tree , visitor ) ;
141
- const expected = { foo : 'bar' } ;
142
- expect ( actual ) . toMatchObject ( expected ) ;
117
+ return reactTreeWalker ( tree , visitor ) . then ( ( ) => {
118
+ const expected = { foo : 'bar' } ;
119
+ expect ( actual ) . toMatchObject ( expected ) ;
120
+ } ) ;
143
121
} ) ;
144
122
145
123
it ( 'getChildContext' , ( ) => {
@@ -163,9 +141,9 @@ describe('reactTreeWalker', () => {
163
141
const tree = < Baz > < Qux /> </ Baz > ;
164
142
// eslint-disable-next-line no-unused-vars
165
143
const visitor = ( element , instance , context ) => undefined ;
166
- reactTreeWalker ( tree , visitor ) ;
167
-
168
- const expected = { foo : 'bar' } ;
169
- expect ( actual ) . toMatchObject ( expected ) ;
144
+ return reactTreeWalker ( tree , visitor ) . then ( ( ) => {
145
+ const expected = { foo : 'bar' } ;
146
+ expect ( actual ) . toMatchObject ( expected ) ;
147
+ } ) ;
170
148
} ) ;
171
149
} ) ;
0 commit comments