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 more experimental event API warnings #15287

Merged
merged 1 commit into from
Apr 1, 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
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', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export function getChildHostContextForEvent(
): HostContext {
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