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

Event propagation test suite #19483

Merged
merged 1 commit into from Jul 29, 2020
Merged

Event propagation test suite #19483

merged 1 commit into from Jul 29, 2020

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Jul 29, 2020

We've been making a series of changes to the event system with the goal of improving interop in the scenarios where a React app is nested into another React app — or even a non-React app. In particular, because React used event delegation to the document, stopPropagation() did not correctly work between two Reacts, as events would not bubble between them.

@trueadm has landed most of the work related to this, but we don't have a very good regression suite for events. We also don't have a place where all the behaviors are documented. So I'm adding a test suite that verifies the expected propagation scenarios, both within and across isolated React roots.

This is a suite of jsdom tests so that we notice regressions early on. Initially, I wrote it as a browser fixture (and it's not hard to convert back), but I found that it wasn't particularly useful since I was emulating the events anyway, and we know well which ones bubble (or don't bubble) in the browsers. So I just hardcoded that in the test.

The suite doesn't include all events, but it includes all kinds of events (edit: there's a few more I need to add) for which we have different behavior. We can add more concrete events to it but I don't think this is a blocker for merging the suite.

Note that the suite would not pass in React 16. It documents the current behavior, with tests verifying that cross-root propagation works as well as it can. React 16 did not do a very good job at that.

I will add some other comments inline.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Jul 29, 2020
});
jest.isolateModules(() => {
InnerReactDOM = require('react-dom');
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to ensure we're actually testing the interop scenario.

});
});

it('onKeyPress', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each event has a separate test case so that it's easy to "focus" on it.

});

it('onKeyPress', () => {
testNativeBubblingEvent({
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the actual tests are a sequence of several in each case. I didn't want to repeat because we want consistent behavior across events in the same group. So we call a helper function that runs all tests. If you want to run one case in particular you can always comment out the calls inside of that function.

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 34bcdb2:

Sandbox Source
React Configuration

@sizebot
Copy link

sizebot commented Jul 29, 2020

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against 34bcdb2

@sizebot
Copy link

sizebot commented Jul 29, 2020

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against 34bcdb2

testNativeBubblingEvent({
type: 'input',
reactEvent: 'onFocus',
nativeEvent: 'focusin',
Copy link
Collaborator Author

@gaearon gaearon Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is interesting. It's in the bubbling category because it's effectively an alias for the browser focusin which does bubble. So it's React event name that's a misnomer.

Copy link
Contributor

@trueadm trueadm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really good. Great work Dan.

In the future, we can added some extended tests around portals too, as those have proven to also be problematic areas around React, especially in regards to how event propagation works (and if it even should work for certain events).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants