Issues (4)

src/Input.karma.js (4 issues)

Labels
Severity
1
import Input from "Input";
2
3
let mouseEvent = function(eventName, params) {
4
	const target = document;
5
	let event = document.createEvent("Event");
6
	Object.assign(event, params);
7
	event.initEvent(eventName, true, true);
8
	target.dispatchEvent(event); //was document
9
};
10
11
let mockGamepad = () => {
12
	return {
13
		timestamp: 0,
14
		axes: [0, 0, 0, 0],
15
		buttons: [
16
			{ pressed: false, value: 0 },
17
			{ pressed: false, value: 0 },
18
			{ pressed: false, value: 0 },
19
			{ pressed: false, value: 0 }
20
		]
21
	};
22
};
23
24
describe("Input integration tests", () => {
25
	let input;
26
27
	beforeEach(() => {
28
		input = new Input({
29
			axes: {
30
				horizontal: [
31
					{
32
						type: "keyboard",
33
						positive: "right",
34
						negative: "left"
35
					},
36
					{ type: "gamepad", axis: 0 }
37
				],
38
				vertical: [
39
					{
40
						type: "keyboard",
41
						positive: "up",
42
						negative: "down"
43
					},
44
					{ type: "gamepad", axis: 1 }
45
				]
46
			},
47
			keyboardMapping: {
48
				test: 12,
49
				test2: 34,
50
				left: 37,
51
				up: 38,
52
				right: 39,
53
				down: 40,
54
				space: 32
55
			},
56
			buttons: {
57
				jump: [
58
					{ type: "gamepad", button: 0 },
59
					{ type: "keyboard", key: 32 }
60
				],
61
				special: [
62
					{ type: "gamepad", button: 1 },
63
					{ type: "mouse", button: 2 }
64
				],
65
				fire: [
66
					{ type: "gamepad", button: 2 },
67
					{ type: "keyboard", key: "ctrl" },
68
					{ type: "mouse", button: 0 }
69
				],
70
				mapped: [{ type: "keyboard", key: "test" }],
71
				raw: [{ type: "keyboard", key: 2 }]
72
			}
73
		});
74
	});
75
	it("should exist", () => {
76
		expect(Input).toBeDefined();
77
	});
78
	it("should be a class", () => {
79
		expect(input).toBeDefined();
80
	});
81
	describe("mouse", () => {
82
		beforeEach(() => {});
83
		it("should have mouse functions", () => {
84
			expect(input.getMouseButton).toBeDefined();
85
		});
86
		it("should detect mouse move", () => {
87
			expect(input.mouse.position.x).toBe(0);
88
			expect(input.mouse.position.y).toBe(0);
89
			mouseEvent("mousemove", { clientX: 10, clientY: 20 });
90
			expect(input.mouse.position.x).toBe(10);
91
			expect(input.mouse.position.y).toBe(20);
92
		});
93
		it("should mouseup and mousedown", () => {
94
			expect(input.getMouseButtonDown("left")).toBe(false);
95
			mouseEvent("mousedown", { button: 0 });
96
			expect(input.getMouseButtonDown("left")).toBe(true);
97
			input.endTick();
98
			expect(input.getMouseButtonDown("left")).toBe(false);
99
			expect(input.getMouseButtonUp("left")).toBe(false);
100
			mouseEvent("mouseup", { button: 0 });
101
			expect(input.getMouseButtonUp("left")).toBe(true);
102
			input.endTick();
103
			expect(input.getMouseButtonUp("left")).toBe(false);
104
		});
105
	});
106
	describe("getMouseButton", () => {
107
		it("should detect clicks", () => {
108
			expect(input.getMouseButton("left")).toBe(0);
109
			mouseEvent("mousedown", { button: 0 });
110
			expect(input.getMouseButton(0)).toBe(1);
111
			mouseEvent("mouseup", { button: 0 });
112
			expect(input.getMouseButton(0)).toBe(0);
113
		});
114
	});
115
	describe("getAxis", () => {
116
		it("should exist", () => {
117
			expect(input.getAxis).toBeDefined();
118
		});
119
		it("should keyboard", () => {
120
			window.onkeydown({ keyCode: 37 });
121
			expect(input.getDevice()).toBe("keyboard");
122
			expect(input.getKey("left")).toBe(1);
123
			expect(input.getAxis("horizontal")).toBe(-1);
124
			window.onkeydown({ keyCode: 39 });
125
			expect(input.getKey("right")).toBe(1);
126
			expect(input.getAxis("horizontal")).toBe(0);
127
			window.onkeyup({ keyCode: 37 });
128
			expect(input.getKey("left")).toBe(0);
129
			expect(input.getAxis("horizontal")).toBe(1);
130
		});
131
		it("should gamepad", () => {
132
			let gp = mockGamepad();
133
			gp.timestamp = 100;
134
			gp.axes[0] = 1;
135
			spyOn(navigator, "getGamepads").and.returnValue({
0 ignored issues
show
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
136
				length: 1,
137
				"0": gp
138
			});
139
			input.endTick();
140
			expect(input.getDevice()).toBe("gamepad");
141
			expect(input.getAxis("horizontal")).toBe(1);
142
		});
143
		// it("should gamepad", ());
144
	});
145
146
	describe("endTick", () => {
147
		it("should notify gamepad", () => {
148
			spyOn(input.gamepad, "endTick");
149
			input.endTick();
150
			expect(input.gamepad.endTick).toHaveBeenCalled();
151
		});
152
		it("should notify keyboard", () => {
153
			spyOn(input.keyboard, "endTick");
154
			input.endTick();
155
			expect(input.keyboard.endTick).toHaveBeenCalled();
156
		});
157
		it("should notify mouse", () => {
158
			spyOn(input.mouse, "endTick");
159
			input.endTick();
160
			expect(input.mouse.endTick).toHaveBeenCalled();
161
		});
162
	});
163
164
	describe("getButton", () => {
165
		describe("active device selective ignorance", () => {
166
			it("should return keyboard value if it was used after gamepad", () => {
167
				let gp = mockGamepad();
168
				gp.timestamp = 1;
169
				gp.buttons[0].pressed = true;
170
				gp.buttons[0].value = 1;
171
				spyOn(navigator, "getGamepads").and.returnValue({
0 ignored issues
show
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
172
					length: 1,
173
					"0": gp
174
				});
175
				input.endTick();
176
				expect(input.getLastActivityDevice()).toBe("gamepad");
177
				expect(input.getButton("jump")).toBe(1);
178
				window.onkeyup({ keyCode: 32 });
179
				expect(input.getLastActivityDevice()).toBe("keyboard");
180
				expect(input.getButton("jump")).toBe(0);
181
			});
182
			it("should return gamepad value if it was used after keyboard", done => {
183
				window.onkeydown({ keyCode: 32 });
184
				expect(input.getLastActivityDevice()).toBe("keyboard");
185
				expect(input.getButton("jump")).toBe(1);
186
				setTimeout(() => {
187
					//due to endtick a little bit later
188
					let gp = mockGamepad();
189
					gp.timestamp = 1;
190
					gp.buttons[0].pressed = false;
191
					gp.buttons[0].value = 0;
192
					spyOn(navigator, "getGamepads").and.returnValue({
0 ignored issues
show
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
193
						length: 1,
194
						"0": gp
195
					});
196
					input.endTick();
197
					expect(input.getLastActivityDevice()).toBe("gamepad");
198
					expect(input.getButton("jump")).toBe(0);
199
					done();
200
				}, 10);
201
			});
202
		});
203
		describe("GamePad", () => {
204
			// it("should register");
205
		});
206
	});
207
208
	describe("getButtonDown", () => {
209
		describe("keyboard", () => {
210
			// it("should change from frame to frame", () => {
211
			// 	expect(input.getButtonDown("raw")).toBe(false);
212
			// });
213
		});
214
	});
215
	describe("getLastActivityDevice", () => {
216
		it("mouse", () => {
217
			mouseEvent("mousemove", { clientX: 10, clientY: 20 });
218
			expect(input.getLastActivityDevice()).toBe("mouse");
219
		});
220
		it("keyboard", () => {
221
			window.onkeydown({ keyCode: 1 });
222
			expect(input.getLastActivityDevice()).toBe("keyboard");
223
		});
224
		it("gamepad", () => {
225
			spyOn(navigator, "getGamepads").and.returnValue({
0 ignored issues
show
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
226
				"0": { timestamp: 1234 },
227
				length: 1
228
			});
229
			input.endTick();
230
			expect(input.getLastActivityDevice()).toBe("gamepad");
231
		});
232
	});
233
});
234