-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unstable APIs for async rendering to test renderer
These are based on the ReactNoop renderer, which we use to test React itself. This gives library authors (Relay, Apollo, Redux, et al.) a way to test their components for async compatibility. - Pass `unstable_isAsync` to `TestRenderer.create` to create an async renderer instance. This causes updates to be lazily flushed. - `renderer.unstable_yield` tells React to yield execution after the currently rendering component. - `renderer.unstable_flushAll` flushes all pending async work, and returns an array of yielded values. - `renderer.unstable_flushThrough` receives an array of expected values, begins rendering, and stops once those values have been yielded. It returns the array of values that are actually yielded. The user should assert that they are equal. Although we've used this pattern successfully in our own tests, I'm not sure if these are the final APIs we'll make public.
- Loading branch information
Showing
8 changed files
with
226 additions
and
8 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
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
97 changes: 97 additions & 0 deletions
97
packages/react-test-renderer/src/__tests__/ReactTestRendererAsync-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,97 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* | ||
* 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'; | ||
|
||
const React = require('react'); | ||
const ReactTestRenderer = require('react-test-renderer'); | ||
|
||
describe('ReactTestRendererAsync', () => { | ||
it('flushAll flushes all work', () => { | ||
function Foo(props) { | ||
return props.children; | ||
} | ||
const renderer = ReactTestRenderer.create(<Foo>Hi</Foo>, { | ||
unstable_isAsync: true, | ||
}); | ||
|
||
// Before flushing, nothing has mounted. | ||
expect(renderer.toJSON()).toEqual(null); | ||
|
||
// Flush initial mount. | ||
renderer.unstable_flushAll(); | ||
expect(renderer.toJSON()).toEqual('Hi'); | ||
|
||
// Update | ||
renderer.update(<Foo>Bye</Foo>); | ||
// Not yet updated. | ||
expect(renderer.toJSON()).toEqual('Hi'); | ||
// Flush update. | ||
renderer.unstable_flushAll(); | ||
expect(renderer.toJSON()).toEqual('Bye'); | ||
}); | ||
|
||
it('flushAll returns array of yielded values', () => { | ||
function Child(props) { | ||
renderer.unstable_yield(props.children); | ||
return props.children; | ||
} | ||
function Parent(props) { | ||
return ( | ||
<React.Fragment> | ||
<Child>{'A:' + props.step}</Child> | ||
<Child>{'B:' + props.step}</Child> | ||
<Child>{'C:' + props.step}</Child> | ||
</React.Fragment> | ||
); | ||
} | ||
const renderer = ReactTestRenderer.create(<Parent step={1} />, { | ||
unstable_isAsync: true, | ||
}); | ||
|
||
expect(renderer.unstable_flushAll()).toEqual(['A:1', 'B:1', 'C:1']); | ||
expect(renderer.toJSON()).toEqual(['A:1', 'B:1', 'C:1']); | ||
|
||
renderer.update(<Parent step={2} />); | ||
expect(renderer.unstable_flushAll()).toEqual(['A:2', 'B:2', 'C:2']); | ||
expect(renderer.toJSON()).toEqual(['A:2', 'B:2', 'C:2']); | ||
}); | ||
|
||
it('flushThrough flushes until the expected values is yielded', () => { | ||
function Child(props) { | ||
renderer.unstable_yield(props.children); | ||
return props.children; | ||
} | ||
function Parent(props) { | ||
return ( | ||
<React.Fragment> | ||
<Child>{'A:' + props.step}</Child> | ||
<Child>{'B:' + props.step}</Child> | ||
<Child>{'C:' + props.step}</Child> | ||
</React.Fragment> | ||
); | ||
} | ||
const renderer = ReactTestRenderer.create(<Parent step={1} />, { | ||
unstable_isAsync: true, | ||
}); | ||
|
||
// Flush the first two siblings | ||
expect(renderer.unstable_flushThrough(['A:1', 'B:1'])).toEqual([ | ||
'A:1', | ||
'B:1', | ||
]); | ||
// Did not commit yet. | ||
expect(renderer.toJSON()).toEqual(null); | ||
|
||
// Flush the remaining work | ||
expect(renderer.unstable_flushAll()).toEqual(['C:1']); | ||
expect(renderer.toJSON()).toEqual(['A:1', 'B:1', 'C:1']); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import invariant from 'fbjs/lib/invariant'; | ||
|
||
import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags'; | ||
import typeof * as PersistentFeatureFlagsType from './ReactFeatureFlags.persistent'; | ||
|
||
export const debugRenderPhaseSideEffects = false; | ||
export const debugRenderPhaseSideEffectsForStrictMode = false; | ||
export const enableCreateRoot = false; | ||
export const enableUserTimingAPI = __DEV__; | ||
export const enableGetDerivedStateFromCatch = false; | ||
export const warnAboutDeprecatedLifecycles = false; | ||
export const replayFailedUnitOfWorkWithInvokeGuardedCallback = false; | ||
export const enableMutatingReconciler = true; | ||
export const enableNoopReconciler = false; | ||
export const enablePersistentReconciler = false; | ||
export const alwaysUseRequestIdleCallbackPolyfill = false; | ||
|
||
// Only used in www builds. | ||
export function addUserTimingListener() { | ||
invariant(false, 'Not implemented.'); | ||
} | ||
|
||
// Flow magic to verify the exports of this file match the original version. | ||
// eslint-disable-next-line no-unused-vars | ||
type Check<_X, Y: _X, X: Y = _X> = null; | ||
// eslint-disable-next-line no-unused-expressions | ||
(null: Check<PersistentFeatureFlagsType, FeatureFlagsType>); |
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