Skip to content

Commit 6fbb4ae

Browse files
committedMay 11, 2018
Merge branch 'tests' into shiftKey
2 parents 95a9b0a + 170d77f commit 6fbb4ae

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed
 

‎docs/manual-build.md

+8
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ For build requirements for your OS look below.
2828
## Windows
2929
- Install: msys2 with autotools, pkg-config, libtool, gcc, clang, glib, C++ Build Tools, cmake
3030
- `npm run build`
31+
32+
# Testing
33+
34+
iohook uses Jest for automated testing. To execute tests, run `npm run test` in your console.
35+
36+
::: warning
37+
It is important you don't press any buttons on your keyboard, don't use your mouse nor the scroll wheel. Tests depend on native events fired by the real keyboard and mouse. Interrupting them will cause tests to fail.
38+
:::

‎test/specs/keyboard.spec.js

+36-14
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,46 @@ const ioHook = require('../../index');
22
const robot = require('robotjs');
33

44
describe('Keyboard events', () => {
5-
it('receives the text "Hello World"', (done) => {
6-
let currentKey = 0;
5+
afterEach(() => {
6+
ioHook.stop();
7+
});
78

8-
const actualCharacters = [];
9-
const expectedCharacters = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'];
9+
it('receives the text "hello world" on keyup event', (done) => {
10+
expect.assertions(44);
1011

11-
ioHook.on('keydown', (event) => {
12-
actualCharacters.push(String.fromCharCode(event.keychar));
12+
const chars = [
13+
{ keycode: 35, value: 'h' },
14+
{ keycode: 18, value: 'e' },
15+
{ keycode: 38, value: 'l' },
16+
{ keycode: 38, value: 'l' },
17+
{ keycode: 24, value: 'o' },
18+
{ keycode: 57, value: ' ' },
19+
{ keycode: 17, value: 'w' },
20+
{ keycode: 24, value: 'o' },
21+
{ keycode: 19, value: 'r' },
22+
{ keycode: 38, value: 'l' },
23+
{ keycode: 32, value: 'd' }
24+
];
25+
let i = 0;
1326

14-
if (currentKey === expectedCharacters.length) {
15-
expect(actualCharacters).toEqual(expectedCharacters);
16-
}
27+
ioHook.on('keydown', (event) => {
28+
expect(event.keycode).toEqual(chars[i].keycode);
29+
expect(event.type).toEqual('keydown');
30+
});
31+
ioHook.on('keyup', (event) => {
32+
expect(event.keycode).toEqual(chars[i].keycode);
33+
expect(event.type).toEqual('keyup');
1734

18-
currentKey += 1;
19-
done();
20-
});
21-
ioHook.start();
35+
if (i === chars.length - 1) {
36+
done();
37+
}
2238

23-
robot.typeString('Hello World');
39+
i += 1;
2440
});
41+
ioHook.start();
42+
43+
for (const char of chars) {
44+
robot.keyTap(char.value);
45+
}
46+
});
2547
});

0 commit comments

Comments
 (0)
Please sign in to comment.