Skip to content

Commit 66e30e0

Browse files
committed
test: update io_keyboard tests & test for keyboard events
1 parent 6d8c3a6 commit 66e30e0

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

test/unit/io_keyboard.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ const test = require('tap').test;
22
const Keyboard = require('../../src/io/keyboard');
33
const Runtime = require('../../src/engine/runtime');
44

5+
function listenForKeyboardEvents(rt) {
6+
const eventsEmitted = {
7+
KEY_PRESSED: [],
8+
KEY_ANY_PRESSED: [],
9+
};
10+
11+
rt.on('KEY_PRESSED', event => {
12+
eventsEmitted.KEY_PRESSED.push(event);
13+
});
14+
15+
// Note that KEY_ANY_PRESSED doesn't emit any event details and so
16+
// the event value should be `undefined`.
17+
rt.on('KEY_ANY_PRESSED', event => {
18+
eventsEmitted.KEY_ANY_PRESSED.push(event);
19+
});
20+
21+
return eventsEmitted;
22+
}
23+
524
test('spec', t => {
625
const rt = new Runtime();
726
const k = new Keyboard(rt);
@@ -15,89 +34,154 @@ test('spec', t => {
1534
test('space key', t => {
1635
const rt = new Runtime();
1736
const k = new Keyboard(rt);
37+
const eventsEmitted = listenForKeyboardEvents(rt);
1838

1939
k.postData({
2040
key: ' ',
2141
isDown: true
2242
});
43+
2344
t.strictDeepEquals(k._keysPressed, ['space']);
45+
2446
t.strictEquals(k.getKeyIsDown('space'), true);
2547
t.strictEquals(k.getKeyIsDown('any'), true);
48+
49+
t.same(eventsEmitted.KEY_PRESSED, ['space']);
50+
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
51+
2652
t.end();
2753
});
2854

2955
test('letter key', t => {
3056
const rt = new Runtime();
3157
const k = new Keyboard(rt);
58+
const eventsEmitted = listenForKeyboardEvents(rt);
3259

3360
k.postData({
3461
key: 'a',
3562
isDown: true
3663
});
64+
3765
t.strictDeepEquals(k._keysPressed, ['A']);
66+
3867
t.strictEquals(k.getKeyIsDown(65), true);
3968
t.strictEquals(k.getKeyIsDown('a'), true);
4069
t.strictEquals(k.getKeyIsDown('A'), true);
4170
t.strictEquals(k.getKeyIsDown('any'), true);
71+
72+
t.same(eventsEmitted.KEY_PRESSED, ['A']);
73+
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
74+
4275
t.end();
4376
});
4477

4578
test('number key', t => {
4679
const rt = new Runtime();
4780
const k = new Keyboard(rt);
81+
const eventsEmitted = listenForKeyboardEvents(rt);
4882

4983
k.postData({
5084
key: '1',
5185
isDown: true
5286
});
87+
5388
t.strictDeepEquals(k._keysPressed, ['1']);
89+
5490
t.strictEquals(k.getKeyIsDown(49), true);
5591
t.strictEquals(k.getKeyIsDown('1'), true);
5692
t.strictEquals(k.getKeyIsDown('any'), true);
93+
94+
t.same(eventsEmitted.KEY_PRESSED, ['1']);
95+
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
96+
5797
t.end();
5898
});
5999

60100
test('non-english key', t => {
61101
const rt = new Runtime();
62102
const k = new Keyboard(rt);
103+
const eventsEmitted = listenForKeyboardEvents(rt);
63104

64105
k.postData({
65106
key: '日',
66107
isDown: true
67108
});
109+
68110
t.strictDeepEquals(k._keysPressed, ['日']);
111+
69112
t.strictEquals(k.getKeyIsDown('日'), true);
70113
t.strictEquals(k.getKeyIsDown('any'), true);
114+
115+
t.same(eventsEmitted.KEY_PRESSED, ['日']);
116+
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
117+
71118
t.end();
72119
});
73120

74-
test('ignore modifier key', t => {
121+
test('shift key', t => {
75122
const rt = new Runtime();
76123
const k = new Keyboard(rt);
124+
const eventsEmitted = listenForKeyboardEvents(rt);
77125

78126
k.postData({
79127
key: 'Shift',
80128
isDown: true
81129
});
130+
131+
t.strictDeepEquals(k._keysPressed, ['shift']);
132+
133+
t.strictEquals(k.getKeyIsDown('shift'), true);
134+
t.strictEquals(k.getKeyIsDown('any'), false);
135+
136+
t.same(eventsEmitted.KEY_PRESSED, ['shift']);
137+
t.same(eventsEmitted.KEY_ANY_PRESSED, []);
138+
139+
t.end();
140+
});
141+
142+
test('ignore control key', t => {
143+
const rt = new Runtime();
144+
const k = new Keyboard(rt);
145+
const eventsEmitted = listenForKeyboardEvents(rt);
146+
147+
k.postData({
148+
key: 'Control',
149+
isDown: true
150+
});
151+
82152
t.strictDeepEquals(k._keysPressed, []);
153+
154+
t.strictEquals(k.getKeyIsDown('control'), false);
83155
t.strictEquals(k.getKeyIsDown('any'), false);
156+
157+
t.same(eventsEmitted.KEY_PRESSED, []);
158+
t.same(eventsEmitted.KEY_ANY_PRESSED, []);
159+
84160
t.end();
85161
});
86162

87163
test('keyup', t => {
88164
const rt = new Runtime();
89165
const k = new Keyboard(rt);
166+
const eventsEmitted = listenForKeyboardEvents(rt);
90167

91168
k.postData({
92169
key: 'ArrowLeft',
93170
isDown: true
94171
});
172+
95173
k.postData({
96174
key: 'ArrowLeft',
97175
isDown: false
98176
});
177+
99178
t.strictDeepEquals(k._keysPressed, []);
179+
100180
t.strictEquals(k.getKeyIsDown('left arrow'), false);
101181
t.strictEquals(k.getKeyIsDown('any'), false);
182+
183+
t.same(eventsEmitted.KEY_PRESSED, ['left arrow']);
184+
t.same(eventsEmitted.KEY_ANY_PRESSED, [undefined]);
185+
102186
t.end();
103187
});

0 commit comments

Comments
 (0)