|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + * @oncall react_native |
| 10 | + * @fantom_flags delayPressInInScrollView:true |
| 11 | + */ |
| 12 | + |
| 13 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 14 | + |
| 15 | +import type {HostInstance} from 'react-native'; |
| 16 | + |
| 17 | +import * as Fantom from '@react-native/fantom'; |
| 18 | +import nullthrows from 'nullthrows'; |
| 19 | +import * as React from 'react'; |
| 20 | +import {createRef} from 'react'; |
| 21 | +import {Pressable, ScrollView} from 'react-native'; |
| 22 | + |
| 23 | +function render(inScrollView: boolean, onPressIn: () => void): HostInstance { |
| 24 | + const ref = createRef<HostInstance>(); |
| 25 | + const pressable = ( |
| 26 | + <Pressable ref={ref} onPressIn={onPressIn} style={{height: 100}} /> |
| 27 | + ); |
| 28 | + const root = Fantom.createRoot(); |
| 29 | + Fantom.runTask(() => { |
| 30 | + root.render( |
| 31 | + inScrollView ? <ScrollView>{pressable}</ScrollView> : pressable, |
| 32 | + ); |
| 33 | + }); |
| 34 | + return nullthrows(ref.current); |
| 35 | +} |
| 36 | + |
| 37 | +function touch( |
| 38 | + element: HostInstance, |
| 39 | + type: 'onTouchStart' | 'onTouchEnd' | 'onTouchCancel', |
| 40 | +): void { |
| 41 | + const point = {identifier: 0, pageX: 5, pageY: 5, timestamp: 0}; |
| 42 | + Fantom.dispatchNativeEvent( |
| 43 | + element, |
| 44 | + type, |
| 45 | + {touches: type === 'onTouchStart' ? [point] : [], changedTouches: [point]}, |
| 46 | + {category: Fantom.NativeEventCategory.Discrete}, |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +describe('<Pressable> in <ScrollView>', () => { |
| 51 | + let timers: Fantom.TimerMock; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + timers = Fantom.installTimerMock(); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + timers.runAllTimers(); |
| 59 | + timers.uninstall(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('delays onPressIn', () => { |
| 63 | + const onPressIn = jest.fn(); |
| 64 | + const element = render(true, onPressIn); |
| 65 | + |
| 66 | + touch(element, 'onTouchStart'); |
| 67 | + timers.advanceTimersByTime(129); |
| 68 | + expect(onPressIn).toHaveBeenCalledTimes(0); |
| 69 | + |
| 70 | + timers.advanceTimersByTime(1); |
| 71 | + expect(onPressIn).toHaveBeenCalledTimes(1); |
| 72 | + |
| 73 | + touch(element, 'onTouchEnd'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('does not call onPressIn when the touch is canceled before the delay', () => { |
| 77 | + const onPressIn = jest.fn(); |
| 78 | + const element = render(true, onPressIn); |
| 79 | + |
| 80 | + touch(element, 'onTouchStart'); |
| 81 | + touch(element, 'onTouchCancel'); |
| 82 | + timers.runAllTimers(); |
| 83 | + |
| 84 | + expect(onPressIn).toHaveBeenCalledTimes(0); |
| 85 | + }); |
| 86 | + |
| 87 | + it('does not delay onPressIn outside of a scroll view', () => { |
| 88 | + const onPressIn = jest.fn(); |
| 89 | + const element = render(false, onPressIn); |
| 90 | + |
| 91 | + touch(element, 'onTouchStart'); |
| 92 | + expect(onPressIn).toHaveBeenCalledTimes(1); |
| 93 | + |
| 94 | + touch(element, 'onTouchEnd'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments