|
| 1 | +let React; |
| 2 | +let ReactNoop; |
| 3 | +let Scheduler; |
| 4 | +let act; |
| 5 | +let Activity; |
| 6 | +let useState; |
| 7 | +let assertLog; |
| 8 | + |
| 9 | +describe('Activity error handling', () => { |
| 10 | + beforeEach(() => { |
| 11 | + jest.resetModules(); |
| 12 | + |
| 13 | + React = require('react'); |
| 14 | + ReactNoop = require('react-noop-renderer'); |
| 15 | + Scheduler = require('scheduler'); |
| 16 | + act = require('internal-test-utils').act; |
| 17 | + Activity = React.Activity; |
| 18 | + useState = React.useState; |
| 19 | + |
| 20 | + const InternalTestUtils = require('internal-test-utils'); |
| 21 | + assertLog = InternalTestUtils.assertLog; |
| 22 | + }); |
| 23 | + |
| 24 | + function Text({text}) { |
| 25 | + Scheduler.log(text); |
| 26 | + return text; |
| 27 | + } |
| 28 | + |
| 29 | + // @gate enableActivity |
| 30 | + it( |
| 31 | + 'errors inside a hidden Activity do not escape in the visible part ' + |
| 32 | + 'of the UI', |
| 33 | + async () => { |
| 34 | + class ErrorBoundary extends React.Component { |
| 35 | + state = {error: null}; |
| 36 | + static getDerivedStateFromError(error) { |
| 37 | + return {error}; |
| 38 | + } |
| 39 | + render() { |
| 40 | + if (this.state.error) { |
| 41 | + return ( |
| 42 | + <Text text={`Caught an error: ${this.state.error.message}`} /> |
| 43 | + ); |
| 44 | + } |
| 45 | + return this.props.children; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + function Throws() { |
| 50 | + throw new Error('Oops!'); |
| 51 | + } |
| 52 | + |
| 53 | + let setShowMore; |
| 54 | + function App({content, more}) { |
| 55 | + const [showMore, _setShowMore] = useState(false); |
| 56 | + setShowMore = _setShowMore; |
| 57 | + return ( |
| 58 | + <> |
| 59 | + <div>{content}</div> |
| 60 | + <div> |
| 61 | + <ErrorBoundary> |
| 62 | + <Activity mode={showMore ? 'visible' : 'hidden'}> |
| 63 | + {more} |
| 64 | + </Activity> |
| 65 | + </ErrorBoundary> |
| 66 | + </div> |
| 67 | + </> |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + await act(() => |
| 72 | + ReactNoop.render( |
| 73 | + <App content={<Text text="Visible" />} more={<Throws />} />, |
| 74 | + ), |
| 75 | + ); |
| 76 | + |
| 77 | + // Initial render. An error is thrown when prerendering the hidden |
| 78 | + // Activity boundary, but since it's hidden, the UI doesn't observe it. |
| 79 | + assertLog(['Visible']); |
| 80 | + expect(ReactNoop).toMatchRenderedOutput( |
| 81 | + <> |
| 82 | + <div>Visible</div> |
| 83 | + <div /> |
| 84 | + </>, |
| 85 | + ); |
| 86 | + |
| 87 | + // Once the Activity boundary is revealed, the error is thrown and |
| 88 | + // captured by the outer ErrorBoundary. |
| 89 | + await act(() => setShowMore(true)); |
| 90 | + assertLog(['Caught an error: Oops!', 'Caught an error: Oops!']); |
| 91 | + expect(ReactNoop).toMatchRenderedOutput( |
| 92 | + <> |
| 93 | + <div>Visible</div> |
| 94 | + <div>Caught an error: Oops!</div> |
| 95 | + </>, |
| 96 | + ); |
| 97 | + }, |
| 98 | + ); |
| 99 | +}); |
0 commit comments