Skip to content

Commit b6c33d0

Browse files
refactor: remove real timers warning in User Event (#1654)
* refactor: remove real timers warning in User Event * chore: tweak docs
1 parent 239e046 commit b6c33d0

File tree

7 files changed

+11
-104
lines changed

7 files changed

+11
-104
lines changed

src/user-event/press/__tests__/longPress.real-timers.test.tsx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import React from 'react';
22
import { Pressable, Text } from 'react-native';
33
import { render, screen } from '../../../pure';
44
import { userEvent } from '../..';
5-
import * as WarnAboutRealTimers from '../../utils/warn-about-real-timers';
65

76
describe('userEvent.longPress with real timers', () => {
87
beforeEach(() => {
98
jest.useRealTimers();
109
jest.restoreAllMocks();
11-
jest.spyOn(WarnAboutRealTimers, 'warnAboutRealTimersIfNeeded').mockImplementation();
1210
});
1311

1412
test('calls onLongPress if the delayLongPress is the default one', async () => {
@@ -90,18 +88,3 @@ describe('userEvent.longPress with real timers', () => {
9088
expect(mockOnLongPress).toHaveBeenCalled();
9189
});
9290
});
93-
94-
test('warns about using real timers with userEvent', async () => {
95-
jest.restoreAllMocks();
96-
const mockConsoleWarn = jest.spyOn(console, 'warn').mockImplementation();
97-
98-
render(<Pressable testID="pressable" />);
99-
100-
await userEvent.longPress(screen.getByTestId('pressable'));
101-
102-
expect(mockConsoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(`
103-
"It is recommended to use userEvent with fake timers
104-
Some events involve duration so your tests may take a long time to run.
105-
For instance calling userEvent.longPress with real timers will take 500 ms."
106-
`);
107-
});

src/user-event/press/__tests__/press.real-timers.test.tsx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import {
1010
import { createEventLogger, getEventsNames } from '../../../test-utils';
1111
import { render, screen } from '../../..';
1212
import { userEvent } from '../..';
13-
import * as WarnAboutRealTimers from '../../utils/warn-about-real-timers';
1413

1514
describe('userEvent.press with real timers', () => {
1615
beforeEach(() => {
1716
jest.useRealTimers();
1817
jest.restoreAllMocks();
19-
jest.spyOn(WarnAboutRealTimers, 'warnAboutRealTimersIfNeeded').mockImplementation();
2018
});
2119

2220
test('calls onPressIn, onPress and onPressOut prop of touchable', async () => {
@@ -301,18 +299,3 @@ describe('userEvent.press with real timers', () => {
301299
expect(mockOnPress).toHaveBeenCalled();
302300
});
303301
});
304-
305-
test('warns about using real timers with userEvent', async () => {
306-
jest.restoreAllMocks();
307-
const mockConsoleWarn = jest.spyOn(console, 'warn').mockImplementation();
308-
309-
render(<Pressable testID="pressable" />);
310-
311-
await userEvent.press(screen.getByTestId('pressable'));
312-
313-
expect(mockConsoleWarn.mock.calls[0][0]).toMatchInlineSnapshot(`
314-
"It is recommended to use userEvent with fake timers
315-
Some events involve duration so your tests may take a long time to run.
316-
For instance calling userEvent.longPress with real timers will take 500 ms."
317-
`);
318-
});

src/user-event/press/press.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isPointerEventEnabled } from '../../helpers/pointer-events';
66
import { isHostText, isHostTextInput } from '../../helpers/host-component-names';
77
import { EventBuilder } from '../event-builder';
88
import { UserEventConfig, UserEventInstance } from '../setup';
9-
import { dispatchEvent, wait, warnAboutRealTimersIfNeeded } from '../utils';
9+
import { dispatchEvent, wait } from '../utils';
1010
import { DEFAULT_MIN_PRESS_DURATION } from './constants';
1111

1212
export interface PressOptions {
@@ -69,8 +69,6 @@ const emitPressablePressEvents = async (
6969
element: ReactTestInstance,
7070
options: BasePressOptions,
7171
) => {
72-
warnAboutRealTimersIfNeeded();
73-
7472
await wait(config);
7573

7674
dispatchEvent(element, 'responderGrant', EventBuilder.Common.responderGrant());

src/user-event/utils/__tests__/warn-about-real-timers.test.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/user-event/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ export * from './content-size';
22
export * from './dispatch-event';
33
export * from './text-range';
44
export * from './wait';
5-
export * from './warn-about-real-timers';

src/user-event/utils/warn-about-real-timers.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

website/docs/12.x/docs/api/events/user-event.mdx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ await user.press(element);
5353

5454
This helper simulates a press on any pressable element, e.g. `Pressable`, `TouchableOpacity`, `Text`, `TextInput`, etc. Unlike `fireEvent.press()`, a more straightforward API that will only call the `onPress` prop, this function simulates the entire press interaction in a more realistic way by reproducing the event sequence emitted by React Native runtime. This helper will trigger additional events like `pressIn` and `pressOut`.
5555

56-
## `longPress()` \{#long-press}
56+
This event will take a minimum of 130 ms to run due to the internal React Native logic. Consider using fake timers to speed up test execution for tests involving `press` and `longPress` interactions.
57+
58+
## `longPress()`
5759

5860
```ts
5961
longPress(
@@ -69,7 +71,9 @@ const user = userEvent.setup();
6971
await user.longPress(element);
7072
```
7173

72-
Simulates a long press user interaction. In React Native, the `longPress` event is emitted when the press duration exceeds the long press threshold (by default, 500 ms). In other aspects, this action behaves similarly to regular `press` action, e.g., by emitting `pressIn` and `pressOut` events. The press duration is customizable through the options. This should be useful if you use the `delayLongPress` prop. When using real timers, this will take 500 ms, so it is highly recommended to use that API with fake timers to prevent the test from taking a long time to run.
74+
Simulates a long press user interaction. In React Native, the `longPress` event is emitted when the press duration exceeds the long press threshold (by default, 500 ms). In other aspects, this action behaves similarly to regular `press` action, e.g., by emitting `pressIn` and `pressOut` events. The press duration is customizable through the options. This should be useful if you use the `delayLongPress` prop.
75+
76+
This event will, by default, take 500 ms to run. Due to internal React Native logic, it will take at least 130 ms regardless of the duration option passed. Consider using fake timers to speed up test execution for tests involving `press` and `longPress` interactions.
7377

7478
### Options {#longpress-options}
7579

@@ -107,7 +111,7 @@ This function will add text to the text already present in the text input (as sp
107111
- `skipPress` - if true, `pressIn` and `pressOut` events will not be triggered.
108112
- `submitEditing` - if true, `submitEditing` event will be triggered after typing the text.
109113
110-
### Sequence of events
114+
### Sequence of events {#type-sequence}
111115
112116
The sequence of events depends on the `multiline` prop and the passed options.
113117
@@ -156,7 +160,7 @@ This helper simulates the user clearing the content of a `TextInput` element.
156160
157161
This function supports only host `TextInput` elements. Passing other element types will result in throwing an error.
158162
159-
### Sequence of events
163+
### Sequence of events {#clear-sequence}
160164
161165
Events will not be emitted if the `editable` prop is set to `false`.
162166
@@ -200,7 +204,7 @@ This helper simulates the user pasting given text to a `TextInput` element.
200204
201205
This function supports only host `TextInput` elements. Passing other element types will result in throwing an error.
202206
203-
### Sequence of events
207+
### Sequence of events {#paste-sequence}
204208
205209
Events will not be emitted if the `editable` prop is set to `false`.
206210
@@ -278,7 +282,7 @@ This function will remember where the last scroll ended, so subsequent scroll in
278282
279283
To simulate a `FlatList` (and other controls based on `VirtualizedList`) scrolling, you should pass the `contentSize` and `layoutMeasurement` options, which enable the underlying logic to update the currently visible window.
280284
281-
### Sequence of events
285+
### Sequence of events {#scroll-sequence}
282286
283287
The sequence of events depends on whether the scroll includes an optional momentum scroll component.
284288

0 commit comments

Comments
 (0)