Skip to content

Commit

Permalink
[react-events] Press: improve test coverage (#16397)
Browse files Browse the repository at this point in the history
1. Run the tests in both an environment without PointerEvent and one with PointerEvent.
2. Improve test coverage to include both mouse and touch pointers.
3. Change 'Press' so that it only listens to either pointer events or fallbacks events.
  • Loading branch information
necolas authored Aug 15, 2019
1 parent a9304e7 commit ebd1f5d
Show file tree
Hide file tree
Showing 6 changed files with 858 additions and 1,702 deletions.
65 changes: 33 additions & 32 deletions packages/react-events/src/dom/Press.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,48 +95,47 @@ type PressEvent = {|
shiftKey: boolean,
|};

const hasPointerEvents =
typeof window !== 'undefined' && window.PointerEvent !== undefined;

const isMac =
typeof window !== 'undefined' && window.navigator != null
? /^Mac/.test(window.navigator.platform)
: false;

const DEFAULT_PRESS_RETENTION_OFFSET = {
bottom: 20,
top: 20,
left: 20,
right: 20,
};

const targetEventTypes = [
'keydown_active',
// We need to preventDefault on pointerdown for mouse/pen events
// that are in hit target area but not the element area.
'pointerdown_active',
'click_active',
];
const rootEventTypes = [
'click',
'keyup',
'pointerup',
'pointermove',
'scroll',
'pointercancel',
// We listen to this here so stopPropagation can
// block other mouseup events used internally
'mouseup_active',
'touchend',
];

// 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', 'mousedown');
rootEventTypes.push(
'mousemove',
'touchmove',
'touchcancel',
// Used as a 'cancel' signal for mouse interactions
'dragstart',
);
}
const targetEventTypes = hasPointerEvents
? [
'keydown_active',
// We need to preventDefault on pointerdown for mouse/pen events
// that are in hit target area but not the element area.
'pointerdown_active',
'click_active',
]
: ['keydown_active', 'touchstart', 'mousedown', 'click_active'];

const rootEventTypes = hasPointerEvents
? ['pointerup', 'pointermove', 'pointercancel', 'click', 'keyup', 'scroll']
: [
'click',
'keyup',
'scroll',
'mousemove',
'touchmove',
'touchcancel',
// Used as a 'cancel' signal for mouse interactions
'dragstart',
// We listen to this here so stopPropagation can
// block other mouseup events used internally
'mouseup_active',
'touchend',
];

