* feat: create first version of userEvent.press * feat: do not trigger press event when pointer events is disabled * feat: make press events bubble up when trigerring a non touch responder * tests: add test cases for userevent.press to check calls to onPressIn and onPressOut * refactor: move userEvent.press tests in a dedicated test file * feat: add pressDuration option for userEvent.press * refactor: group test that check prop calls * feat: add support for touchable opacity for userevent press * feat: add support for Text * feat: add support for TextInput for userEvent.press * refactor: add some comments to explain pointer events api * refactor: change the api of userEvent.press to make it async * feat: add support for real timers for userEvent.press * refactor: create a longPress api and remove duration option from press api * feat: add warning for users when userEvent is used with real timers * refactor: rewrite press using common user event code * refactor: remove duplicate pointerEventEnabled method * refactor: remove check on fake timers in user.press * refactor: change order of functions in press file to have exports first * feat: add delay before press * feat: wait min press duration before calling onPressout for text and textInput * chore: improve coverage * feat: account for press duration when waiting for press out * fix: wait for press duration also when pressing text or textinput * docs: add documentation on press and longpress * fix: check pointer events for Text and TextInput * chore: fixes and tweaks on userEvent docs * Update press doc based on review suggestion Co-authored-by: Maciej Jastrzebski <mdjastrzebski@gmail.com> * refactor: rename pressDuration option to duration * Update longPress doc based on review suggestion Co-authored-by: Maciej Jastrzebski <mdjastrzebski@gmail.com> * refactor: use ts doc for isPointerEventsEnabled method * refactor: rename file isPointerEventsEnabled to pointer-events * refactor: split test in longPress in two * refactor: use Date.now instead of new Date().getTime() * refactor: also test payload of events for press and longpress * refactor: fix typo in some longpress test names * Update src/user-event/press/utils/warnAboutRealTimers.ts Co-authored-by: Maciej Jastrzebski <mdjastrzebski@gmail.com> * refactor: merge press and touch events * fix: return after pressing text or textinput * refactor: extract functions to trigger press on text/textInput * refactor: use optional chaining * feat: update warning when using userEvent with real timers * refactor: check on press test that longPress is not called * refactor: test directly warnings logged with real timers without mocking function --------- Co-authored-by: pierrezimmermann <pierrez@nam.tech> Co-authored-by: Maciej Jastrzebski <mdjastrzebski@gmail.com>
1.9 KiB
id, title
| id | title |
|---|---|
| user-event | User Event |
Table of contents
userEvent.setup
userEvent.setup(options?: {
delay: number;
advanceTimers: (delay: number) => Promise<void> | void;
})
Example
const user = userEvent.setup();
Creates User Event instances which can be used to trigger events.
Options
delay- controls the default delay between subsequent events, e.g. keystrokes, etc.advanceTimers- time advancement utility function that should be used for fake timers. The default setup handles both real and Jest fake timers.
press()
press(
element: ReactTestInstance,
): Promise<void>
Example
const user = userEvent.setup();
await user.press(element);
This helper simulates a press on any pressable element, e.g. Pressable, TouchableOpacity, Text, TextInput, etc. Unlike fireEvent.press() which is a simpler API that will only call the onPress prop, this simulates the entire press event in a more realistic way by reproducing what really happens when a user presses an interface view. This will trigger additional events like pressIn and pressOut.
longPress()
longPress(
element: ReactTestInstance,
options: { duration: number } = { duration: 500 }
): Promise<void>
Example
const user = userEvent.setup();
await user.longPress(element);
Simulates a long press user interaction. In React Native the longPress event is emitted when the press duration exceeds long press threshold (by default 500 ms). In other aspects this actions behaves similar to regular press action, e.g. by emitting pressIn and pressOut events. The press duration is customisable 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 test taking a long time to run.