Skip to content

Commit 5677d18

Browse files
committed
Errors are no longer caught/swallowed internally, leaving error handling to the calling code.
1 parent a2d0cb4 commit 5677d18

File tree

2 files changed

+230
-162
lines changed

2 files changed

+230
-162
lines changed

src/__tests__/index.test.js

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ describe('reactTreeWalker', () => {
1515
constructor(props) {
1616
super(props)
1717
// $FlowIgnore
18-
this.getSomething = this.getSomething.bind(this)
18+
this.getData = this.getData.bind(this)
1919
}
2020

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
2525
}
2626

2727
render() {
@@ -39,19 +39,19 @@ describe('reactTreeWalker', () => {
3939
const createTree = async => (
4040
<div>
4141
<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}>
4444
<div>
4545
<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} />
4949
</Foo>
5050
</Bob>
5151
<div>hi!</div>
5252
</div>
5353
</Foo>
54-
<Foo something={async ? () => resolveLater(3) : 3} />
54+
<Foo data={async ? () => resolveLater(3) : 3} />
5555
</div>
5656
)
5757

@@ -60,9 +60,9 @@ describe('reactTreeWalker', () => {
6060
const actual = []
6161
// eslint-disable-next-line no-unused-vars
6262
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)
6666
}
6767
}
6868
return reactTreeWalker(tree, visitor).then(() => {
@@ -76,9 +76,9 @@ describe('reactTreeWalker', () => {
7676
const actual = []
7777
// eslint-disable-next-line no-unused-vars
7878
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)
8282
return true
8383
})
8484
}
@@ -95,10 +95,10 @@ describe('reactTreeWalker', () => {
9595
const actual = []
9696
// eslint-disable-next-line no-unused-vars
9797
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
102102
})
103103
}
104104
return true
@@ -193,8 +193,8 @@ describe('reactTreeWalker', () => {
193193
render() {
194194
return (
195195
<div>
196-
<Foo something={1} />
197-
<Foo something={2} />
196+
<Foo data={1} />
197+
<Foo data={2} />
198198
</div>
199199
)
200200
}
@@ -208,14 +208,68 @@ describe('reactTreeWalker', () => {
208208
const actual = []
209209
// eslint-disable-next-line no-unused-vars
210210
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)
214214
}
215215
}
216216
return reactTreeWalker(tree, visitor).then(() => {
217217
const expected = [1, 2]
218218
expect(actual).toEqual(expected)
219219
})
220220
})
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+
})
221275
})

0 commit comments

Comments
 (0)