function isFunction(obj): boolean {
return typeof obj === 'function';
Expand Down Expand Up @@ -539,7 +538,7 @@ const pressResponderImpl = {
}

state.shouldPreventClick = false;
if (isPointerEvent || isTouchEvent) {
if (isTouchEvent) {
state.ignoreEmulatedMouseEvents = true;
} else if (isKeyboardEvent) {
// Ignore unrelated key events
Expand Down Expand Up @@ -676,6 +675,7 @@ const pressResponderImpl = {
if (state.isPressWithinResponderRegion) {
if (isPressed) {
const onPressMove = props.onPressMove;

if (isFunction(onPressMove)) {
dispatchEvent(
event,
Expand Down Expand Up @@ -777,6 +777,7 @@ const pressResponderImpl = {
);
}
}

if (state.isPressWithinResponderRegion && button !== 1) {
dispatchEvent(
event,
Expand Down
24 changes: 12 additions & 12 deletions packages/react-events/src/dom/__tests__/Focus-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
keydown,
setPointerEvent,
platform,
dispatchPointerPressDown,
dispatchPointerPressRelease,
dispatchPointerDown,
dispatchPointerUp,
} from '../test-utils';

let React;
Expand Down Expand Up @@ -138,8 +138,8 @@ describe.each(table)('Focus responder', hasPointerEvents => {

it('is called with the correct pointerType: mouse', () => {
const target = ref.current;
dispatchPointerPressDown(target, {pointerType: 'mouse'});
dispatchPointerPressRelease(target, {pointerType: 'mouse'});
dispatchPointerDown(target, {pointerType: 'mouse'});
dispatchPointerUp(target, {pointerType: 'mouse'});
expect(onFocus).toHaveBeenCalledTimes(1);
expect(onFocus).toHaveBeenCalledWith(
expect.objectContaining({pointerType: 'mouse'}),
Expand All @@ -148,8 +148,8 @@ describe.each(table)('Focus responder', hasPointerEvents => {

it('is called with the correct pointerType: touch', () => {
const target = ref.current;
dispatchPointerPressDown(target, {pointerType: 'touch'});
dispatchPointerPressRelease(target, {pointerType: 'touch'});
dispatchPointerDown(target, {pointerType: 'touch'});
dispatchPointerUp(target, {pointerType: 'touch'});
expect(onFocus).toHaveBeenCalledTimes(1);
expect(onFocus).toHaveBeenCalledWith(
expect.objectContaining({pointerType: 'touch'}),
Expand All @@ -159,8 +159,8 @@ describe.each(table)('Focus responder', hasPointerEvents => {
if (hasPointerEvents) {
it('is called with the correct pointerType: pen', () => {
const target = ref.current;
dispatchPointerPressDown(target, {pointerType: 'pen'});
dispatchPointerPressRelease(target, {pointerType: 'pen'});
dispatchPointerDown(target, {pointerType: 'pen'});
dispatchPointerUp(target, {pointerType: 'pen'});
expect(onFocus).toHaveBeenCalledTimes(1);
expect(onFocus).toHaveBeenCalledWith(
expect.objectContaining({pointerType: 'pen'}),
Expand Down Expand Up @@ -278,7 +278,7 @@ describe.each(table)('Focus responder', hasPointerEvents => {
expect(onFocusVisibleChange).toHaveBeenCalledTimes(1);
expect(onFocusVisibleChange).toHaveBeenCalledWith(true);
// then use pointer on the target, focus should no longer be visible
dispatchPointerPressDown(target);
dispatchPointerDown(target);
expect(onFocusVisibleChange).toHaveBeenCalledTimes(2);
expect(onFocusVisibleChange).toHaveBeenCalledWith(false);
// onFocusVisibleChange should not be called again
Expand All @@ -288,9 +288,9 @@ describe.each(table)('Focus responder', hasPointerEvents => {

it('is not called after "focus" and "blur" events without keyboard', () => {
const target = ref.current;
dispatchPointerPressDown(target);
dispatchPointerPressRelease(target);
dispatchPointerPressDown(container);
dispatchPointerDown(target);
dispatchPointerUp(target);
dispatchPointerDown(container);
target.dispatchEvent(blur({relatedTarget: container}));
expect(onFocusVisibleChange).toHaveBeenCalledTimes(0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
focus,
keydown,
setPointerEvent,
dispatchPointerPressDown,
dispatchPointerPressRelease,
dispatchPointerDown,
dispatchPointerUp,
} from '../test-utils';

let React;
Expand Down Expand Up @@ -203,7 +203,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(1);
expect(onFocusWithinVisibleChange).toHaveBeenCalledWith(true);
// then use pointer on the next target, focus should no longer be visible
dispatchPointerPressDown(innerTarget2);
dispatchPointerDown(innerTarget2);
innerTarget1.dispatchEvent(blur({relatedTarget: innerTarget2}));
innerTarget2.dispatchEvent(focus());
expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(2);
Expand All @@ -215,7 +215,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(3);
expect(onFocusWithinVisibleChange).toHaveBeenCalledWith(true);
// then use pointer on the target, focus should no longer be visible
dispatchPointerPressDown(innerTarget1);
dispatchPointerDown(innerTarget1);
expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(4);
expect(onFocusWithinVisibleChange).toHaveBeenCalledWith(false);
// onFocusVisibleChange should not be called again
Expand All @@ -225,8 +225,8 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {

it('is not called after "focus" and "blur" events without keyboard', () => {
const innerTarget = innerRef.current;
dispatchPointerPressDown(innerTarget);
dispatchPointerPressRelease(innerTarget);
dispatchPointerDown(innerTarget);
dispatchPointerUp(innerTarget);
innerTarget.dispatchEvent(blur({relatedTarget: container}));
expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ describe.each(table)('Hover responder', hasPointerEvents => {

const target = ref.current;
dispatchPointerHoverEnter(target);
dispatchPointerHoverMove(target, {from: {x: 0, y: 0}, to: {x: 1, y: 1}});
dispatchPointerHoverMove(target, {x: 0, y: 0});
dispatchPointerHoverMove(target, {x: 1, y: 1});
expect(onHoverMove).toHaveBeenCalledTimes(2);
expect(onHoverMove).toHaveBeenCalledWith(
expect.objectContaining({type: 'hovermove'}),
Expand Down Expand Up @@ -317,10 +318,8 @@ describe.each(table)('Hover responder', hasPointerEvents => {
const target = ref.current;

dispatchPointerHoverEnter(target, {x: 10, y: 10});
dispatchPointerHoverMove(target, {
from: {x: 10, y: 10},
to: {x: 20, y: 20},
});
dispatchPointerHoverMove(target, {x: 10, y: 10});
dispatchPointerHoverMove(target, {x: 20, y: 20});
dispatchPointerHoverExit(target, {x: 20, y: 20});

expect(eventLog).toEqual([
Expand Down
Loading

0 comments on commit ebd1f5d

Please sign in to comment.