Skip to content

Commit 4425daf

Browse files
author
Thien Do
committed
1 parent 5951226 commit 4425daf

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/__tests__/index.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,36 @@ describe('reactTreeWalker', () => {
164164
expect(actual).toMatchObject(expected)
165165
})
166166
})
167+
168+
it('works with instance-as-result component', () => {
169+
// eslint-disable-next-line react/prefer-stateless-function
170+
class Baz extends Component {
171+
render() {
172+
return (
173+
<div>
174+
<Foo something={1} />
175+
<Foo something={2} />
176+
</div>
177+
)
178+
}
179+
}
180+
const Bar = props => new Baz(props)
181+
const tree = (
182+
<div>
183+
<Bar />
184+
</div>
185+
)
186+
const actual = []
187+
// eslint-disable-next-line no-unused-vars
188+
const visitor = (element, instance, context) => {
189+
if (instance && typeof instance.getSomething === 'function') {
190+
const something = instance.getSomething()
191+
actual.push(something)
192+
}
193+
}
194+
return reactTreeWalker(tree, visitor).then(() => {
195+
const expected = [1, 2]
196+
expect(actual).toEqual(expected)
197+
})
198+
})
167199
})

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ const pMapSeries = (iterable, iterator) => {
4949
).then(() => ret)
5050
}
5151

52+
const ensureChild = child =>
53+
child && typeof child.render === 'function'
54+
? ensureChild(child.render())
55+
: child
56+
5257
export const isPromise = x => x != null && typeof x.then === 'function'
5358

5459
// Recurse an React Element tree, running visitor on each element.
@@ -68,7 +73,7 @@ export default function reactTreeWalker(
6873
resolve()
6974
}
7075

71-
const child = getChildren()
76+
const child = ensureChild(getChildren())
7277
const theChildContext =
7378
typeof childContext === 'function' ? childContext() : childContext
7479

0 commit comments

Comments
 (0)