Skip to content

Commit 150a0de

Browse files
committed
(#310) Migrated keyboard.class to not use adapters
1 parent 320cabd commit 150a0de

File tree

2 files changed

+230
-193
lines changed

2 files changed

+230
-193
lines changed

lib/keyboard.class.spec.ts

+143-106
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,152 @@
1-
import { NativeAdapter } from "./adapter/native.adapter.class";
2-
import { Key } from "./key.enum";
3-
import { KeyboardClass } from "./keyboard.class";
4-
import providerRegistry from "./provider/provider-registry.class";
1+
import {Key} from "./key.enum";
2+
import {KeyboardClass} from "./keyboard.class";
3+
import {ProviderRegistry} from "./provider/provider-registry.class";
4+
import {mockPartial} from "sneer";
5+
import {KeyboardProviderInterface} from "./provider";
56

6-
jest.mock("./adapter/native.adapter.class");
77
jest.setTimeout(10000);
88

99
beforeEach(() => {
10-
jest.resetAllMocks();
10+
jest.clearAllMocks();
1111
});
1212

13-
describe("Keyboard", () => {
14-
it("should have a default delay of 300 ms", () => {
15-
// GIVEN
16-
const adapterMock = new NativeAdapter(providerRegistry);
17-
const SUT = new KeyboardClass(adapterMock);
18-
19-
// WHEN
20-
21-
// THEN
22-
expect(SUT.config.autoDelayMs).toEqual(300);
23-
});
24-
25-
it("should pass input strings down to the type call.", async () => {
26-
// GIVEN
27-
const adapterMock = new NativeAdapter(providerRegistry);
28-
const SUT = new KeyboardClass(adapterMock);
29-
const payload = "Test input!";
30-
31-
// WHEN
32-
await SUT.type(payload);
33-
34-
// THEN
35-
expect(adapterMock.type).toHaveBeenCalledTimes(payload.length);
36-
for (const char of payload.split("")) {
37-
expect(adapterMock.type).toHaveBeenCalledWith(char);
38-
}
39-
});
40-
41-
it("should pass multiple input strings down to the type call.", async () => {
42-
// GIVEN
43-
const adapterMock = new NativeAdapter(providerRegistry);
44-
const SUT = new KeyboardClass(adapterMock);
45-
const payload = ["Test input!", "Array test2"];
46-
47-
// WHEN
48-
await SUT.type(...payload);
49-
50-
// THEN
51-
expect(adapterMock.type).toHaveBeenCalledTimes(payload.join(" ").length);
52-
for (const char of payload.join(" ").split("")) {
53-
expect(adapterMock.type).toHaveBeenCalledWith(char);
54-
}
55-
});
56-
57-
it("should pass input keys down to the click call.", async () => {
58-
// GIVEN
59-
const adapterMock = new NativeAdapter(providerRegistry);
60-
const SUT = new KeyboardClass(adapterMock);
61-
const payload = [Key.A, Key.S, Key.D, Key.F];
62-
63-
// WHEN
64-
await SUT.type(...payload);
65-
66-
// THEN
67-
expect(adapterMock.click).toHaveBeenCalledTimes(1);
68-
expect(adapterMock.click).toHaveBeenCalledWith(...payload);
69-
});
70-
71-
it("should pass a list of input keys down to the click call.", async () => {
72-
// GIVEN
73-
const adapterMock = new NativeAdapter(providerRegistry);
74-
const SUT = new KeyboardClass(adapterMock);
75-
const payload = [Key.A, Key.S, Key.D, Key.F];
76-
77-
// WHEN
78-
for (const key of payload) {
79-
await SUT.type(key);
13+
const providerRegistryMock = mockPartial<ProviderRegistry>({
14+
getKeyboard(): KeyboardProviderInterface {
15+
return mockPartial<KeyboardProviderInterface>({
16+
setKeyboardDelay: jest.fn(),
17+
})
8018
}
19+
})
8120

82-
// THEN
83-
expect(adapterMock.click).toHaveBeenCalledTimes(payload.length);
84-
});
85-
86-
it("should pass a list of input keys down to the pressKey call.", async () => {
87-
// GIVEN
88-
const adapterMock = new NativeAdapter(providerRegistry);
89-
const SUT = new KeyboardClass(adapterMock);
90-
const payload = [Key.A, Key.S, Key.D, Key.F];
91-
92-
// WHEN
93-
for (const key of payload) {
94-
await SUT.pressKey(key);
95-
}
96-
97-
// THEN
98-
expect(adapterMock.pressKey).toHaveBeenCalledTimes(payload.length);
99-
});
100-
101-
it("should pass a list of input keys down to the releaseKey call.", async () => {
102-
// GIVEN
103-
const adapterMock = new NativeAdapter(providerRegistry);
104-
const SUT = new KeyboardClass(adapterMock);
105-
const payload = [Key.A, Key.S, Key.D, Key.F];
106-
107-
// WHEN
108-
for (const key of payload) {
109-
await SUT.releaseKey(key);
110-
}
111-
112-
// THEN
113-
expect(adapterMock.releaseKey).toHaveBeenCalledTimes(payload.length);
114-
});
21+
describe("Keyboard", () => {
22+
it("should have a default delay of 300 ms", () => {
23+
// GIVEN
24+
const SUT = new KeyboardClass(providerRegistryMock);
25+
26+
// WHEN
27+
28+
// THEN
29+
expect(SUT.config.autoDelayMs).toEqual(300);
30+
});
31+
32+
it("should pass input strings down to the type call.", async () => {
33+
// GIVEN
34+
const SUT = new KeyboardClass(providerRegistryMock);
35+
const payload = "Test input!";
36+
37+
const typeMock = jest.fn();
38+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
39+
setKeyboardDelay: jest.fn(),
40+
type: typeMock
41+
}));
42+
43+
// WHEN
44+
await SUT.type(payload);
45+
46+
// THEN
47+
expect(typeMock).toHaveBeenCalledTimes(payload.length);
48+
for (const char of payload.split("")) {
49+
expect(typeMock).toHaveBeenCalledWith(char);
50+
}
51+
});
52+
53+
it("should pass multiple input strings down to the type call.", async () => {
54+
// GIVEN
55+
const SUT = new KeyboardClass(providerRegistryMock);
56+
const payload = ["Test input!", "Array test2"];
57+
58+
const typeMock = jest.fn();
59+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
60+
setKeyboardDelay: jest.fn(),
61+
type: typeMock
62+
}));
63+
64+
// WHEN
65+
await SUT.type(...payload);
66+
67+
// THEN
68+
expect(typeMock).toHaveBeenCalledTimes(payload.join(" ").length);
69+
for (const char of payload.join(" ").split("")) {
70+
expect(typeMock).toHaveBeenCalledWith(char);
71+
}
72+
});
73+
74+
it("should pass input keys down to the click call.", async () => {
75+
// GIVEN
76+
const SUT = new KeyboardClass(providerRegistryMock);
77+
const payload = [Key.A, Key.S, Key.D, Key.F];
78+
79+
const clickMock = jest.fn();
80+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
81+
setKeyboardDelay: jest.fn(),
82+
click: clickMock
83+
}));
84+
85+
// WHEN
86+
await SUT.type(...payload);
87+
88+
// THEN
89+
expect(clickMock).toHaveBeenCalledTimes(1);
90+
expect(clickMock).toHaveBeenCalledWith(...payload);
91+
});
92+
93+
it("should pass a list of input keys down to the click call.", async () => {
94+
// GIVEN
95+
const SUT = new KeyboardClass(providerRegistryMock);
96+
const payload = [Key.A, Key.S, Key.D, Key.F];
97+
98+
const clickMock = jest.fn();
99+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
100+
setKeyboardDelay: jest.fn(),
101+
click: clickMock
102+
}));
103+
104+
// WHEN
105+
for (const key of payload) {
106+
await SUT.type(key);
107+
}
108+
109+
// THEN
110+
expect(clickMock).toHaveBeenCalledTimes(payload.length);
111+
});
112+
113+
it("should pass a list of input keys down to the pressKey call.", async () => {
114+
// GIVEN
115+
const SUT = new KeyboardClass(providerRegistryMock);
116+
const payload = [Key.A, Key.S, Key.D, Key.F];
117+
118+
const keyMock = jest.fn();
119+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
120+
setKeyboardDelay: jest.fn(),
121+
pressKey: keyMock
122+
}));
123+
124+
// WHEN
125+
for (const key of payload) {
126+
await SUT.pressKey(key);
127+
}
128+
129+
// THEN
130+
expect(keyMock).toHaveBeenCalledTimes(payload.length);
131+
});
132+
133+
it("should pass a list of input keys down to the releaseKey call.", async () => {
134+
// GIVEN
135+
const SUT = new KeyboardClass(providerRegistryMock);
136+
const payload = [Key.A, Key.S, Key.D, Key.F];
137+
138+
const keyMock = jest.fn();
139+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
140+
setKeyboardDelay: jest.fn(),
141+
releaseKey: keyMock
142+
}));
143+
144+
// WHEN
145+
for (const key of payload) {
146+
await SUT.releaseKey(key);
147+
}
148+
149+
// THEN
150+
expect(keyMock).toHaveBeenCalledTimes(payload.length);
151+
});
115152
});

0 commit comments

Comments
 (0)