-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathclear.ts
More file actions
63 lines (55 loc) · 2 KB
/
clear.ts
File metadata and controls
63 lines (55 loc) · 2 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
import type { ReactTestInstance } from 'react-test-renderer';
import { ErrorWithStack } from '../helpers/errors';
import { formatElement } from '../helpers/format-element';
import { isHostTextInput } from '../helpers/host-component-names';
import { logger } from '../helpers/logger';
import { isPointerEventEnabled } from '../helpers/pointer-events';
import { getTextInputValue, isEditableTextInput } from '../helpers/text-input';
import { EventBuilder } from './event-builder';
import type { UserEventInstance } from './setup';
import { emitTypingEvents } from './type/type';
import { dispatchEvent, wait } from './utils';
export async function clear(this: UserEventInstance, element: ReactTestInstance): Promise<void> {
if (!isHostTextInput(element)) {
throw new ErrorWithStack(
`clear() only supports host "TextInput" elements. Passed element has type: "${element.type}".`,
clear,
);
}
if (!isEditableTextInput(element)) {
logger.warn(
`User Event (clear): element ${formatElement(element, { compact: true })} is not editable.`,
);
return;
}
if (!isPointerEventEnabled(element)) {
logger.warn(
`User Event (clear): 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 selectionRange = {
start: 0,
end: textToClear.length,
};
await dispatchEvent(
element,
'selectionChange',
EventBuilder.TextInput.selectionChange(selectionRange),
);
// 3. Press backspace with selected text
const emptyText = '';
await emitTypingEvents(element, {
config: this.config,
key: 'Backspace',
text: emptyText,
});
// 4. Exit element
await wait(this.config);
await dispatchEvent(element, 'endEditing', EventBuilder.TextInput.endEditing(emptyText));
await dispatchEvent(element, 'blur', EventBuilder.Common.blur());
}