Skip to content

[react-events] Refactor unit tests for Hover #16320

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

Merged
merged 2 commits into from
Aug 9, 2019

Conversation

necolas
Copy link
Contributor

@necolas necolas commented Aug 8, 2019

First pass at refactoring the tests and responders. This only touches Hover.

Demo: https://codesandbox.io/s/refactor-hover-responder-f9k2q

Problem

The existing responders listen to pointer events by default and add fallback events if PointerEvent is not supported. However, this complicates the responders and makes it easy to create a problematic unit test environment. jsdom doesn't support PointerEvent, which means that the responders end up listening to pointer events and fallback events in unit tests. This isn't a direct problem in production environments, because no browser will fire pointer events if they aren't supported. But in the unit test environment, we often dispatch event sequences taken from browsers that support pointer events. This means that what we're often testing is actually a (complex) scenario that cannot even occur in production: a responder that is listening to and receives both pointer events and fallback events. Not only does this risk making responders more complicated to implement but it could also hide bugs in implementations.

Response

Implement the responders so that they're only listening to either pointer events or fallback events, never both. This should make the default pointer events implementations significantly simpler and easier to test, as well as free to rely on the complete PointerEvents API. In the future it should also make DCE easier for target environments that are known to support PointerEvents, as we can use build tools with an equivalent of the runtime check. The fallback events (touch and mouse) need to coexist and be resilient to browser emulated events. Our unit tests should express a suite of high-level interactions that can be run in environments with and without PointerEvents support.

**Problem**

The existing responders listen to pointer events by default and add fallback events if PointerEvent is not supported. However, this complicates the responders and makes it easy to create a problematic unit test environment. jsdom doesn't support PointerEvent, which means that the responders end up listening to pointer events *and* fallback events in unit tests. This isn't a direct problem in production environments, because no browser will fire pointer events if they aren't supported. But in the unit test environment, we often dispatch event sequences taken from browsers that support pointer events. This means that what we're often testing is actually a (complex) scenario that cannot even occur in production: a responder that is listening to and receives both pointer events and fallback events. Not only does this risk making responders more complicated to implement but it could also hide bugs in implementations.

**Response**

Implement the responders so that they're only listening to *either* pointer events *or* fallback events, never both. This should make the default pointer events implementations significantly simpler and easier to test, as well as free to rely on the complete PointerEvents API. In the future it should also make DCE easier for target environments that are known to support PointerEvents, as we can use build tools with an equivalent of the runtime check. The fallback events (touch and mouse) need to coexist and be resilient to browser emulated events. Our unit tests should express a suite of high-level interactions that can be run in environments with and without PointerEvents support.
@necolas necolas requested a review from trueadm August 8, 2019 18:15
@necolas necolas changed the title [react-events] Refactor unit tests [react-events] Refactor unit tests for Hover Aug 8, 2019
@necolas necolas force-pushed the react-events/unit-test-refactor branch 2 times, most recently from 1d92bdb to 1d268aa Compare August 8, 2019 18:20
@sizebot
Copy link

sizebot commented Aug 8, 2019

Details of bundled changes.

Comparing: 12be893...6d9ae87

react-events

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-events-hover.development.js +4.6% -8.2% 6.77 KB 7.08 KB 1.7 KB 1.56 KB UMD_DEV
react-events-hover.production.min.js 🔺+3.6% -5.5% 3.04 KB 3.15 KB 1.21 KB 1.14 KB UMD_PROD
react-events-drag.production.min.js 0.0% -0.1% 2.48 KB 2.48 KB 1.14 KB 1.14 KB UMD_PROD
react-events-keyboard.production.min.js 0.0% 🔺+0.1% 1.88 KB 1.88 KB 1.01 KB 1.01 KB UMD_PROD
react-events-drag.production.min.js 0.0% -0.1% 3.12 KB 3.12 KB 1.45 KB 1.45 KB NODE_PROD
react-events-context-menu.development.js 0.0% -0.2% 2.69 KB 2.69 KB 1002 B 1000 B UMD_DEV
react-events-input.development.js 0.0% -0.1% 4.54 KB 4.54 KB 1.43 KB 1.43 KB UMD_DEV
react-events-swipe.development.js 0.0% +0.1% 6.59 KB 6.59 KB 1.75 KB 1.75 KB UMD_DEV
react-events-context-menu.production.min.js 0.0% -0.1% 1.39 KB 1.39 KB 726 B 725 B UMD_PROD
react-events-input.production.min.js 0.0% -0.1% 1.82 KB 1.82 KB 973 B 972 B UMD_PROD
react-events-context-menu.development.js 0.0% -0.1% 2.5 KB 2.5 KB 958 B 957 B NODE_DEV
react-events-input.development.js 0.0% -0.1% 4.35 KB 4.35 KB 1.39 KB 1.38 KB NODE_DEV
react-events-swipe.development.js 0.0% +0.1% 6.41 KB 6.41 KB 1.71 KB 1.71 KB NODE_DEV
react-events-context-menu.production.min.js 0.0% -0.2% 1.2 KB 1.2 KB 666 B 665 B NODE_PROD
react-events-input.production.min.js 0.0% -0.1% 1.65 KB 1.65 KB 907 B 906 B NODE_PROD
react-events-swipe.production.min.js 0.0% 🔺+0.1% 2.45 KB 2.45 KB 1.14 KB 1.14 KB NODE_PROD
react-events-hover.development.js +4.8% -8.5% 6.59 KB 6.9 KB 1.66 KB 1.52 KB NODE_DEV
react-events-hover.production.min.js 🔺+4.0% -5.9% 2.86 KB 2.97 KB 1.15 KB 1.08 KB NODE_PROD
react-events-focus.development.js 0.0% -0.0% 10.16 KB 10.16 KB 2.12 KB 2.12 KB UMD_DEV
react-events-press.development.js 0.0% 0.0% 21.69 KB 21.69 KB 5.04 KB 5.04 KB UMD_DEV
ReactEventsHover-dev.js +2.7% -8.9% 6.66 KB 6.84 KB 1.68 KB 1.53 KB FB_WWW_DEV
react-events-press.production.min.js 0.0% 0.0% 7.09 KB 7.09 KB 2.69 KB 2.69 KB UMD_PROD
ReactEventsHover-prod.js 🔺+4.0% -7.0% 5.8 KB 6.04 KB 1.49 KB 1.39 KB FB_WWW_PROD
react-events-focus.development.js 0.0% -0.0% 9.98 KB 9.98 KB 2.07 KB 2.07 KB NODE_DEV
react-events-press.development.js 0.0% 0.0% 21.5 KB 21.5 KB 4.99 KB 4.99 KB NODE_DEV
react-events-press.production.min.js 0.0% 0.0% 6.91 KB 6.91 KB 2.63 KB 2.63 KB NODE_PROD

Generated by 🚫 dangerJS

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.

Added a nit, but otherwise, awesome stuff!

@necolas necolas force-pushed the react-events/unit-test-refactor branch from 1d268aa to 6d9ae87 Compare August 9, 2019 17:48
@necolas necolas merged commit 07a02fb into facebook:master Aug 9, 2019
@necolas necolas deleted the react-events/unit-test-refactor branch December 20, 2019 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants