Skip to content

Commit 7790e8c

Browse files
committed
(#47) Additional tests
1 parent 30c0b99 commit 7790e8c

5 files changed

+614
-304
lines changed

lib/provider/native/robotjs-keyboard-action.class.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,18 @@ export class KeyboardAction implements KeyboardActionProvider {
148148
}
149149

150150
public type(input: string): Promise<void> {
151-
return new Promise<void>(((resolve, reject) => {
151+
return new Promise<void>((resolve, reject) => {
152152
try {
153153
robot.typeString(input);
154154
resolve();
155155
} catch (e) {
156156
reject(e);
157157
}
158-
}));
158+
});
159159
}
160160

161161
public click(...keys: Key[]): Promise<void> {
162-
return new Promise<void>(((resolve, reject) => {
162+
return new Promise<void>((resolve, reject) => {
163163
try {
164164
keys.reverse();
165165
const [key, ...modifiers] = keys;
@@ -172,33 +172,33 @@ export class KeyboardAction implements KeyboardActionProvider {
172172
} catch (e) {
173173
reject(e);
174174
}
175-
}));
175+
});
176176
}
177177

178178
public pressKey(...keys: Key[]): Promise<void> {
179-
return new Promise<void>(((resolve, reject) => {
179+
return new Promise<void>(async (resolve, reject) => {
180180
try {
181181
keys.reverse();
182182
const [key, ...modifiers] = keys;
183-
KeyboardAction.key(key, "down", ...modifiers);
183+
await KeyboardAction.key(key, "down", ...modifiers);
184184
resolve();
185185
} catch (e) {
186186
reject(e);
187187
}
188-
}));
188+
});
189189
}
190190

191191
public releaseKey(...keys: Key[]): Promise<void> {
192-
return new Promise<void>(((resolve, reject) => {
192+
return new Promise<void>(async (resolve, reject) => {
193193
try {
194194
keys.reverse();
195195
const [key, ...modifiers] = keys;
196-
KeyboardAction.key(key, "up", ...modifiers);
196+
await KeyboardAction.key(key, "up", ...modifiers);
197197
resolve();
198198
} catch (e) {
199199
reject(e);
200200
}
201-
}));
201+
});
202202
}
203203

204204
public setKeyboardDelay(delay: number): void {

lib/provider/native/robotjs-keyboard.action.class.spec.ts

+136-76
Original file line numberDiff line numberDiff line change
@@ -9,110 +9,170 @@ beforeEach(() => {
99
});
1010

1111
describe("robotjs keyboard action", () => {
12-
it("should forward the keyTap call to robotjs for a known key", () => {
13-
// GIVEN
14-
const SUT = new KeyboardAction();
12+
describe("click", () => {
13+
it("should forward the keyTap call to robotjs for a known key", () => {
14+
// GIVEN
15+
const SUT = new KeyboardAction();
1516

16-
// WHEN
17-
SUT.click(Key.A);
17+
// WHEN
18+
SUT.click(Key.A);
1819

19-
// THEN
20-
expect(robot.keyTap).toBeCalledTimes(1);
21-
});
20+
// THEN
21+
expect(robot.keyTap).toBeCalledTimes(1);
22+
});
2223

23-
it("should not forward the keyTap call to robotjs for an unknown key", () => {
24-
// GIVEN
25-
const SUT = new KeyboardAction();
24+
it("should reject on robotjs errors", async () => {
25+
// GIVEN
26+
const SUT = new KeyboardAction();
27+
robot.keyTap = jest.fn(() => {
28+
throw new Error("Test error");
29+
});
2630

27-
// WHEN
28-
SUT.click(Key.Add);
31+
// WHEN
2932

30-
// THEN
31-
expect(robot.keyTap).not.toBeCalled();
32-
});
33+
// THEN
34+
expect(SUT.click(Key.A)).rejects.toThrowError("Test error");
35+
});
3336

34-
it("should forward the type call to robotjs", () => {
35-
// GIVEN
36-
const SUT = new KeyboardAction();
37-
const payload = "testInput";
37+
it("should not forward the keyTap call to robotjs for an unknown key", () => {
38+
// GIVEN
39+
const SUT = new KeyboardAction();
3840

39-
// WHEN
40-
SUT.type(payload);
41+
// WHEN
42+
SUT.click(Key.Add);
4143

42-
// THEN
43-
expect(robot.typeString).toBeCalledTimes(1);
44-
expect(robot.typeString).toBeCalledWith(payload);
44+
// THEN
45+
expect(robot.keyTap).not.toBeCalled();
46+
});
4547
});
4648

47-
it("should forward the pressKey call to robotjs for a known key", () => {
48-
// GIVEN
49-
const SUT = new KeyboardAction();
49+
describe("type", () => {
50+
it("should forward the type call to robotjs", () => {
51+
// GIVEN
52+
const SUT = new KeyboardAction();
53+
const payload = "testInput";
5054

51-
// WHEN
52-
SUT.pressKey(Key.A);
55+
// WHEN
56+
SUT.type(payload);
5357

54-
// THEN
55-
expect(robot.keyToggle).toBeCalledTimes(1);
56-
expect(robot.keyToggle).toBeCalledWith(KeyboardAction.keyLookup(Key.A), "down", []);
57-
});
58+
// THEN
59+
expect(robot.typeString).toBeCalledTimes(1);
60+
expect(robot.typeString).toBeCalledWith(payload);
61+
});
5862

59-
it("should treat a list of keys as modifiers + the actual key to press", () => {
60-
// GIVEN
61-
const SUT = new KeyboardAction();
63+
it("should reject on robotjs errors", async () => {
64+
// GIVEN
65+
const SUT = new KeyboardAction();
66+
robot.typeString = jest.fn(() => {
67+
throw new Error("Test error");
68+
});
6269

63-
// WHEN
64-
SUT.pressKey(Key.LeftControl, Key.A);
70+
// WHEN
6571

66-
// THEN
67-
expect(robot.keyToggle).toBeCalledTimes(1);
68-
expect(robot.keyToggle)
69-
.toBeCalledWith(KeyboardAction.keyLookup(Key.A), "down", [KeyboardAction.keyLookup(Key.LeftControl)]);
72+
// THEN
73+
expect(SUT.type("foo")).rejects.toThrowError("Test error");
74+
});
7075
});
7176

72-
it("should not forward the pressKey call to robotjs for an unknown key", () => {
73-
// GIVEN
74-
const SUT = new KeyboardAction();
77+
describe("pressKey", () => {
78+
it("should forward the pressKey call to robotjs for a known key", () => {
79+
// GIVEN
80+
const SUT = new KeyboardAction();
7581

76-
// WHEN
77-
SUT.pressKey(Key.Add);
82+
// WHEN
83+
SUT.pressKey(Key.A);
7884

79-
// THEN
80-
expect(robot.keyToggle).not.toBeCalled();
81-
});
85+
// THEN
86+
expect(robot.keyToggle).toBeCalledTimes(1);
87+
expect(robot.keyToggle).toBeCalledWith(KeyboardAction.keyLookup(Key.A), "down", []);
88+
});
8289

83-
it("should forward the releaseKey call to robotjs for a known key", () => {
84-
// GIVEN
85-
const SUT = new KeyboardAction();
90+
it("should treat a list of keys as modifiers + the actual key to press", () => {
91+
// GIVEN
92+
const SUT = new KeyboardAction();
8693

87-
// WHEN
88-
SUT.releaseKey(Key.A);
94+
// WHEN
95+
SUT.pressKey(Key.LeftControl, Key.A);
8996

90-
// THEN
91-
expect(robot.keyToggle).toBeCalledTimes(1);
92-
expect(robot.keyToggle).toBeCalledWith(KeyboardAction.keyLookup(Key.A), "up", []);
93-
});
97+
// THEN
98+
expect(robot.keyToggle).toBeCalledTimes(1);
99+
expect(robot.keyToggle)
100+
.toBeCalledWith(KeyboardAction.keyLookup(Key.A), "down", [KeyboardAction.keyLookup(Key.LeftControl)]);
101+
});
94102

95-
it("should treat a list of keys as modifiers + the actual key to release", () => {
96-
// GIVEN
97-
const SUT = new KeyboardAction();
103+
it("should not forward the pressKey call to robotjs for an unknown key", () => {
104+
// GIVEN
105+
const SUT = new KeyboardAction();
98106

99-
// WHEN
100-
SUT.releaseKey(Key.LeftControl, Key.A);
107+
// WHEN
108+
SUT.pressKey(Key.Add);
101109

102-
// THEN
103-
expect(robot.keyToggle).toBeCalledTimes(1);
104-
expect(robot.keyToggle)
105-
.toBeCalledWith(KeyboardAction.keyLookup(Key.A), "up", [KeyboardAction.keyLookup(Key.LeftControl)]);
110+
// THEN
111+
expect(robot.keyToggle).not.toBeCalled();
112+
});
113+
114+
it("should reject on robotjs errors", async () => {
115+
// GIVEN
116+
const SUT = new KeyboardAction();
117+
robot.keyToggle = jest.fn(() => {
118+
throw new Error("Test error");
119+
});
120+
121+
// WHEN
122+
123+
// THEN
124+
expect(SUT.pressKey(Key.A)).rejects.toThrowError("Test error");
125+
});
106126
});
107127

108-
it("should not forward the releaseKey call to robotjs for an unknown key", () => {
109-
// GIVEN
110-
const SUT = new KeyboardAction();
128+
describe("releaseKey", () => {
129+
it("should forward the releaseKey call to robotjs for a known key", () => {
130+
// GIVEN
131+
const SUT = new KeyboardAction();
132+
133+
// WHEN
134+
SUT.releaseKey(Key.A);
135+
136+
// THEN
137+
expect(robot.keyToggle).toBeCalledTimes(1);
138+
expect(robot.keyToggle).toBeCalledWith(KeyboardAction.keyLookup(Key.A), "up", []);
139+
});
140+
141+
it("should treat a list of keys as modifiers + the actual key to release", () => {
142+
// GIVEN
143+
const SUT = new KeyboardAction();
144+
145+
// WHEN
146+
SUT.releaseKey(Key.LeftControl, Key.A);
147+
148+
// THEN
149+
expect(robot.keyToggle).toBeCalledTimes(1);
150+
expect(robot.keyToggle)
151+
.toBeCalledWith(KeyboardAction.keyLookup(Key.A), "up", [KeyboardAction.keyLookup(Key.LeftControl)]);
152+
});
153+
154+
it("should not forward the releaseKey call to robotjs for an unknown key", () => {
155+
// GIVEN
156+
const SUT = new KeyboardAction();
157+
158+
// WHEN
159+
SUT.releaseKey(Key.Add);
160+
161+
// THEN
162+
expect(robot.keyToggle).not.toBeCalled();
163+
});
164+
165+
it("should reject on robotjs errors", async () => {
166+
// GIVEN
167+
const SUT = new KeyboardAction();
168+
robot.keyToggle = jest.fn(() => {
169+
throw new Error("Test error");
170+
});
111171

112-
// WHEN
113-
SUT.releaseKey(Key.Add);
172+
// WHEN
114173

115-
// THEN
116-
expect(robot.keyToggle).not.toBeCalled();
174+
// THEN
175+
expect(SUT.releaseKey(Key.A)).rejects.toThrowError("Test error");
176+
});
117177
});
118178
});

0 commit comments

Comments
 (0)