( this: UserEventInstance, instance: TestInstance, text: string, )
| 17 | import { dispatchEvent, getTextContentSize, wait } from './utils'; |
| 18 | |
| 19 | export 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…