-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathtype.ts
More file actions
152 lines (128 loc) · 4.43 KB
/
type.ts
File metadata and controls
152 lines (128 loc) · 4.43 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 { UserEventConfig, UserEventInstance } from '../setup';
import { dispatchEvent, getTextContentSize, wait } from '../utils';
import { parseKeys } from './parse-keys';
import { logger } from '../../helpers/logger';
import { formatElement } from '../../helpers/format-element';
export interface TypeOptions {
skipPress?: boolean;
submitEditing?: boolean;
skipBlur?: 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,
);
}
if (!isEditableTextInput(element)) {
logger.warn(
`User Event (type): element ${formatElement(element, { compact: true })} is not editable.`,
);
return;
}
if (!isPointerEventEnabled(element)) {
logger.warn(
`User Event (type): element ${formatElement(element, { compact: true })} has pointer event handlers disabled.`,
);
return;
}
const keys = parseKeys(text);
if (!options?.skipPress) {
await dispatchEvent(element, 'pressIn', EventBuilder.Common.touch());
}
await dispatchEvent(element, 'focus', EventBuilder.Common.focus());
if (!options?.skipPress) {
await wait(this.config);
await 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) {
await dispatchEvent(element, 'submitEditing', EventBuilder.TextInput.submitEditing(finalText));
}
if (!options?.skipBlur) {
await dispatchEvent(element, 'endEditing', EventBuilder.TextInput.endEditing(finalText));
await 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);
await 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);
await dispatchEvent(element, 'change', EventBuilder.TextInput.change(text));
await dispatchEvent(element, 'changeText', text);
const selectionRange = {
start: text.length,
end: text.length,
};
await 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);
await 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;
}