-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Resolve some suspense crashes #4999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+281
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import React, { | |
| } from 'preact/compat'; | ||
| import { setupScratch, teardown } from '../../../test/_util/helpers'; | ||
| import { createLazy, createSuspender } from './suspense-utils'; | ||
| import { expect } from 'chai'; | ||
|
|
||
| const h = React.createElement; | ||
| /* eslint-env browser, mocha */ | ||
|
|
@@ -2188,6 +2189,282 @@ describe('suspense', () => { | |
| }); | ||
| }); | ||
|
|
||
| it('should not crash when suspended child updates after unmount', () => { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uncommenting line 240 will surface the crash of this |
||
| let childInstance = null; | ||
| const neverResolvingPromise = new Promise(() => {}); | ||
|
|
||
| class ThrowingChild extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
| this.state = { suspend: false, value: 0 }; | ||
| childInstance = this; | ||
| } | ||
|
|
||
| render(props, state) { | ||
| if (state.suspend) { | ||
| throw neverResolvingPromise; | ||
| } | ||
| return <div>value:{state.value}</div>; | ||
| } | ||
| } | ||
|
|
||
| render( | ||
| <Suspense fallback={<div>Suspended...</div>}> | ||
| <ThrowingChild /> | ||
| </Suspense>, | ||
| scratch | ||
| ); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>value:0</div>'); | ||
|
|
||
| childInstance.setState({ suspend: true }); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Suspended...</div>'); | ||
|
|
||
| render(null, scratch); | ||
| expect(scratch.innerHTML).to.equal(''); | ||
|
|
||
| childInstance.setState({ value: 1 }); | ||
| rerender(); | ||
|
|
||
| expect(scratch.innerHTML).to.equal(''); | ||
| }); | ||
|
|
||
| it('should not crash when suspended child updates after diffed unmount', () => { | ||
| let childInstance = null; | ||
| const neverResolvingPromise = new Promise(() => {}); | ||
|
|
||
| class ThrowingChild extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
| this.state = { suspend: false, value: 0 }; | ||
| childInstance = this; | ||
| } | ||
|
|
||
| render(props, state) { | ||
| if (state.suspend) { | ||
| throw neverResolvingPromise; | ||
| } | ||
| return <div>value:{state.value}</div>; | ||
| } | ||
| } | ||
|
|
||
| const HelloWorld = () => <p>Hello world</p>; | ||
|
|
||
| let set; | ||
| const App = () => { | ||
| const [show, setShow] = useState(true); | ||
| set = setShow; | ||
| return show ? ( | ||
| <Suspense fallback={<div>Suspended...</div>}> | ||
| <ThrowingChild /> | ||
| </Suspense> | ||
| ) : ( | ||
| <HelloWorld /> | ||
| ); | ||
| }; | ||
|
|
||
| render(<App />, scratch); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>value:0</div>'); | ||
|
|
||
| childInstance.setState({ suspend: true }); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Suspended...</div>'); | ||
|
|
||
| set(false); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<p>Hello world</p>'); | ||
|
|
||
| childInstance.setState({ value: 1 }); | ||
| rerender(); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<p>Hello world</p>'); | ||
| }); | ||
|
|
||
| it('should not crash when suspended child resolves after unmount', async () => { | ||
| let childInstance = null, | ||
| res; | ||
| const neverResolvingPromise = new Promise(r => { | ||
| res = r; | ||
| }); | ||
|
|
||
| class ThrowingChild extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
| this.state = { suspend: false, value: 0 }; | ||
| childInstance = this; | ||
| } | ||
|
|
||
| render(props, state) { | ||
| if (state.suspend) { | ||
| throw neverResolvingPromise; | ||
| } | ||
| return <div>value:{state.value}</div>; | ||
| } | ||
| } | ||
|
|
||
| render( | ||
| <Suspense fallback={<div>Suspended...</div>}> | ||
| <ThrowingChild /> | ||
| </Suspense>, | ||
| scratch | ||
| ); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>value:0</div>'); | ||
|
|
||
| childInstance.setState({ suspend: true }); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Suspended...</div>'); | ||
|
|
||
| render(null, scratch); | ||
| expect(scratch.innerHTML).to.equal(''); | ||
|
|
||
| res(); | ||
| return neverResolvingPromise.then(() => { | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal(''); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not crash when suspended child resolves after diffed unmount', async () => { | ||
| let childInstance = null, | ||
| res; | ||
| const neverResolvingPromise = new Promise(r => { | ||
| res = r; | ||
| }); | ||
|
|
||
| class ThrowingChild extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
| this.state = { suspend: false, value: 0 }; | ||
| childInstance = this; | ||
| } | ||
|
|
||
| render(props, state) { | ||
| if (state.suspend) { | ||
| throw neverResolvingPromise; | ||
| } | ||
| return <div>value:{state.value}</div>; | ||
| } | ||
| } | ||
|
|
||
| const HelloWorld = () => <p>Hello world</p>; | ||
|
|
||
| let set; | ||
| const App = () => { | ||
| const [show, setShow] = useState(true); | ||
| set = setShow; | ||
| return show ? ( | ||
| <Suspense fallback={<div>Suspended...</div>}> | ||
| <ThrowingChild /> | ||
| </Suspense> | ||
| ) : ( | ||
| <HelloWorld /> | ||
| ); | ||
| }; | ||
|
|
||
| render(<App />, scratch); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>value:0</div>'); | ||
|
|
||
| childInstance.setState({ suspend: true }); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Suspended...</div>'); | ||
|
|
||
| set(false); | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<p>Hello world</p>'); | ||
|
|
||
| res(); | ||
| return neverResolvingPromise.then(() => { | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<p>Hello world</p>'); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not crash when suspense promise resolves after unmount', () => { | ||
| let resolve; | ||
| const promise = new Promise(r => { | ||
| resolve = r; | ||
| }); | ||
|
|
||
| class ThrowingChild extends Component { | ||
| render() { | ||
| throw promise; | ||
| } | ||
| } | ||
|
|
||
| render( | ||
| <Suspense fallback={<div>Suspended...</div>}> | ||
| <ThrowingChild /> | ||
| </Suspense>, | ||
| scratch | ||
| ); | ||
| rerender(); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>Suspended...</div>'); | ||
|
|
||
| render(null, scratch); | ||
| expect(scratch.innerHTML).to.equal(''); | ||
|
|
||
| resolve(); | ||
|
|
||
| return promise.then(() => { | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal(''); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not crash when useContext is used in a suspending component', () => { | ||
| const TestContext = createContext('default'); | ||
| let resolve; | ||
| let shouldSuspend = false; | ||
| const promise = new Promise(r => { | ||
| resolve = r; | ||
| }); | ||
|
|
||
| function ContextUser() { | ||
| const value = React.useContext(TestContext); | ||
| if (shouldSuspend) { | ||
| throw promise; | ||
| } | ||
| return <div>Context: {value}</div>; | ||
| } | ||
|
|
||
| render( | ||
| <TestContext.Provider value="test-value"> | ||
| <Suspense fallback={<div>Suspended...</div>}> | ||
| <ContextUser /> | ||
| </Suspense> | ||
| </TestContext.Provider>, | ||
| scratch | ||
| ); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>Context: test-value</div>'); | ||
|
|
||
| shouldSuspend = true; | ||
| render( | ||
| <TestContext.Provider value="test-value"> | ||
| <Suspense fallback={<div>Suspended...</div>}> | ||
| <ContextUser /> | ||
| </Suspense> | ||
| </TestContext.Provider>, | ||
| scratch | ||
| ); | ||
| rerender(); | ||
|
|
||
| expect(scratch.innerHTML).to.equal('<div>Suspended...</div>'); | ||
|
|
||
| shouldSuspend = false; | ||
| resolve(); | ||
|
|
||
| return promise.then(() => { | ||
| rerender(); | ||
| expect(scratch.innerHTML).to.equal('<div>Context: test-value</div>'); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not crash if fallback has same DOM as suspended nodes', () => { | ||
| const [Lazy, resolveLazy] = createLazy(); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe nit: is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whups, darn vscode auto-import