Skip to content

Commit

Permalink
Adds displayName to EventComponent and EventTarget (#15268)
Browse files Browse the repository at this point in the history
* Adds displayName to EventComponent and EventTarget
  • Loading branch information
trueadm authored Apr 3, 2019
1 parent fc6a9f1 commit 89064fe
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function createReactEventComponent(targetEventTypes, handleEvent) {

return {
$$typeof: Symbol.for('react.event_component'),
displayName: 'TestEventComponent',
props: null,
responder: testEventResponder,
};
Expand Down
1 change: 1 addition & 0 deletions packages/react-events/src/Drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const DragResponder = {

export default {
$$typeof: REACT_EVENT_COMPONENT_TYPE,
displayName: 'Drag',
props: null,
responder: DragResponder,
};
1 change: 1 addition & 0 deletions packages/react-events/src/Focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const FocusResponder = {

export default {
$$typeof: REACT_EVENT_COMPONENT_TYPE,
displayName: 'Focus',
props: null,
responder: FocusResponder,
};
1 change: 1 addition & 0 deletions packages/react-events/src/Hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const HoverResponder = {

export default {
$$typeof: REACT_EVENT_COMPONENT_TYPE,
displayName: 'Hover',
props: null,
responder: HoverResponder,
};
1 change: 1 addition & 0 deletions packages/react-events/src/Press.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ const PressResponder = {

export default {
$$typeof: REACT_EVENT_COMPONENT_TYPE,
displayName: 'Press',
props: null,
responder: PressResponder,
};
1 change: 1 addition & 0 deletions packages/react-events/src/Swipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ const SwipeResponder = {

export default {
$$typeof: REACT_EVENT_COMPONENT_TYPE,
displayName: 'Swipe',
props: null,
responder: SwipeResponder,
};
4 changes: 4 additions & 0 deletions packages/react-events/src/__tests__/Press-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,8 @@ describe('Event responder: Press', () => {
expect(events).toEqual(['keydown', 'inner: onPress', 'outer: onPress']);
});
});

it('expect displayName to show up for event component', () => {
expect(Press.displayName).toBe('Press');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const noOpResponder = {
function createReactEventComponent() {
return {
$$typeof: ReactSymbols.REACT_EVENT_COMPONENT_TYPE,
displayName: 'TestEventComponent',
props: null,
responder: noOpResponder,
};
Expand All @@ -37,6 +38,7 @@ function createReactEventComponent() {
function createReactEventTarget() {
return {
$$typeof: ReactSymbols.REACT_EVENT_TARGET_TYPE,
displayName: 'TestEventTarget',
type: Symbol.for('react.event_target.test'),
};
}
Expand Down Expand Up @@ -392,6 +394,54 @@ describe('ReactFiberEvents', () => {
'Warning: validateDOMNesting: React event targets must not have event components as children.',
);
});

it('should error with a component stack contains the names of the event components and event targets', () => {
let componentStackMessage;

function ErrorComponent() {
throw new Error('Failed!');
}

const Test = () => (
<EventComponent>
<EventTarget>
<span>
<ErrorComponent />
</span>
</EventTarget>
</EventComponent>
);

class Wrapper extends React.Component {
state = {
error: null,
};

componentDidCatch(error, errMessage) {
componentStackMessage = errMessage.componentStack;
this.setState({
error,
});
}

render() {
if (this.state.error) {
return null;
}
return <Test />;
}
}

ReactNoop.render(<Wrapper />);
expect(Scheduler).toFlushWithoutYielding();

expect(componentStackMessage.includes('ErrorComponent')).toBe(true);
expect(componentStackMessage.includes('span')).toBe(true);
expect(componentStackMessage.includes('TestEventTarget')).toBe(true);
expect(componentStackMessage.includes('TestEventComponent')).toBe(true);
expect(componentStackMessage.includes('Test')).toBe(true);
expect(componentStackMessage.includes('Wrapper')).toBe(true);
});
});

describe('TestRenderer', () => {
Expand Down Expand Up @@ -570,7 +620,7 @@ describe('ReactFiberEvents', () => {
error: null,
};

componentDidCatch(error) {
componentDidCatch(error, errStack) {
this.setState({
error,
});
Expand Down Expand Up @@ -734,6 +784,55 @@ describe('ReactFiberEvents', () => {
'Warning: validateDOMNesting: React event targets must not have event components as children.',
);
});

it('should error with a component stack contains the names of the event components and event targets', () => {
let componentStackMessage;

function ErrorComponent() {
throw new Error('Failed!');
}

const Test = () => (
<EventComponent>
<EventTarget>
<span>
<ErrorComponent />
</span>
</EventTarget>
</EventComponent>
);

class Wrapper extends React.Component {
state = {
error: null,
};

componentDidCatch(error, errMessage) {
componentStackMessage = errMessage.componentStack;
this.setState({
error,
});
}

render() {
if (this.state.error) {
return null;
}
return <Test />;
}
}

const root = ReactTestRenderer.create(null);
root.update(<Wrapper />);
expect(Scheduler).toFlushWithoutYielding();

expect(componentStackMessage.includes('ErrorComponent')).toBe(true);
expect(componentStackMessage.includes('span')).toBe(true);
expect(componentStackMessage.includes('TestEventTarget')).toBe(true);
expect(componentStackMessage.includes('TestEventComponent')).toBe(true);
expect(componentStackMessage.includes('Test')).toBe(true);
expect(componentStackMessage.includes('Wrapper')).toBe(true);
});
});

describe('ReactDOM', () => {
Expand Down Expand Up @@ -1053,6 +1152,54 @@ describe('ReactFiberEvents', () => {
'Warning: validateDOMNesting: React event targets must not have event components as children.',
);
});

it('should error with a component stack contains the names of the event components and event targets', () => {
let componentStackMessage;

function ErrorComponent() {
throw new Error('Failed!');
}

const Test = () => (
<EventComponent>
<EventTarget>
<span>
<ErrorComponent />
</span>
</EventTarget>
</EventComponent>
);

class Wrapper extends React.Component {
state = {
error: null,
};

componentDidCatch(error, errMessage) {
componentStackMessage = errMessage.componentStack;
this.setState({
error,
});
}

render() {
if (this.state.error) {
return null;
}
return <Test />;
}
}

const container = document.createElement('div');
ReactDOM.render(<Wrapper />, container);

expect(componentStackMessage.includes('ErrorComponent')).toBe(true);
expect(componentStackMessage.includes('span')).toBe(true);
expect(componentStackMessage.includes('TestEventTarget')).toBe(true);
expect(componentStackMessage.includes('TestEventComponent')).toBe(true);
expect(componentStackMessage.includes('Test')).toBe(true);
expect(componentStackMessage.includes('Wrapper')).toBe(true);
});
});

describe('ReactDOMServer', () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/ReactTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ export type ReactEventResponder = {

export type ReactEventComponent = {|
$$typeof: Symbol | number,
displayName?: string,
props: null | Object,
responder: ReactEventResponder,
|};

export type ReactEventTarget = {|
$$typeof: Symbol | number,
displayName?: string,
type: Symbol | number,
|};
23 changes: 23 additions & 0 deletions packages/shared/getComponentName.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import {
REACT_STRICT_MODE_TYPE,
REACT_SUSPENSE_TYPE,
REACT_LAZY_TYPE,
REACT_EVENT_COMPONENT_TYPE,
REACT_EVENT_TARGET_TYPE,
REACT_EVENT_TARGET_TOUCH_HIT,
} from 'shared/ReactSymbols';
import {refineResolvedLazyComponent} from 'shared/ReactLazyComponent';
import type {ReactEventComponent, ReactEventTarget} from 'shared/ReactTypes';

function getWrappedName(
outerType: mixed,
Expand Down Expand Up @@ -87,6 +91,25 @@ function getComponentName(type: mixed): string | null {
if (resolvedThenable) {
return getComponentName(resolvedThenable);
}
break;
}
case REACT_EVENT_COMPONENT_TYPE: {
const eventComponent = ((type: any): ReactEventComponent);
const displayName = eventComponent.displayName;
if (displayName !== undefined) {
return displayName;
}
break;
}
case REACT_EVENT_TARGET_TYPE: {
const eventTarget = ((type: any): ReactEventTarget);
if (eventTarget.type === REACT_EVENT_TARGET_TOUCH_HIT) {
return 'TouchHitTarget';
}
const displayName = eventTarget.displayName;
if (displayName !== undefined) {
return displayName;
}
}
}
}
Expand Down

0 comments on commit 89064fe

Please sign in to comment.