[change] modernize AppRegistry

Rewrite AppRegistry to use function components and hooks.
Rewrite the tests to replace enzyme with testing-library.
This commit is contained in:
Nicolas Gallagher
2020-02-04 11:06:36 -08:00
parent a2cccaf528
commit 96d8649197
4 changed files with 25 additions and 46 deletions
@@ -8,16 +8,11 @@
* @flow * @flow
*/ */
import type { ComponentType } from 'react'; import type { ComponentType, Context } from 'react';
import StyleSheet from '../StyleSheet'; import StyleSheet from '../StyleSheet';
import View from '../View'; import View from '../View';
import { any } from 'prop-types'; import React, { createContext } from 'react';
import React from 'react';
type Context = {
rootTag: any
};
type Props = { type Props = {
WrapperComponent?: ?ComponentType<*>, WrapperComponent?: ?ComponentType<*>,
@@ -26,44 +21,26 @@ type Props = {
rootTag: any rootTag: any
}; };
type State = { const RootTagContext: Context<any> = createContext(null);
mainKey: number
};
export default class AppContainer extends React.Component<Props, State> { export default function AppContainer(props: Props) {
state = { mainKey: 1 }; const { children, WrapperComponent } = props;
static childContextTypes = { let innerView = (
rootTag: any <View children={children} key={1} pointerEvents="box-none" style={styles.appContainer} />
}; );
getChildContext(): Context { if (WrapperComponent) {
return { innerView = <WrapperComponent>{innerView}</WrapperComponent>;
rootTag: this.props.rootTag
};
} }
render() { return (
const { children, WrapperComponent } = this.props; <RootTagContext.Provider value={props.rootTag}>
let innerView = (
<View
children={children}
key={this.state.mainKey}
pointerEvents="box-none"
style={styles.appContainer}
/>
);
if (WrapperComponent) {
innerView = <WrapperComponent>{innerView}</WrapperComponent>;
}
return (
<View pointerEvents="box-none" style={styles.appContainer}> <View pointerEvents="box-none" style={styles.appContainer}>
{innerView} {innerView}
</View> </View>
); </RootTagContext.Provider>
} );
} }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
@@ -4,21 +4,16 @@ import AppRegistry from '..';
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment'; import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
import React from 'react'; import React from 'react';
import ReactDOMServer from 'react-dom/server'; import ReactDOMServer from 'react-dom/server';
import { render } from 'enzyme'; import { render } from '@testing-library/react';
import StyleSheet from '../../StyleSheet'; import StyleSheet from '../../StyleSheet';
import Text from '../../Text'; import Text from '../../Text';
import View from '../../View'; import View from '../../View';
const canUseDOM = ExecutionEnvironment.canUseDOM;
const NoopComponent = () => <div />; const NoopComponent = () => <div />;
const styles = StyleSheet.create({ root: { borderWidth: 1234, backgroundColor: 'purple' } });
const RootComponent = () => <View />;
const AlternativeComponent = () => <Text style={styles.root} />;
describe('AppRegistry', () => { describe('AppRegistry', () => {
describe('getApplication', () => { describe('getApplication', () => {
const canUseDOM = ExecutionEnvironment.canUseDOM;
beforeEach(() => { beforeEach(() => {
ExecutionEnvironment.canUseDOM = false; ExecutionEnvironment.canUseDOM = false;
}); });
@@ -56,6 +51,10 @@ describe('AppRegistry', () => {
return getStyleElement().props.dangerouslySetInnerHTML.__html; return getStyleElement().props.dangerouslySetInnerHTML.__html;
}; };
const styles = StyleSheet.create({ root: { borderWidth: 1234, backgroundColor: 'purple' } });
const RootComponent = () => <View />;
const AlternativeComponent = () => <Text style={styles.root} />;
// First render "RootComponent" // First render "RootComponent"
AppRegistry.registerComponent('App', () => RootComponent); AppRegistry.registerComponent('App', () => RootComponent);
const first = getApplicationStyles('App'); const first = getApplicationStyles('App');
+2 -1
View File
@@ -8,10 +8,11 @@
* @flow * @flow
*/ */
import type { ComponentType } from 'react';
import invariant from 'fbjs/lib/invariant'; import invariant from 'fbjs/lib/invariant';
import unmountComponentAtNode from '../unmountComponentAtNode'; import unmountComponentAtNode from '../unmountComponentAtNode';
import renderApplication, { getApplication } from './renderApplication'; import renderApplication, { getApplication } from './renderApplication';
import type { ComponentType } from 'react';
const emptyObject = {}; const emptyObject = {};
const runnables = {}; const runnables = {};
@@ -8,12 +8,14 @@
* @flow * @flow
*/ */
import type { ComponentType } from 'react';
import AppContainer from './AppContainer'; import AppContainer from './AppContainer';
import invariant from 'fbjs/lib/invariant'; import invariant from 'fbjs/lib/invariant';
import hydrate from '../../modules/hydrate'; import hydrate from '../../modules/hydrate';
import render from '../render'; import render from '../render';
import styleResolver from '../StyleSheet/styleResolver'; import styleResolver from '../StyleSheet/styleResolver';
import React, { type ComponentType } from 'react'; import React from 'react';
export default function renderApplication<Props: Object>( export default function renderApplication<Props: Object>(
RootComponent: ComponentType<Props>, RootComponent: ComponentType<Props>,