Skip to content

Commit 748ee9a

Browse files
committed
WIP
1 parent e45dda6 commit 748ee9a

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/cdk/testing/type-in-element.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
import {dispatchFakeEvent, dispatchKeyboardEvent} from './dispatch-events';
1010
import {triggerFocus} from './element-focus';
1111

12+
/** Modifier keys that may be held while typing. */
13+
export interface KeyModifiers {
14+
control?: boolean;
15+
alt?: boolean;
16+
shift?: boolean;
17+
meta?: boolean;
18+
}
19+
1220
/**
1321
* Represents a special key that does not result in a character being inputed in a text field.
1422
* @docs-private
@@ -30,15 +38,40 @@ export function isTextInput(element: Element): element is HTMLInputElement | HTM
3038
/**
3139
* Focuses an input, sets its value and dispatches
3240
* the `input` event, simulating the user typing.
33-
* @param value Value to be set on the input.
3441
* @param element Element onto which to set the value.
42+
* @param keys The keys to send to the element.
43+
* @docs-private
44+
*/
45+
export function typeInElement(element: HTMLElement, ...keys: (string | SpecialKey)[]): void;
46+
47+
/**
48+
* Focuses an input, sets its value and dispatches
49+
* the `input` event, simulating the user typing.
50+
* @param element Element onto which to set the value.
51+
* @param modifiers Modifier keys that are held while typing.
52+
* @param keys The keys to send to the element.
3553
* @docs-private
3654
*/
37-
export function typeInElement(element: HTMLElement, ...value: (string | SpecialKey)[]) {
55+
export function typeInElement(
56+
element: HTMLElement, modifiers: KeyModifiers, ...keys: (string | SpecialKey)[]): void;
57+
58+
export function typeInElement(element: HTMLElement, ...modifiersAndKeys: any) {
59+
const first = modifiersAndKeys[0];
60+
let modifiers: KeyModifiers;
61+
let keys: (string | SpecialKey)[];
62+
if (typeof first !== 'string' && first.keyCode === undefined) {
63+
modifiers = first;
64+
keys = modifiersAndKeys.slice(1);
65+
} else {
66+
modifiers = {};
67+
keys = modifiersAndKeys;
68+
}
69+
70+
// TODO: pass through modifiers
3871
triggerFocus(element);
39-
for (const keys of value) {
40-
if (typeof keys === 'string') {
41-
for (const key of keys) {
72+
for (const keyOrStr of keys) {
73+
if (typeof keyOrStr === 'string') {
74+
for (const key of keyOrStr) {
4275
const keyCode = key.charCodeAt(0);
4376
dispatchKeyboardEvent(element, 'keydown', keyCode);
4477
dispatchKeyboardEvent(element, 'keypress', keyCode);
@@ -49,9 +82,9 @@ export function typeInElement(element: HTMLElement, ...value: (string | SpecialK
4982
dispatchKeyboardEvent(element, 'keyup', keyCode);
5083
}
5184
} else {
52-
dispatchKeyboardEvent(element, 'keydown', keys.keyCode, keys.key);
53-
dispatchKeyboardEvent(element, 'keypress', keys.keyCode, keys.key);
54-
dispatchKeyboardEvent(element, 'keyup', keys.keyCode, keys.key);
85+
dispatchKeyboardEvent(element, 'keydown', keyOrStr.keyCode, keyOrStr.key);
86+
dispatchKeyboardEvent(element, 'keypress', keyOrStr.keyCode, keyOrStr.key);
87+
dispatchKeyboardEvent(element, 'keyup', keyOrStr.keyCode, keyOrStr.key);
5588
}
5689
}
5790
}

0 commit comments

Comments
 (0)