-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtests.utilities.ts
76 lines (64 loc) · 1.88 KB
/
tests.utilities.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
import { flushMicrotasks } from '@angular/core/testing';
import { Iterable } from 'immutable';
// redux-logger is a dev dependency in the workspace
// tslint:disable-next-line:no-implicit-dependencies
import { createLogger } from 'redux-logger';
export const logger = createLogger({
level: 'debug',
collapsed: true,
predicate: (getState, action) => true,
stateTransformer: state => {
const newState = new Object();
for (const i of Object.keys(state)) {
newState[i] = Iterable.isIterable(state[i]) ? state[i].toJS() : state[i];
}
return newState;
},
});
export const simulateUserTyping = (control, text: string): Promise<void> => {
return new Promise<void>((resolve, reject) => {
try {
dispatchKeyEvents(control, text);
resolve();
} catch (error) {
console.error('Failed to dispatch typing events', error);
reject(error);
} finally {
flushMicrotasks();
}
});
};
export const dispatchKeyEvents = (control, text: string) => {
if (!text) {
return;
}
control.focus();
for (const character of text) {
const c = character.charCodeAt(0);
const keyboardEventFactory = (eventType: string, value) => {
return new KeyboardEvent(eventType, {
altKey: false,
cancelable: false,
bubbles: true,
ctrlKey: false,
metaKey: false,
detail: value,
view: window,
shiftKey: false,
repeat: false,
key: value,
});
};
const eventFactory = (eventType: string) => {
return new Event(eventType, {
bubbles: true,
cancelable: false,
});
};
control.dispatchEvent(keyboardEventFactory('keydown', c));
control.dispatchEvent(keyboardEventFactory('keypress', c));
control.dispatchEvent(keyboardEventFactory('keyup', c));
control.value += character;
control.dispatchEvent(eventFactory('input'));
}
};