Skip to content
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

Adds displayName to EventComponent and EventTarget #15268

Merged
merged 6 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -182,6 +182,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 @@ -96,6 +96,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 @@ -174,6 +174,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 @@ -373,6 +373,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 @@ -212,6 +212,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 @@ -392,4 +392,8 @@ describe('Event responder: Press', () => {
expect(events).toEqual(['keydown', 'inner: 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) {
trueadm marked this conversation as resolved.
Show resolved Hide resolved
return 'TouchHitTarget';
}
const displayName = eventTarget.displayName;
if (displayName !== undefined) {
return displayName;
}
}
}
}
Expand Down