-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Add unstable APIs for async rendering to test renderer #12478
Merged
Merged
Changes from all commits
Commits
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 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
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.
Nice!