|
1 | 1 | import React, { Suspense } from 'react' |
2 | | -import { create, act } from 'react-test-renderer' |
| 2 | +import { act, create } from 'react-test-renderer' |
3 | 3 |
|
4 | | -function TestHook({ callback, hookProps, children }) { |
5 | | - children(callback(hookProps)) |
6 | | - return null |
7 | | -} |
8 | | - |
9 | | -class ErrorBoundary extends React.Component { |
10 | | - constructor(props) { |
11 | | - super(props) |
12 | | - this.state = { hasError: false } |
13 | | - } |
14 | | - |
15 | | - static getDerivedStateFromError() { |
16 | | - return { hasError: true } |
17 | | - } |
18 | | - |
19 | | - componentDidCatch(error) { |
20 | | - this.props.onError(error) |
21 | | - } |
22 | | - |
23 | | - componentDidUpdate(prevProps) { |
24 | | - if (this.props != prevProps && this.state.hasError) { |
25 | | - this.setState({ hasError: false }) |
26 | | - } |
27 | | - } |
28 | | - |
29 | | - render() { |
30 | | - return !this.state.hasError && this.props.children |
| 4 | +function TestHook({ callback, hookProps, onError, children }) { |
| 5 | + try { |
| 6 | + children(callback(hookProps)) |
| 7 | + } catch (err) { |
| 8 | + onError(err) |
31 | 9 | } |
| 10 | + return null |
32 | 11 | } |
33 | 12 |
|
34 | 13 | function Fallback() { |
@@ -77,13 +56,11 @@ function renderHook(callback, { initialProps, wrapper } = {}) { |
77 | 56 |
|
78 | 57 | const toRender = () => |
79 | 58 | wrapUiIfNeeded( |
80 | | - <ErrorBoundary onError={setError}> |
81 | | - <Suspense fallback={<Fallback />}> |
82 | | - <TestHook callback={callback} hookProps={hookProps.current}> |
83 | | - {setValue} |
84 | | - </TestHook> |
85 | | - </Suspense> |
86 | | - </ErrorBoundary> |
| 59 | + <Suspense fallback={<Fallback />}> |
| 60 | + <TestHook callback={callback} hookProps={hookProps.current} onError={setError}> |
| 61 | + {setValue} |
| 62 | + </TestHook> |
| 63 | + </Suspense> |
87 | 64 | ) |
88 | 65 |
|
89 | 66 | let testRenderer |
|
0 commit comments