-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/react-reconciler/src/__tests__/ReactSuspenseEffectsSemanticsDOM-test.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactDOM; | ||
let act; | ||
|
||
describe('ReactSuspenseEffectsSemanticsDOM', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
act = require('jest-react').act; | ||
}); | ||
|
||
it('should not cause a cycle when combined with a render phase update', () => { | ||
let scheduleSuspendingUpdate; | ||
|
||
function App() { | ||
const [value, setValue] = React.useState(true); | ||
|
||
scheduleSuspendingUpdate = () => setValue(!value); | ||
|
||
return ( | ||
<> | ||
<React.Suspense fallback="Loading..."> | ||
<ComponentThatCausesBug value={value} /> | ||
<ComponentThatSuspendsOnUpdate shouldSuspend={!value} /> | ||
</React.Suspense> | ||
</> | ||
); | ||
} | ||
|
||
function ComponentThatCausesBug({value}) { | ||
const [mirroredValue, setMirroredValue] = React.useState(value); | ||
if (mirroredValue !== value) { | ||
setMirroredValue(value); | ||
} | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const [_, setRef] = React.useState(null); | ||
|
||
return <div ref={setRef} />; | ||
} | ||
|
||
const promise = Promise.resolve(); | ||
|
||
function ComponentThatSuspendsOnUpdate({shouldSuspend}) { | ||
if (shouldSuspend) { | ||
// Fake Suspend | ||
throw promise; | ||
} | ||
return null; | ||
} | ||
|
||
act(() => { | ||
const root = ReactDOM.createRoot(document.createElement('div')); | ||
root.render(<App />); | ||
}); | ||
|
||
act(() => { | ||
scheduleSuspendingUpdate(); | ||
}); | ||
}); | ||
}); |