forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-land "Fix: flushSync changes priority inside effect (facebook#21122)"
This re-lands commit 0e3c7e1.
- Loading branch information
Showing
3 changed files
with
86 additions
and
22 deletions.
There are no files selected for viewing
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
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
64 changes: 64 additions & 0 deletions
64
packages/react-reconciler/src/__tests__/ReactFlushSync-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,64 @@ | ||
let React; | ||
let ReactNoop; | ||
let Scheduler; | ||
let useState; | ||
let useEffect; | ||
|
||
describe('ReactFlushSync', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
Scheduler = require('scheduler'); | ||
useState = React.useState; | ||
useEffect = React.useEffect; | ||
}); | ||
|
||
function Text({text}) { | ||
Scheduler.unstable_yieldValue(text); | ||
return text; | ||
} | ||
|
||
// @gate experimental || !enableSyncDefaultUpdates | ||
test('changes priority of updates in useEffect', async () => { | ||
function App() { | ||
const [syncState, setSyncState] = useState(0); | ||
const [state, setState] = useState(0); | ||
useEffect(() => { | ||
if (syncState !== 1) { | ||
setState(1); | ||
ReactNoop.flushSync(() => setSyncState(1)); | ||
} | ||
}, [syncState, state]); | ||
return <Text text={`${syncState}, ${state}`} />; | ||
} | ||
|
||
const root = ReactNoop.createRoot(); | ||
await ReactNoop.act(async () => { | ||
if (gate(flags => flags.enableSyncDefaultUpdates)) { | ||
React.unstable_startTransition(() => { | ||
root.render(<App />); | ||
}); | ||
} else { | ||
root.render(<App />); | ||
} | ||
// This will yield right before the passive effect fires | ||
expect(Scheduler).toFlushUntilNextPaint(['0, 0']); | ||
|
||
// The passive effect will schedule a sync update and a normal update. | ||
// They should commit in two separate batches. First the sync one. | ||
expect(() => { | ||
expect(Scheduler).toFlushUntilNextPaint(['1, 0']); | ||
}).toErrorDev('flushSync was called from inside a lifecycle method'); | ||
|
||
// The remaining update is not sync | ||
ReactNoop.flushSync(); | ||
expect(Scheduler).toHaveYielded([]); | ||
|
||
// Now flush it. | ||
expect(Scheduler).toFlushUntilNextPaint(['1, 1']); | ||
}); | ||
expect(root).toMatchRenderedOutput('1, 1'); | ||
}); | ||
}); |