-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pointer event test ensuring that pointerover events are fired whe…
…n an element appears under the cursor Summary: Changelog: [RNTester][Internal] - Add pointer event test ensuring that pointerover events are fired when an element appears under the cursor This diff adds a port of an existing web platform test that checks if pointerover & pointerenter events are fired for elements that are mounted underneath the cursor. Reviewed By: lunaleaps Differential Revision: D40449499 fbshipit-source-id: 393f4325f7a9d9025fe9d0aab38642b1e3ab2612
- Loading branch information
1 parent
1546666
commit f5bd272
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
...xperimental/W3CPointerEventPlatformTests/PointerEventLayoutChangeShouldFirePointerOver.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
import type {PlatformTestComponentBaseProps} from '../PlatformTest/RNTesterPlatformTestTypes'; | ||
import type {PointerEvent} from 'react-native/Libraries/Types/CoreEventTypes'; | ||
|
||
import RNTesterPlatformTest from '../PlatformTest/RNTesterPlatformTest'; | ||
import * as React from 'react'; | ||
import {useRef, useCallback, useState, useMemo} from 'react'; | ||
import {StyleSheet, View} from 'react-native'; | ||
|
||
const styles = StyleSheet.create({ | ||
spacer: { | ||
width: 100, | ||
height: 100, | ||
}, | ||
red: { | ||
backgroundColor: 'red', | ||
position: 'absolute', | ||
left: 0, | ||
top: 0, | ||
width: 100, | ||
height: 100, | ||
zIndex: 0, | ||
}, | ||
blue: { | ||
backgroundColor: 'blue', | ||
position: 'absolute', | ||
left: 0, | ||
top: 0, | ||
width: 100, | ||
height: 100, | ||
zIndex: 1, | ||
}, | ||
}); | ||
|
||
// adapted from https://github.com/web-platform-tests/wpt/blob/master/uievents/mouse/layout_change_should_fire_mouseover.html | ||
function PointerEventLayoutShouldFirePointerOverTestCase( | ||
props: PlatformTestComponentBaseProps, | ||
) { | ||
const {harness} = props; | ||
|
||
const testMouseOver = harness.useAsyncTest( | ||
'Tests that the pointerover event is fired and the element has a hover effect when the element underneath the mouse cursor is changed.', | ||
); | ||
|
||
const [showBlue, setShowBlue] = useState(false); | ||
|
||
const eventListRef = useRef([]); | ||
|
||
const checkEventSequence = useCallback(() => { | ||
testMouseOver.step(({assert_equals}) => { | ||
const result = eventListRef.current.join(','); | ||
assert_equals( | ||
result, | ||
'pointerover,pointerenter', | ||
'The only events recorded should be pointerover and pointerenter (in that order)', | ||
); | ||
}); | ||
testMouseOver.done(); | ||
}, [testMouseOver]); | ||
|
||
const redClickHandler = useCallback(() => { | ||
setShowBlue(true); | ||
setTimeout(() => { | ||
checkEventSequence(); | ||
}, 2500); | ||
}, [checkEventSequence]); | ||
|
||
const createBlueHoverEventHandler = useCallback( | ||
(eventType: string) => (event: PointerEvent) => { | ||
eventListRef.current.push(eventType); | ||
testMouseOver.step(({assert_equals}) => { | ||
if (eventType === 'pointerenter') { | ||
checkEventSequence(); | ||
} | ||
}); | ||
}, | ||
[checkEventSequence, testMouseOver], | ||
); | ||
|
||
const blueEventHandlers = useMemo( | ||
() => ({ | ||
onPointerOver: createBlueHoverEventHandler('pointerover'), | ||
onPointerMove: createBlueHoverEventHandler('pointermove'), | ||
onPointerEnter: createBlueHoverEventHandler('pointerenter'), | ||
onPointerLeave: createBlueHoverEventHandler('pointerleave'), | ||
}), | ||
[createBlueHoverEventHandler], | ||
); | ||
|
||
return ( | ||
<> | ||
<View style={styles.spacer} /> | ||
<View onPointerUp={redClickHandler} style={styles.red} /> | ||
{showBlue && <View {...blueEventHandlers} style={styles.blue} />} | ||
</> | ||
); | ||
} | ||
|
||
type Props = $ReadOnly<{}>; | ||
export default function PointerEventLayoutShouldFirePointerOver( | ||
props: Props, | ||
): React.MixedElement { | ||
return ( | ||
<RNTesterPlatformTest | ||
component={PointerEventLayoutShouldFirePointerOverTestCase} | ||
description="Tests that the pointerover event is fired and the element has a hover effect when the element underneath the mouse cursor is changed." | ||
instructions={[ | ||
'Put your mouse over the red rectangle', | ||
'Click the primary mouse button', | ||
]} | ||
title="Pointerover/enter is sent on layout change" | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters