MCPcopy
hub / github.com/callstack/react-native-testing-library / clear

Function clear

src/user-event/clear.ts:17–52  ·  view source on GitHub ↗
(this: UserEventInstance, instance: TestInstance)

Source from the content-addressed store, hash-verified

15import { dispatchEvent, wait } from './utils';
16
17export async function clear(this: UserEventInstance, instance: TestInstance): Promise<void> {
18 if (!isHostTextInput(instance)) {
19 throw new ErrorWithStack(
20 `clear() only supports host "TextInput" instances. Passed instance has type: "${instance.type}".`,
21 clear,
22 );
23 }
24
25 if (!isEditableTextInput(instance) || !isPointerEventEnabled(instance)) {
26 return;
27 }
28
29 // 1. Enter instance
30 await dispatchEvent(instance, 'focus', buildFocusEvent());
31
32 // 2. Select all
33 const textToClear = getTextInputValue(instance);
34 const selectionRange = {
35 start: 0,
36 end: textToClear.length,
37 };
38 await dispatchEvent(instance, 'selectionChange', buildTextSelectionChangeEvent(selectionRange));
39
40 // 3. Press backspace with selected text
41 const emptyText = '';
42 await emitTypingEvents(instance, {
43 config: this.config,
44 key: 'Backspace',
45 text: emptyText,
46 });
47
48 // 4. Exit instance
49 await wait(this.config);
50 await dispatchEvent(instance, 'endEditing', buildEndEditingEvent(emptyText));
51 await dispatchEvent(instance, 'blur', buildBlurEvent());
52}

Callers

nothing calls this directly

Calls 11

isHostTextInputFunction · 0.90
isEditableTextInputFunction · 0.90
isPointerEventEnabledFunction · 0.90
dispatchEventFunction · 0.90
buildFocusEventFunction · 0.90
getTextInputValueFunction · 0.90
emitTypingEventsFunction · 0.90
waitFunction · 0.90
buildEndEditingEventFunction · 0.90
buildBlurEventFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…