-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathkey-bindings.spec.ts
More file actions
266 lines (224 loc) · 7.57 KB
/
key-bindings.spec.ts
File metadata and controls
266 lines (224 loc) · 7.57 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import {
defineCE,
expect,
fixture,
html,
unsafeStatic,
} from '@open-wc/testing';
import { LitElement } from 'lit';
import { simulateKeyboard } from '../utils.spec.js';
import {
addKeybindings,
altKey,
arrowUp,
enterKey,
shiftKey,
spaceBar,
} from './key-bindings.js';
describe('Key bindings controller', () => {
describe('addKeybindings', () => {
let tag: string;
let instance: LitElement & {
key: string;
event: KeyboardEvent;
input: HTMLInputElement;
};
before(() => {
tag = defineCE(
class extends LitElement {
public key?: string;
public event?: KeyboardEvent;
public get input() {
return this.renderRoot.querySelector('input')!;
}
constructor() {
super();
addKeybindings(this, {
skip: () => !!this.hidden,
})
.setActivateHandler(this.handleKeyboardEvent)
.set('a', this.handleKeyboardEvent, { triggers: ['keydown'] })
.set('s', this.handleKeyboardEvent, { triggers: ['keyup'] })
.set('k', this.handleKeyboardEvent)
.set('d', this.handleKeyboardEvent, {
triggers: ['keydown', 'keyup'],
preventDefault: true,
stopPropagation: true,
})
.set([shiftKey, 'c'], this.handleKeyboardEvent)
.set([shiftKey, altKey, arrowUp], this.handleKeyboardEvent);
addKeybindings(this).set('x', this.handleKeyboardEvent);
}
private handleKeyboardEvent(event: KeyboardEvent) {
this.key = event.key;
this.event = event;
}
protected override render() {
return html`<input />`;
}
}
);
});
beforeEach(async () => {
const tagName = unsafeStatic(tag);
instance = await fixture(html`<${tagName}></${tagName}>`);
});
describe('Triggers', () => {
it('keydown - keydown event works', async () => {
dispatch(instance, 'keydown', 'a');
expect(instance.key).to.equal('a');
expect(instance.event.type).to.equal('keydown');
});
it('keydown - keyup event is ignored', async () => {
dispatch(instance, 'keyup', 'a');
expect(instance.key).to.be.undefined;
expect(instance.event).to.be.undefined;
});
it('keyup - keyup event works', async () => {
dispatch(instance, 'keyup', 's');
expect(instance.key).to.equal('s');
expect(instance.event.type).to.equal('keyup');
});
it('keyup - keydown event is ignored', async () => {
dispatch(instance, 'keydown', 's');
expect(instance.key).to.be.undefined;
expect(instance.event).to.be.undefined;
});
});
it('should honor default skip condition', () => {
simulateKeyboard(instance.input, 'x');
expect(instance.key).to.be.undefined;
expect(instance.event).to.be.undefined;
});
it('should honor skip condition', () => {
instance.hidden = true;
simulateKeyboard(instance, 'k');
expect(instance.key).to.be.undefined;
expect(instance.event).to.be.undefined;
});
it('event handler modifiers - prevent default', () => {
dispatch(instance, 'keydown', 'd');
expect(instance.event.type).to.equal('keydown');
expect(instance.event.defaultPrevented).to.be.true;
dispatch(instance, 'keyup', 'd');
expect(instance.event.type).to.equal('keyup');
expect(instance.event.defaultPrevented).to.be.true;
});
it('event handler modifiers - stop propagation', () => {
let parentHandlerCalled = false;
const handler = () => {
parentHandlerCalled = true;
};
const container = instance.parentElement!;
container.addEventListener('keydown', handler, { once: true });
container.addEventListener('keyup', handler, { once: true });
simulateKeyboard(instance, 'd');
expect(parentHandlerCalled).to.be.false;
expect(instance.key).to.equal('d');
});
it('activation keys', () => {
for (const key of [enterKey, spaceBar]) {
simulateKeyboard(instance, key);
expect(instance.key).to.equal(key.toLowerCase());
}
});
it('combinations', () => {
simulateKeyboard(instance, shiftKey);
expect(instance.key).to.be.undefined;
simulateKeyboard(instance, 'c');
expect(instance.key).to.be.undefined;
simulateKeyboard(instance, [shiftKey, 'c']);
expect(instance.key).to.equal('c');
simulateKeyboard(instance, [shiftKey, altKey, arrowUp]);
expect(instance.key).to.equal(arrowUp.toLowerCase());
});
});
describe('multi non-modifier key combinations', () => {
let multiTag: string;
let multiInstance: LitElement & { callCount: number };
before(() => {
multiTag = defineCE(
class extends LitElement {
public callCount = 0;
constructor() {
super();
addKeybindings(this).set(['x', 'z'], () => this.callCount++);
}
}
);
});
beforeEach(async () => {
const tagName = unsafeStatic(multiTag);
multiInstance = await fixture(html`<${tagName}></${tagName}>`);
});
it('should not fire when only one of the keys is pressed', () => {
dispatch(multiInstance, 'keydown', 'x');
expect(multiInstance.callCount).to.equal(0);
dispatch(multiInstance, 'keyup', 'x');
dispatch(multiInstance, 'keydown', 'z');
expect(multiInstance.callCount).to.equal(0);
});
it('should fire when all keys are held simultaneously', () => {
dispatch(multiInstance, 'keydown', 'x');
dispatch(multiInstance, 'keydown', 'z');
expect(multiInstance.callCount).to.equal(1);
});
it('should clear pressed keys on window blur', () => {
// Hold 'x', then switch away without releasing it
dispatch(multiInstance, 'keydown', 'x');
window.dispatchEvent(new FocusEvent('blur'));
// 'x' should no longer be tracked; pressing 'z' alone should not fire
dispatch(multiInstance, 'keydown', 'z');
expect(multiInstance.callCount).to.equal(0);
});
});
describe('repeat option', () => {
let repeatTag: string;
let repeatInstance: LitElement & { callCount: number };
before(() => {
repeatTag = defineCE(
class extends LitElement {
public callCount = 0;
constructor() {
super();
addKeybindings(this)
.set('a', () => this.callCount++)
.set('b', () => this.callCount++, { repeat: true });
}
}
);
});
beforeEach(async () => {
const tagName = unsafeStatic(repeatTag);
repeatInstance = await fixture(html`<${tagName}></${tagName}>`);
});
it('should not fire on repeated keydown by default', () => {
dispatch(repeatInstance, 'keydown', 'a', true);
expect(repeatInstance.callCount).to.equal(0);
});
it('should fire on initial keydown regardless of repeat option', () => {
dispatch(repeatInstance, 'keydown', 'b');
expect(repeatInstance.callCount).to.equal(1);
});
it('should fire on repeated keydown when repeat is enabled', () => {
dispatch(repeatInstance, 'keydown', 'b', true);
expect(repeatInstance.callCount).to.equal(1);
});
});
});
function dispatch(
node: Element,
type: 'keydown' | 'keyup',
key: string,
repeat = false
) {
node.dispatchEvent(
new KeyboardEvent(type, {
key,
bubbles: true,
composed: true,
cancelable: true,
repeat,
})
);
}