forked from simondiep/node-multiplayer-snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameControlsServiceTest.js
65 lines (55 loc) · 2.47 KB
/
GameControlsServiceTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const assert = require('chai').assert;
const Direction = require('../app/models/direction');
const Player = require('../app/models/player');
const GameControlsService = require('../app/services/game-controls-service');
describe('GameControlsService', () => {
'use strict';
it('should get valid next moves when moving up', done => {
const nextMoves = GameControlsService.getValidNextMove(Direction.UP);
assert.deepEqual(nextMoves, [Direction.LEFT, Direction.RIGHT]);
done();
});
it('should get valid next moves when moving down', done => {
const nextMoves = GameControlsService.getValidNextMove(Direction.DOWN);
assert.deepEqual(nextMoves, [Direction.LEFT, Direction.RIGHT]);
done();
});
it('should get valid next moves when moving left', done => {
const nextMoves = GameControlsService.getValidNextMove(Direction.LEFT);
assert.deepEqual(nextMoves, [Direction.UP, Direction.DOWN]);
done();
});
it('should get valid next moves when moving right', done => {
const nextMoves = GameControlsService.getValidNextMove(Direction.RIGHT);
assert.deepEqual(nextMoves, [Direction.UP, Direction.DOWN]);
done();
});
it('should handle directional key downs and change player direction', done => {
const player = new Player();
player.direction = Direction.RIGHT;
player.directionBeforeMove = Direction.RIGHT;
let keyCode = 38; // Up arrow
GameControlsService.handleKeyDown(player, keyCode);
assert.equal(player.direction, Direction.UP);
keyCode = 40; // Down arrow
GameControlsService.handleKeyDown(player, keyCode);
assert.equal(player.direction, Direction.DOWN);
player.directionBeforeMove = Direction.UP;
keyCode = 37; // Left arrow
GameControlsService.handleKeyDown(player, keyCode);
assert.equal(player.direction, Direction.LEFT);
keyCode = 39; // Right arrow
GameControlsService.handleKeyDown(player, keyCode);
assert.equal(player.direction, Direction.RIGHT);
done();
});
it('should not handle a non-directional key down', done => {
const player = new Player();
player.direction = Direction.RIGHT;
player.directionBeforeMove = Direction.RIGHT;
const keyCode = 82; // r
GameControlsService.handleKeyDown(player, keyCode);
assert.equal(player.direction, Direction.RIGHT);
done();
});
});