Skip to content

Commit

Permalink
Warn on nested EventTragets in experimental event API (facebook#15287)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Apr 1, 2019
1 parent a050f3d commit 9444a54
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ export function getChildHostContextForEvent(
let eventData = null;

if (type === REACT_EVENT_COMPONENT_TYPE) {
warning(
parentHostContextDev.eventData === null ||
!parentHostContextDev.eventData.isEventTarget,
'validateDOMNesting: React event targets must not have event components as children.',
);
eventData = {
isEventComponent: true,
isEventTarget: false,
Expand Down
4 changes: 4 additions & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
) {
if (__DEV__ && enableEventAPI) {
if (type === REACT_EVENT_COMPONENT_TYPE) {
warning(
parentHostContext !== EVENT_TARGET_CONTEXT,
'validateDOMNesting: React event targets must not have event components as children.',
);
return EVENT_COMPONENT_CONTEXT;
} else if (type === REACT_EVENT_TARGET_TYPE) {
warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ describe('ReactFiberEvents', () => {
'Warning: validateDOMNesting: React event targets must be direct children of event components.',
);
});

it('should warn if an event target has an event component as a child', () => {
const Test = () => (
<EventComponent>
<EventTarget>
<EventComponent>
<span>Child 1</span>
</EventComponent>
</EventTarget>
</EventComponent>
);

expect(() => {
ReactNoop.render(<Test />);
expect(Scheduler).toFlushWithoutYielding();
}).toWarnDev(
'Warning: validateDOMNesting: React event targets must not have event components as children.',
);
});
});

describe('TestRenderer', () => {
Expand Down Expand Up @@ -368,6 +387,26 @@ describe('ReactFiberEvents', () => {
'Warning: validateDOMNesting: React event targets must be direct children of event components.',
);
});

it('should warn if an event target has an event component as a child', () => {
const Test = () => (
<EventComponent>
<EventTarget>
<EventComponent>
<span>Child 1</span>
</EventComponent>
</EventTarget>
</EventComponent>
);

const root = ReactTestRenderer.create(null);
expect(() => {
root.update(<Test />);
expect(Scheduler).toFlushWithoutYielding();
}).toWarnDev(
'Warning: validateDOMNesting: React event targets must not have event components as children.',
);
});
});

describe('ReactDOM', () => {
Expand Down Expand Up @@ -535,6 +574,26 @@ describe('ReactFiberEvents', () => {
'Warning: validateDOMNesting: React event targets must be direct children of event components.',
);
});

it('should warn if an event target has an event component as a child', () => {
const Test = () => (
<EventComponent>
<EventTarget>
<EventComponent>
<span>Child 1</span>
</EventComponent>
</EventTarget>
</EventComponent>
);

expect(() => {
const container = document.createElement('div');
ReactDOM.render(<Test />, container);
expect(Scheduler).toFlushWithoutYielding();
}).toWarnDev(
'Warning: validateDOMNesting: React event targets must not have event components as children.',
);
});
});

describe('ReactDOMServer', () => {