Skip to content

Commit 762b2f5

Browse files
committed
refactor: modify pressable implemenatation
1 parent 064a484 commit 762b2f5

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/user-event/press/press.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ReactTestInstance } from 'react-test-renderer';
2+
import act from '../../act';
23
import { getHostParent } from '../../helpers/component-tree';
34
import { isTextInputEditable } from '../../helpers/text-input';
45
import { isPointerEventEnabled } from '../../helpers/pointer-events';
@@ -18,7 +19,7 @@ export interface PressOptions {
1819
export async function press(this: UserEventInstance, element: ReactTestInstance): Promise<void> {
1920
await basePress(this.config, element, {
2021
type: 'press',
21-
duration: 0,
22+
duration: DEFAULT_MIN_PRESS_DURATION,
2223
});
2324
}
2425

@@ -75,14 +76,18 @@ const emitPressablePressEvents = async (
7576

7677
dispatchEvent(element, 'responderGrant', EventBuilder.Common.responderGrant());
7778

78-
// We apply minimum press duration here to ensure that `press` events are emitted after `pressOut`.
79-
// Otherwise, pressables would emit them in the reverse order, which in reality happens only for
80-
// very short presses (< 130ms) and contradicts the React Native docs.
81-
// See: https://reactnative.dev/docs/pressable#onpress
82-
let duration = Math.max(options.duration, DEFAULT_MIN_PRESS_DURATION);
83-
await wait(config, duration);
79+
await wait(config, options.duration);
8480

8581
dispatchEvent(element, 'responderRelease', EventBuilder.Common.responderRelease());
82+
83+
// React Native will wait for minimal delay of DEFAULT_MIN_PRESS_DURATION
84+
// before emitting the `pressOut` event. We need to wait here, so that
85+
// `press()` function does not return before that.
86+
if (DEFAULT_MIN_PRESS_DURATION - options.duration > 0) {
87+
await act(async () => {
88+
await wait(config, DEFAULT_MIN_PRESS_DURATION - options.duration);
89+
});
90+
}
8691
};
8792

8893
const isEnabledTouchResponder = (element: ReactTestInstance) => {

0 commit comments

Comments
 (0)