Skip to content

Commit

Permalink
[react-events] Refactor unit tests for Hover (#16320)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
necolas authored Aug 9, 2019
1 parent f62b53d commit 07a02fb
Show file tree
Hide file tree
Showing 4 changed files with 670 additions and 532 deletions.
207 changes: 111 additions & 96 deletions packages/react-events/src/dom/Hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {
ReactDOMResponderEvent,
ReactDOMResponderContext,
PointerType,
} from 'shared/ReactDOMTypes';
import type {ReactEventResponderListener} from 'shared/ReactTypes';

Expand All @@ -29,15 +30,14 @@ type HoverState = {
hoverTarget: null | Element | Document,
isActiveHovered: boolean,
isHovered: boolean,
isTouched: boolean,
hoverStartTimeout: null | number,
hoverEndTimeout: null | number,
ignoreEmulatedMouseEvents: boolean,
isTouched?: boolean,
ignoreEmulatedMouseEvents?: boolean,
};

type HoverEventType = 'hoverstart' | 'hoverend' | 'hoverchange' | 'hovermove';

type HoverEvent = {|
pointerType: PointerType,
target: Element | Document,
type: HoverEventType,
timeStamp: number,
Expand All @@ -51,17 +51,8 @@ type HoverEvent = {|
y: null | number,
|};

const targetEventTypes = [
'pointerover',
'pointermove',
'pointerout',
'pointercancel',
];

// If PointerEvents is not supported (e.g., Safari), also listen to touch and mouse events.
if (typeof window !== 'undefined' && window.PointerEvent === undefined) {
targetEventTypes.push('touchstart', 'mouseover', 'mousemove', 'mouseout');
}
const hasPointerEvents =
typeof window !== 'undefined' && window.PointerEvent != null;

function isFunction(obj): boolean {
return typeof obj === 'function';
Expand All @@ -79,13 +70,16 @@ function createHoverEvent(
let pageY = null;
let screenX = null;
let screenY = null;
let pointerType = '';

if (event) {
const nativeEvent = (event.nativeEvent: any);
pointerType = event.pointerType;
({clientX, clientY, pageX, pageY, screenX, screenY} = nativeEvent);
}

return {
pointerType,
target,
type,
timeStamp: context.getTimeStamp(),
Expand Down Expand Up @@ -131,11 +125,6 @@ function dispatchHoverStartEvents(

state.isHovered = true;

if (state.hoverEndTimeout !== null) {
context.clearTimeout(state.hoverEndTimeout);
state.hoverEndTimeout = null;
}

if (!state.isActiveHovered) {
state.isActiveHovered = true;
const onHoverStart = props.onHoverStart;
Expand All @@ -152,6 +141,20 @@ function dispatchHoverStartEvents(
}
}

function dispatchHoverMoveEvent(event, context, props, state) {
const target = state.hoverTarget;
const onHoverMove = props.onHoverMove;
if (isFunction(onHoverMove)) {
const syntheticEvent = createHoverEvent(
event,
context,
'hovermove',
((target: any): Element | Document),
);
context.dispatchEvent(syntheticEvent, onHoverMove, UserBlockingEvent);
}
}

function dispatchHoverEndEvents(
event: null | ReactDOMResponderEvent,
context: ReactDOMResponderContext,
Expand All @@ -170,11 +173,6 @@ function dispatchHoverEndEvents(

state.isHovered = false;

if (state.hoverStartTimeout !== null) {
context.clearTimeout(state.hoverStartTimeout);
state.hoverStartTimeout = null;
}

if (state.isActiveHovered) {
state.isActiveHovered = false;
const onHoverEnd = props.onHoverEnd;
Expand All @@ -189,7 +187,6 @@ function dispatchHoverEndEvents(
}
dispatchHoverChangeEvent(event, context, props, state);
state.hoverTarget = null;
state.ignoreEmulatedMouseEvents = false;
state.isTouched = false;
}
}
Expand All @@ -204,24 +201,17 @@ function unmountResponder(
}
}

function isEmulatedMouseEvent(event, state) {
const {type} = event;
return (
state.ignoreEmulatedMouseEvents &&
(type === 'mousemove' || type === 'mouseover' || type === 'mouseout')
);
}

const hoverResponderImpl = {
targetEventTypes,
targetEventTypes: [
'pointerover',
'pointermove',
'pointerout',
'pointercancel',
],
getInitialState() {
return {
isActiveHovered: false,
isHovered: false,
isTouched: false,
hoverStartTimeout: null,
hoverEndTimeout: null,
ignoreEmulatedMouseEvents: false,
};
},
allowMultipleHostChildren: false,
Expand All @@ -237,95 +227,120 @@ const hoverResponderImpl = {
if (props.disabled) {
if (state.isHovered) {
dispatchHoverEndEvents(event, context, props, state);
state.ignoreEmulatedMouseEvents = false;
}
if (state.isTouched) {
state.isTouched = false;
}
return;
}

switch (type) {
// START
case 'pointerover':
case 'mouseover':
case 'touchstart': {
if (!state.isHovered) {
// Prevent hover events for touch
if (state.isTouched || pointerType === 'touch') {
state.isTouched = true;
return;
}

// Prevent hover events for emulated events
if (isEmulatedMouseEvent(event, state)) {
return;
}
case 'pointerover': {
if (!state.isHovered && pointerType !== 'touch') {
state.hoverTarget = event.responderTarget;
state.ignoreEmulatedMouseEvents = true;
dispatchHoverStartEvents(event, context, props, state);
}
return;
break;
}

// MOVE
case 'pointermove':
case 'mousemove': {
if (state.isHovered && !isEmulatedMouseEvent(event, state)) {
const onHoverMove = props.onHoverMove;
if (state.hoverTarget !== null && isFunction(onHoverMove)) {
const syntheticEvent = createHoverEvent(
event,
context,
'hovermove',
state.hoverTarget,
);
context.dispatchEvent(
syntheticEvent,
onHoverMove,
UserBlockingEvent,
);
}
case 'pointermove': {
if (state.isHovered && state.hoverTarget !== null) {
dispatchHoverMoveEvent(event, context, props, state);
}
return;
break;
}

// END
case 'pointerout':
case 'pointercancel':
case 'mouseout':
case 'touchcancel':
case 'touchend': {
case 'pointercancel': {
if (state.isHovered) {
dispatchHoverEndEvents(event, context, props, state);
state.ignoreEmulatedMouseEvents = false;
}
if (state.isTouched) {
state.isTouched = false;
}
return;
break;
}
}
},
onUnmount(
context: ReactDOMResponderContext,
props: HoverProps,
state: HoverState,
) {
unmountResponder(context, props, state);
onUnmount: unmountResponder,
onOwnershipChange: unmountResponder,
};

const hoverResponderFallbackImpl = {
targetEventTypes: ['mouseover', 'mousemove', 'mouseout', 'touchstart'],
getInitialState() {
return {
isActiveHovered: false,
isHovered: false,
isTouched: false,
ignoreEmulatedMouseEvents: false,
};
},
onOwnershipChange(
allowMultipleHostChildren: false,
allowEventHooks: true,
onEvent(
event: ReactDOMResponderEvent,
context: ReactDOMResponderContext,
props: HoverProps,
state: HoverState,
) {
unmountResponder(context, props, state);
): void {
const {type} = event;

if (props.disabled) {
if (state.isHovered) {
dispatchHoverEndEvents(event, context, props, state);
state.ignoreEmulatedMouseEvents = false;
}
state.isTouched = false;
return;
}

switch (type) {
// START
case 'mouseover': {
if (!state.isHovered && !state.ignoreEmulatedMouseEvents) {
state.hoverTarget = event.responderTarget;
dispatchHoverStartEvents(event, context, props, state);
}
break;
}

// MOVE
case 'mousemove': {
if (
state.isHovered &&
state.hoverTarget !== null &&
!state.ignoreEmulatedMouseEvents
) {
dispatchHoverMoveEvent(event, context, props, state);
} else if (!state.isHovered && type === 'mousemove') {
state.ignoreEmulatedMouseEvents = false;
state.isTouched = false;
}
break;
}

// END
case 'mouseout': {
if (state.isHovered) {
dispatchHoverEndEvents(event, context, props, state);
}
break;
}

case 'touchstart': {
if (!state.isHovered) {
state.isTouched = true;
state.ignoreEmulatedMouseEvents = true;
}
break;
}
}
},
onUnmount: unmountResponder,
onOwnershipChange: unmountResponder,
};

export const HoverResponder = React.unstable_createResponder(
'Hover',
hoverResponderImpl,
hasPointerEvents ? hoverResponderImpl : hoverResponderFallbackImpl,
);

export function useHoverResponder(
Expand Down
Loading

0 comments on commit 07a02fb

Please sign in to comment.