-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bugfix] Pending state is always user-blocking
Fixes a bug where `isPending` is only set to `true` if `startTransition` is called from inside an input event. That's usually the case, but not always. Now it works regardless of where you call it.
- Loading branch information
Showing
2 changed files
with
155 additions
and
54 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
102 changes: 102 additions & 0 deletions
102
packages/react-reconciler/src/__tests__/ReactTransition-test.internal.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,102 @@ | ||
/** | ||
* 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 | ||
* @jest-environment node | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let ReactFeatureFlags; | ||
let React; | ||
let ReactNoop; | ||
let Scheduler; | ||
let Suspense; | ||
let useState; | ||
let useTransition; | ||
let act; | ||
|
||
describe('ReactHooksWithNoopRenderer', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false; | ||
ReactFeatureFlags.enableSchedulerTracing = true; | ||
ReactFeatureFlags.flushSuspenseFallbacksInTests = false; | ||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
Scheduler = require('scheduler'); | ||
useState = React.useState; | ||
useTransition = React.useTransition; | ||
Suspense = React.Suspense; | ||
act = ReactNoop.act; | ||
}); | ||
|
||
function Text(props) { | ||
Scheduler.unstable_yieldValue(props.text); | ||
return props.text; | ||
} | ||
|
||
function createAsyncText(text) { | ||
let resolved = false; | ||
let Component = function() { | ||
if (!resolved) { | ||
Scheduler.unstable_yieldValue('Suspend! [' + text + ']'); | ||
throw promise; | ||
} | ||
return <Text text={text} />; | ||
}; | ||
let promise = new Promise(resolve => { | ||
Component.resolve = function() { | ||
resolved = true; | ||
return resolve(); | ||
}; | ||
}); | ||
return Component; | ||
} | ||
|
||
it('isPending works even if called from outside an input event', async () => { | ||
const Async = createAsyncText('Async'); | ||
let start; | ||
function App() { | ||
const [show, setShow] = useState(false); | ||
const [startTransition, isPending] = useTransition(); | ||
start = () => startTransition(() => setShow(true)); | ||
return ( | ||
<Suspense fallback={<Text text="Loading..." />}> | ||
{isPending ? <Text text="Pending..." /> : null} | ||
{show ? <Async /> : <Text text="(empty)" />} | ||
</Suspense> | ||
); | ||
} | ||
|
||
const root = ReactNoop.createRoot(); | ||
|
||
await act(async () => { | ||
root.render(<App />); | ||
}); | ||
expect(Scheduler).toHaveYielded(['(empty)']); | ||
expect(root).toMatchRenderedOutput('(empty)'); | ||
|
||
await act(async () => { | ||
start(); | ||
|
||
expect(Scheduler).toFlushAndYield([ | ||
'Pending...', | ||
'(empty)', | ||
'Suspend! [Async]', | ||
'Loading...', | ||
]); | ||
|
||
expect(root).toMatchRenderedOutput('Pending...(empty)'); | ||
|
||
await Async.resolve(); | ||
}); | ||
expect(Scheduler).toHaveYielded(['Async']); | ||
expect(root).toMatchRenderedOutput('Async'); | ||
}); | ||
}); |