-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathtype.ts
135 lines (111 loc) · 3.94 KB
/
type.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { ReactTestInstance } from 'react-test-renderer';
import { isHostTextInput } from '../../helpers/host-component-names';
import { nativeState } from '../../native-state';
import { EventBuilder } from '../event-builder';
import { ErrorWithStack } from '../../helpers/errors';
import { getTextInputValue, isTextInputEditable } from '../../helpers/text-input';
import { isPointerEventEnabled } from '../../helpers/pointer-events';
import { UserEventConfig, UserEventInstance } from '../setup';
import { dispatchEvent, wait, getTextContentSize } from '../utils';
import { parseKeys } from './parse-keys';
export interface TypeOptions {
skipPress?: boolean;
submitEditing?: boolean;
}
export async function type(
this: UserEventInstance,
element: ReactTestInstance,
text: string,
options?: TypeOptions,
): Promise<void> {
if (!isHostTextInput(element)) {
throw new ErrorWithStack(
`type() works only with host "TextInput" elements. Passed element has type "${element.type}".`,
type,
);
}
// Skip events if the element is disabled
if (!isTextInputEditable(element) || !isPointerEventEnabled(element)) {
return;
}
const keys = parseKeys(text);
if (!options?.skipPress) {
dispatchEvent(element, 'pressIn', EventBuilder.Common.touch());
}
dispatchEvent(element, 'focus', EventBuilder.Common.focus());
if (!options?.skipPress) {
await wait(this.config);
dispatchEvent(element, 'pressOut', EventBuilder.Common.touch());
}
let currentText = getTextInputValue(element);
for (const key of keys) {
const previousText = getTextInputValue(element);
const proposedText = applyKey(previousText, key);
const isAccepted = isTextChangeAccepted(element, proposedText);
currentText = isAccepted ? proposedText : previousText;
await emitTypingEvents(element, {
config: this.config,
key,
text: currentText,
isAccepted,
});
}
const finalText = getTextInputValue(element);
await wait(this.config);
if (options?.submitEditing) {
dispatchEvent(element, 'submitEditing', EventBuilder.TextInput.submitEditing(finalText));
}
dispatchEvent(element, 'endEditing', EventBuilder.TextInput.endEditing(finalText));
dispatchEvent(element, 'blur', EventBuilder.Common.blur());
}
type EmitTypingEventsContext = {
config: UserEventConfig;
key: string;
text: string;
isAccepted?: boolean;
};
export async function emitTypingEvents(
element: ReactTestInstance,
{ config, key, text, isAccepted }: EmitTypingEventsContext,
) {
const isMultiline = element.props.multiline === true;
await wait(config);
dispatchEvent(element, 'keyPress', EventBuilder.TextInput.keyPress(key));
// Platform difference (based on experiments):
// - iOS and RN Web: TextInput emits only `keyPress` event when max length has been reached
// - Android: TextInputs does not emit any events
if (isAccepted === false) {
return;
}
nativeState.valueForElement.set(element, text);
dispatchEvent(element, 'change', EventBuilder.TextInput.change(text));
dispatchEvent(element, 'changeText', text);
const selectionRange = {
start: text.length,
end: text.length,
};
dispatchEvent(element, 'selectionChange', EventBuilder.TextInput.selectionChange(selectionRange));
// According to the docs only multiline TextInput emits contentSizeChange event
// @see: https://reactnative.dev/docs/textinput#oncontentsizechange
if (isMultiline) {
const contentSize = getTextContentSize(text);
dispatchEvent(
element,
'contentSizeChange',
EventBuilder.TextInput.contentSizeChange(contentSize),
);
}
}
function applyKey(text: string, key: string) {
if (key === 'Enter') {
return `${text}\n`;
}
if (key === 'Backspace') {
return text.slice(0, -1);
}
return text + key;
}
function isTextChangeAccepted(element: ReactTestInstance, text: string) {
const maxLength = element.props.maxLength;
return maxLength === undefined || text.length <= maxLength;
}