GeezerVison is showing github.com — [react-events] Press: improve test coverage (#16397) · react/react@ebd1f5d
File treeExpand file treeCollapse file tree Open diff view settings packages/react-events/src/dom
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/Press.js +33-32Lines changed: 33 additions & 32 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -95,48 +95,47 @@ type PressEvent = {| | 95 | 95 | shiftKey: boolean, | 96 | 96 | |}; | 97 | 97 |
| | 98 | +const hasPointerEvents = | | 99 | + typeof window !== 'undefined' && window.PointerEvent !== undefined; | | 100 | + | 98 | 101 | const isMac = | 99 | 102 | typeof window !== 'undefined' && window.navigator != null | 100 | 103 | ? /^Mac/.test(window.navigator.platform) | 101 | 104 | : false; | | 105 | + | 102 | 106 | const DEFAULT_PRESS_RETENTION_OFFSET = { | 103 | 107 | bottom: 20, | 104 | 108 | top: 20, | 105 | 109 | left: 20, | 106 | 110 | right: 20, | 107 | 111 | }; | 108 | 112 |
| 109 | | -const targetEventTypes = [ | 110 | | - 'keydown_active', | 111 | | - // We need to preventDefault on pointerdown for mouse/pen events | 112 | | - // that are in hit target area but not the element area. | 113 | | - 'pointerdown_active', | 114 | | - 'click_active', | 115 | | -]; | 116 | | -const rootEventTypes = [ | 117 | | - 'click', | 118 | | - 'keyup', | 119 | | - 'pointerup', | 120 | | - 'pointermove', | 121 | | - 'scroll', | 122 | | - 'pointercancel', | 123 | | - // We listen to this here so stopPropagation can | 124 | | - // block other mouseup events used internally | 125 | | - 'mouseup_active', | 126 | | - 'touchend', | 127 | | -]; | 128 | | - | 129 | | -// If PointerEvents is not supported (e.g., Safari), also listen to touch and mouse events. | 130 | | -if (typeof window !== 'undefined' && window.PointerEvent === undefined) { | 131 | | - targetEventTypes.push('touchstart', 'mousedown'); | 132 | | - rootEventTypes.push( | 133 | | - 'mousemove', | 134 | | - 'touchmove', | 135 | | - 'touchcancel', | 136 | | - // Used as a 'cancel' signal for mouse interactions | 137 | | - 'dragstart', | 138 | | - ); | 139 | | -} | | 113 | +const targetEventTypes = hasPointerEvents | | 114 | + ? [ | | 115 | + 'keydown_active', | | 116 | + // We need to preventDefault on pointerdown for mouse/pen events | | 117 | + // that are in hit target area but not the element area. | | 118 | + 'pointerdown_active', | | 119 | + 'click_active', | | 120 | + ] | | 121 | + : ['keydown_active', 'touchstart', 'mousedown', 'click_active']; | | 122 | + | | 123 | +const rootEventTypes = hasPointerEvents | | 124 | + ? ['pointerup', 'pointermove', 'pointercancel', 'click', 'keyup', 'scroll'] | | 125 | + : [ | | 126 | + 'click', | | 127 | + 'keyup', | | 128 | + 'scroll', | | 129 | + 'mousemove', | | 130 | + 'touchmove', | | 131 | + 'touchcancel', | | 132 | + // Used as a 'cancel' signal for mouse interactions | | 133 | + 'dragstart', | | 134 | + // We listen to this here so stopPropagation can | | 135 | + // block other mouseup events used internally | | 136 | + 'mouseup_active', | | 137 | + 'touchend', | | 138 | + ]; | 140 | 139 |
| 141 | 140 | function isFunction(obj): boolean { | 142 | 141 | return typeof obj === 'function'; | @@ -539,7 +538,7 @@ const pressResponderImpl = { | 539 | 538 | } | 540 | 539 |
| 541 | 540 | state.shouldPreventClick = false; | 542 | | - if (isPointerEvent || isTouchEvent) { | | 541 | + if (isTouchEvent) { | 543 | 542 | state.ignoreEmulatedMouseEvents = true; | 544 | 543 | } else if (isKeyboardEvent) { | 545 | 544 | // Ignore unrelated key events | @@ -676,6 +675,7 @@ const pressResponderImpl = { | 676 | 675 | if (state.isPressWithinResponderRegion) { | 677 | 676 | if (isPressed) { | 678 | 677 | const onPressMove = props.onPressMove; | | 678 | + | 679 | 679 | if (isFunction(onPressMove)) { | 680 | 680 | dispatchEvent( | 681 | 681 | event, | @@ -777,6 +777,7 @@ const pressResponderImpl = { | 777 | 777 | ); | 778 | 778 | } | 779 | 779 | } | | 780 | + | 780 | 781 | if (state.isPressWithinResponderRegion && button !== 1) { | 781 | 782 | dispatchEvent( | 782 | 783 | event, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/__tests__/Focus-test.internal.js +12-12Lines changed: 12 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,8 +15,8 @@ import { | 15 | 15 | keydown, | 16 | 16 | setPointerEvent, | 17 | 17 | platform, | 18 | | - dispatchPointerPressDown, | 19 | | - dispatchPointerPressRelease, | | 18 | + dispatchPointerDown, | | 19 | + dispatchPointerUp, | 20 | 20 | } from '../test-utils'; | 21 | 21 |
| 22 | 22 | let React; | @@ -138,8 +138,8 @@ describe.each(table)('Focus responder', hasPointerEvents => { | 138 | 138 |
| 139 | 139 | it('is called with the correct pointerType: mouse', () => { | 140 | 140 | const target = ref.current; | 141 | | - dispatchPointerPressDown(target, {pointerType: 'mouse'}); | 142 | | - dispatchPointerPressRelease(target, {pointerType: 'mouse'}); | | 141 | + dispatchPointerDown(target, {pointerType: 'mouse'}); | | 142 | + dispatchPointerUp(target, {pointerType: 'mouse'}); | 143 | 143 | expect(onFocus).toHaveBeenCalledTimes(1); | 144 | 144 | expect(onFocus).toHaveBeenCalledWith( | 145 | 145 | expect.objectContaining({pointerType: 'mouse'}), | @@ -148,8 +148,8 @@ describe.each(table)('Focus responder', hasPointerEvents => { | 148 | 148 |
| 149 | 149 | it('is called with the correct pointerType: touch', () => { | 150 | 150 | const target = ref.current; | 151 | | - dispatchPointerPressDown(target, {pointerType: 'touch'}); | 152 | | - dispatchPointerPressRelease(target, {pointerType: 'touch'}); | | 151 | + dispatchPointerDown(target, {pointerType: 'touch'}); | | 152 | + dispatchPointerUp(target, {pointerType: 'touch'}); | 153 | 153 | expect(onFocus).toHaveBeenCalledTimes(1); | 154 | 154 | expect(onFocus).toHaveBeenCalledWith( | 155 | 155 | expect.objectContaining({pointerType: 'touch'}), | @@ -159,8 +159,8 @@ describe.each(table)('Focus responder', hasPointerEvents => { | 159 | 159 | if (hasPointerEvents) { | 160 | 160 | it('is called with the correct pointerType: pen', () => { | 161 | 161 | const target = ref.current; | 162 | | - dispatchPointerPressDown(target, {pointerType: 'pen'}); | 163 | | - dispatchPointerPressRelease(target, {pointerType: 'pen'}); | | 162 | + dispatchPointerDown(target, {pointerType: 'pen'}); | | 163 | + dispatchPointerUp(target, {pointerType: 'pen'}); | 164 | 164 | expect(onFocus).toHaveBeenCalledTimes(1); | 165 | 165 | expect(onFocus).toHaveBeenCalledWith( | 166 | 166 | expect.objectContaining({pointerType: 'pen'}), | @@ -278,7 +278,7 @@ describe.each(table)('Focus responder', hasPointerEvents => { | 278 | 278 | expect(onFocusVisibleChange).toHaveBeenCalledTimes(1); | 279 | 279 | expect(onFocusVisibleChange).toHaveBeenCalledWith(true); | 280 | 280 | // then use pointer on the target, focus should no longer be visible | 281 | | - dispatchPointerPressDown(target); | | 281 | + dispatchPointerDown(target); | 282 | 282 | expect(onFocusVisibleChange).toHaveBeenCalledTimes(2); | 283 | 283 | expect(onFocusVisibleChange).toHaveBeenCalledWith(false); | 284 | 284 | // onFocusVisibleChange should not be called again | @@ -288,9 +288,9 @@ describe.each(table)('Focus responder', hasPointerEvents => { | 288 | 288 |
| 289 | 289 | it('is not called after "focus" and "blur" events without keyboard', () => { | 290 | 290 | const target = ref.current; | 291 | | - dispatchPointerPressDown(target); | 292 | | - dispatchPointerPressRelease(target); | 293 | | - dispatchPointerPressDown(container); | | 291 | + dispatchPointerDown(target); | | 292 | + dispatchPointerUp(target); | | 293 | + dispatchPointerDown(container); | 294 | 294 | target.dispatchEvent(blur({relatedTarget: container})); | 295 | 295 | expect(onFocusVisibleChange).toHaveBeenCalledTimes(0); | 296 | 296 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/__tests__/FocusWithin-test.internal.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,8 +14,8 @@ import { | 14 | 14 | focus, | 15 | 15 | keydown, | 16 | 16 | setPointerEvent, | 17 | | - dispatchPointerPressDown, | 18 | | - dispatchPointerPressRelease, | | 17 | + dispatchPointerDown, | | 18 | + dispatchPointerUp, | 19 | 19 | } from '../test-utils'; | 20 | 20 |
| 21 | 21 | let React; | @@ -203,7 +203,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => { | 203 | 203 | expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(1); | 204 | 204 | expect(onFocusWithinVisibleChange).toHaveBeenCalledWith(true); | 205 | 205 | // then use pointer on the next target, focus should no longer be visible | 206 | | - dispatchPointerPressDown(innerTarget2); | | 206 | + dispatchPointerDown(innerTarget2); | 207 | 207 | innerTarget1.dispatchEvent(blur({relatedTarget: innerTarget2})); | 208 | 208 | innerTarget2.dispatchEvent(focus()); | 209 | 209 | expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(2); | @@ -215,7 +215,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => { | 215 | 215 | expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(3); | 216 | 216 | expect(onFocusWithinVisibleChange).toHaveBeenCalledWith(true); | 217 | 217 | // then use pointer on the target, focus should no longer be visible | 218 | | - dispatchPointerPressDown(innerTarget1); | | 218 | + dispatchPointerDown(innerTarget1); | 219 | 219 | expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(4); | 220 | 220 | expect(onFocusWithinVisibleChange).toHaveBeenCalledWith(false); | 221 | 221 | // onFocusVisibleChange should not be called again | @@ -225,8 +225,8 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => { | 225 | 225 |
| 226 | 226 | it('is not called after "focus" and "blur" events without keyboard', () => { | 227 | 227 | const innerTarget = innerRef.current; | 228 | | - dispatchPointerPressDown(innerTarget); | 229 | | - dispatchPointerPressRelease(innerTarget); | | 228 | + dispatchPointerDown(innerTarget); | | 229 | + dispatchPointerUp(innerTarget); | 230 | 230 | innerTarget.dispatchEvent(blur({relatedTarget: container})); | 231 | 231 | expect(onFocusWithinVisibleChange).toHaveBeenCalledTimes(0); | 232 | 232 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-events/src/dom/__tests__/Hover-test.internal.js +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -213,7 +213,8 @@ describe.each(table)('Hover responder', hasPointerEvents => { | 213 | 213 |
| 214 | 214 | const target = ref.current; | 215 | 215 | dispatchPointerHoverEnter(target); | 216 | | - dispatchPointerHoverMove(target, {from: {x: 0, y: 0}, to: {x: 1, y: 1}}); | | 216 | + dispatchPointerHoverMove(target, {x: 0, y: 0}); | | 217 | + dispatchPointerHoverMove(target, {x: 1, y: 1}); | 217 | 218 | expect(onHoverMove).toHaveBeenCalledTimes(2); | 218 | 219 | expect(onHoverMove).toHaveBeenCalledWith( | 219 | 220 | expect.objectContaining({type: 'hovermove'}), | @@ -317,10 +318,8 @@ describe.each(table)('Hover responder', hasPointerEvents => { | 317 | 318 | const target = ref.current; | 318 | 319 |
| 319 | 320 | dispatchPointerHoverEnter(target, {x: 10, y: 10}); | 320 | | - dispatchPointerHoverMove(target, { | 321 | | - from: {x: 10, y: 10}, | 322 | | - to: {x: 20, y: 20}, | 323 | | - }); | | 321 | + dispatchPointerHoverMove(target, {x: 10, y: 10}); | | 322 | + dispatchPointerHoverMove(target, {x: 20, y: 20}); | 324 | 323 | dispatchPointerHoverExit(target, {x: 20, y: 20}); | 325 | 324 |
| 326 | 325 | expect(eventLog).toEqual([ | |
|