-
-
Notifications
You must be signed in to change notification settings - Fork 635
test(dummy): enable StrictMode in OSS and Pro dummies #3206
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3e9a18f
test(dummy): enable React StrictMode across spec/dummy startup
justin808 d466e91
test(pro dummy): enable StrictMode across startup
justin808 45da8de
test(dummy): harden StrictMode wrappers
justin808 3dd51c9
test(dummy): address StrictMode review feedback
justin808 ae6c115
test(dummy): bring OSS StrictMode helper to parity with Pro
justin808 d526ecd
test(dummy): act on PR #3206 review pass — server alias, dead branch,…
justin808 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
5 changes: 3 additions & 2 deletions
5
react_on_rails/spec/dummy/client/app-react16/startup/ManualRenderApp.jsx
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
5 changes: 3 additions & 2 deletions
5
react_on_rails/spec/dummy/client/app/startup/ManualRenderApp.jsx
This file contains hidden or 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 hidden or 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 hidden or 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
70 changes: 70 additions & 0 deletions
70
react_on_rails/spec/dummy/client/app/strictModeSupport.jsx
This file contains hidden or 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,70 @@ | ||
| import React from 'react'; | ||
|
|
||
| const wrappedFunctionComponents = new WeakMap(); | ||
| const wrappedOtherComponents = new Map(); | ||
|
justin808 marked this conversation as resolved.
Outdated
justin808 marked this conversation as resolved.
Outdated
|
||
|
|
||
| const isRenderFunction = (component) => { | ||
| if (typeof component !== 'function') { | ||
| return false; | ||
| } | ||
|
|
||
| if (component.prototype?.isReactComponent) { | ||
| return false; | ||
| } | ||
|
|
||
| if (component.renderFunction) { | ||
| return true; | ||
| } | ||
|
|
||
| return component.length >= 2; | ||
|
justin808 marked this conversation as resolved.
justin808 marked this conversation as resolved.
justin808 marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| const createStrictModeWrapper = (Component) => { | ||
| function StrictModeWrapper(props) { | ||
| return ( | ||
| <React.StrictMode> | ||
| {typeof Component === 'function' ? <Component {...props} /> : React.createElement(Component, props)} | ||
|
justin808 marked this conversation as resolved.
Outdated
|
||
| </React.StrictMode> | ||
|
justin808 marked this conversation as resolved.
Outdated
|
||
| ); | ||
| } | ||
|
|
||
| const componentName = | ||
| typeof Component === 'string' | ||
| ? Component | ||
| : Component.displayName || Component.name || 'AnonymousComponent'; | ||
| StrictModeWrapper.displayName = `StrictMode(${componentName})`; | ||
|
|
||
| return StrictModeWrapper; | ||
| }; | ||
|
|
||
| const wrapComponentInStrictMode = (component) => { | ||
| if (typeof component === 'function') { | ||
| const cachedComponent = wrappedFunctionComponents.get(component); | ||
| if (cachedComponent) { | ||
| return cachedComponent; | ||
| } | ||
|
|
||
| const wrappedComponent = createStrictModeWrapper(component); | ||
| wrappedFunctionComponents.set(component, wrappedComponent); | ||
| return wrappedComponent; | ||
| } | ||
|
|
||
| const cachedComponent = wrappedOtherComponents.get(component); | ||
| if (cachedComponent) { | ||
| return cachedComponent; | ||
| } | ||
|
|
||
| const wrappedComponent = createStrictModeWrapper(component); | ||
| wrappedOtherComponents.set(component, wrappedComponent); | ||
| return wrappedComponent; | ||
| }; | ||
|
|
||
| export const wrapElementInStrictMode = (reactElement) => <React.StrictMode>{reactElement}</React.StrictMode>; | ||
|
|
||
| export const wrapRegisteredComponentsWithStrictMode = (components) => | ||
| Object.fromEntries( | ||
| Object.entries(components).map(([name, component]) => [ | ||
| name, | ||
| isRenderFunction(component) ? component : wrapComponentInStrictMode(component), | ||
|
justin808 marked this conversation as resolved.
Outdated
justin808 marked this conversation as resolved.
Outdated
|
||
| ]), | ||
| ); | ||
54 changes: 54 additions & 0 deletions
54
react_on_rails/spec/dummy/tests/strict-mode-support.test.jsx
This file contains hidden or 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,54 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import React from 'react'; | ||
|
|
||
| import { | ||
| wrapElementInStrictMode, | ||
| wrapRegisteredComponentsWithStrictMode, | ||
| } from '../client/app/strictModeSupport'; | ||
|
|
||
| describe('strictModeSupport', () => { | ||
| it('wraps registered React components in StrictMode', () => { | ||
| const HelloWorld = ({ greeting }) => <div>{greeting}</div>; | ||
| HelloWorld.propTypes = { | ||
| greeting: PropTypes.string.isRequired, | ||
| }; | ||
| const wrappedComponent = wrapRegisteredComponentsWithStrictMode({ HelloWorld }).HelloWorld; | ||
|
|
||
| expect(wrappedComponent).not.toBe(HelloWorld); | ||
|
|
||
| const wrappedElement = wrappedComponent({ greeting: 'hello' }); | ||
| expect(wrappedElement.type).toBe(React.StrictMode); | ||
| expect(wrappedElement.props.children.type).toBe(HelloWorld); | ||
| expect(wrappedElement.props.children.props.greeting).toBe('hello'); | ||
| }); | ||
|
|
||
| it('reuses the same wrapper for repeated registrations of the same component', () => { | ||
| const HelloWorld = () => <div>Hello</div>; | ||
|
|
||
| const firstWrappedComponent = wrapRegisteredComponentsWithStrictMode({ HelloWorld }).HelloWorld; | ||
| const secondWrappedComponent = wrapRegisteredComponentsWithStrictMode({ HelloWorld }).HelloWorld; | ||
|
|
||
| expect(firstWrappedComponent).toBe(secondWrappedComponent); | ||
| }); | ||
|
|
||
| it('does not wrap render functions or renderer functions', () => { | ||
|
justin808 marked this conversation as resolved.
Outdated
|
||
| const renderFunction = (props, railsContext) => ({ props, railsContext }); | ||
| const rendererFunction = (props, railsContext, domNodeId) => ({ props, railsContext, domNodeId }); | ||
|
|
||
| const wrappedComponents = wrapRegisteredComponentsWithStrictMode({ | ||
| renderFunction, | ||
| rendererFunction, | ||
| }); | ||
|
|
||
| expect(wrappedComponents.renderFunction).toBe(renderFunction); | ||
| expect(wrappedComponents.rendererFunction).toBe(rendererFunction); | ||
| }); | ||
|
justin808 marked this conversation as resolved.
Outdated
|
||
|
|
||
| it('wraps manual render trees in StrictMode', () => { | ||
| const innerElement = <div>hello</div>; | ||
| const wrappedElement = wrapElementInStrictMode(innerElement); | ||
|
|
||
| expect(wrappedElement.type).toBe(React.StrictMode); | ||
| expect(wrappedElement.props.children).toBe(innerElement); | ||
| }); | ||
| }); | ||
|
justin808 marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.