-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathpaste.ts
More file actions
80 lines (70 loc) · 2.63 KB
/
paste.ts
File metadata and controls
80 lines (70 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { ReactTestInstance } from 'react-test-renderer';
import { ErrorWithStack } from '../helpers/errors';
import { isHostTextInput } from '../helpers/host-component-names';
import { isPointerEventEnabled } from '../helpers/pointer-events';
import { getTextInputValue, isEditableTextInput } from '../helpers/text-input';
import { nativeState } from '../native-state';
import { EventBuilder } from './event-builder';
import type { UserEventInstance } from './setup';
import { dispatchEvent, getTextContentSize, wait } from './utils';
import { formatElement } from '../helpers/format-element';
import { logger } from '../helpers/logger';
export async function paste(
this: UserEventInstance,
element: ReactTestInstance,
text: string,
): Promise<void> {
if (!isHostTextInput(element)) {
throw new ErrorWithStack(
`paste() only supports host "TextInput" elements. Passed element has type: "${element.type}".`,
paste,
);
}
if (!isEditableTextInput(element)) {
logger.warn(
`User Event (paste): element ${formatElement(element, { compact: true })} is not editable.`,
);
return;
}
if (!isPointerEventEnabled(element)) {
logger.warn(
`User Event (paste): element ${formatElement(element, { compact: true })} has pointer event handlers disabled.`,
);
return;
}
// 1. Enter element
await dispatchEvent(element, 'focus', EventBuilder.Common.focus());
// 2. Select all
const textToClear = getTextInputValue(element);
const rangeToClear = { start: 0, end: textToClear.length };
await dispatchEvent(
element,
'selectionChange',
EventBuilder.TextInput.selectionChange(rangeToClear),
);
// 3. Paste the text
nativeState.valueForElement.set(element, text);
await dispatchEvent(element, 'change', EventBuilder.TextInput.change(text));
await dispatchEvent(element, 'changeText', text);
const rangeAfter = { start: text.length, end: text.length };
await dispatchEvent(
element,
'selectionChange',
EventBuilder.TextInput.selectionChange(rangeAfter),
);
// According to the docs only multiline TextInput emits contentSizeChange event
// @see: https://reactnative.dev/docs/textinput#oncontentsizechange
const isMultiline = element.props.multiline === true;
if (isMultiline) {
const contentSize = getTextContentSize(text);
await dispatchEvent(
element,
'contentSizeChange',
EventBuilder.TextInput.contentSizeChange(contentSize),
);
}
// 4. Exit element
await wait(this.config);
await dispatchEvent(element, 'endEditing', EventBuilder.TextInput.endEditing(text));
await dispatchEvent(element, 'blur', EventBuilder.Common.blur());
}