MCPcopy Index your code
hub / github.com/callstack/react-native-testing-library / paste

Function paste

src/user-event/paste.ts:19–63  ·  view source on GitHub ↗
(
  this: UserEventInstance,
  instance: TestInstance,
  text: string,
)

Source from the content-addressed store, hash-verified

17import { dispatchEvent, getTextContentSize, wait } from './utils';
18
19export async function paste(
20 this: UserEventInstance,
21 instance: TestInstance,
22 text: string,
23): Promise<void> {
24 if (!isHostTextInput(instance)) {
25 throw new ErrorWithStack(
26 `paste() only supports host "TextInput" instances. Passed instance has type: "${instance.type}".`,
27 paste,
28 );
29 }
30
31 if (!isEditableTextInput(instance) || !isPointerEventEnabled(instance)) {
32 return;
33 }
34
35 // 1. Enter instance
36 await dispatchEvent(instance, 'focus', buildFocusEvent());
37
38 // 2. Select all
39 const textToClear = getTextInputValue(instance);
40 const rangeToClear = { start: 0, end: textToClear.length };
41 await dispatchEvent(instance, 'selectionChange', buildTextSelectionChangeEvent(rangeToClear));
42
43 // 3. Paste the text
44 nativeState.valueForInstance.set(instance, text);
45
46 const rangeAfter = { start: text.length, end: text.length };
47 await dispatchEvent(instance, 'change', buildTextChangeEvent(text, rangeAfter));
48 await dispatchEvent(instance, 'changeText', text);
49 await dispatchEvent(instance, 'selectionChange', buildTextSelectionChangeEvent(rangeAfter));
50
51 // According to the docs only multiline TextInput emits contentSizeChange event
52 // @see: https://reactnative.dev/docs/textinput#oncontentsizechange
53 const isMultiline = instance.props.multiline === true;
54 if (isMultiline) {
55 const contentSize = getTextContentSize(text);
56 await dispatchEvent(instance, 'contentSizeChange', buildContentSizeChangeEvent(contentSize));
57 }
58
59 // 4. Exit instance
60 await wait(this.config);
61 await dispatchEvent(instance, 'endEditing', buildEndEditingEvent(text));
62 await dispatchEvent(instance, 'blur', buildBlurEvent());
63}

Callers

nothing calls this directly

Calls 13

isHostTextInputFunction · 0.90
isEditableTextInputFunction · 0.90
isPointerEventEnabledFunction · 0.90
dispatchEventFunction · 0.90
buildFocusEventFunction · 0.90
getTextInputValueFunction · 0.90
buildTextChangeEventFunction · 0.90
getTextContentSizeFunction · 0.90
waitFunction · 0.90
buildEndEditingEventFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…