-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.js
More file actions
163 lines (162 loc) · 6.6 KB
/
server.test.js
File metadata and controls
163 lines (162 loc) · 6.6 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const { game } = require('../battleship/app.js')
var battleship_game = false;
beforeEach(() => {
battleship_game = new game()
})
describe("server-side game testing", () => {
describe("grid functionality",() => {
it("places ships correcty horizontally", () => {
battleship_game.place_ship(0,[0,0],[5,0])
expect(battleship_game.player_grid[0][0][3]).toBe(1)
})
it("places ships correctly vertically", () => {
battleship_game.place_ship(1,[1,0],[1,5])
expect(battleship_game.player_grid[1][4][1]).toBe(1)
})
it("checks valid ship position correctly for invalid placement", () => {
battleship_game.player_grid[0][0] = [0,0,1,1,1,0,0,0]
expect(battleship_game.check_valid_ship_placement(0,[1,0],[6,0])).toBe(false)
})
it("checks valid ship position correctly for valid placement", () => {
battleship_game.player_grid[0][0] = [1,1,1,0,0,0,0,0]
expect(battleship_game.check_valid_ship_placement(0,[3,0],[6,0])).toBe(true)
})
it("hit updates board correctly", () => {
let send = jest.fn();
let on = jest.fn()
const player_1 = {send, on}
const player_2 = {send, on}
battleship_game.add_player(player_1)
battleship_game.add_player(player_2)
battleship_game.player_grid[1][5][3] = 1
battleship_game.hit(0,[5,3])
expect(battleship_game.player_grid[1][3][5]).toBe(2)
})
it("hit function returns correct value", () => {
let send = jest.fn();
let on = jest.fn()
const player_1 = {send, on}
const player_2 = {send, on}
battleship_game.add_player(player_1)
battleship_game.add_player(player_2)
battleship_game.player_grid[1][4] = [0,0,0,1,1,1,0,0]
expect(battleship_game.hit(0,[4,4])).toBe(true)
})
})
describe("game functionality", () => {
it("swaps turns correctly", () => {
let send = jest.fn();
let on = jest.fn()
battleship_game.add_player({send, on})
battleship_game.add_player({send, on})
battleship_game.turn = 0
battleship_game.swap_turns()
expect(battleship_game.turn).toBe(1)
})
it("adds one player correctly", () => {
let send;
const player_1 = {send}
battleship_game.add_player(player_1)
expect(battleship_game.players.length).toBe(1)
})
it("adds two player correctly", () => {
let send = jest.fn();
let on = jest.fn()
const player_1 = {send, on}
const player_2 = {send, on}
battleship_game.add_player(player_1)
battleship_game.add_player(player_2)
expect(send.mock.calls.length).toBe(2)
})
it("player can place ship in valid position", async () => {
let send = jest.fn();
let event_callback;
// stores event listener callback in event_callback
const on = (type,callback) => {
event_callback = callback
}
const player_1 = {send, on}
battleship_game.add_player(player_1)
await battleship_game.setup_receive_listeners()
const message = {
type: "PLACE_SHIP",
player_number : 0,
value: [[0,0],[5,0]]
}
await event_callback(JSON.stringify(message))
expect(battleship_game.player_grid[0][0][3]).toBe(1)
})
it("player can't place ship in invalid position", async () => {
battleship_game.player_grid[0][0] = [1,1,1,1,0,0,0,0]
let send = jest.fn();
let event_callback;
// stores event listener callback in event_callback
const on = (type,callback) => {
event_callback = callback
}
const player_1 = {send, on}
battleship_game.add_player(player_1)
await battleship_game.setup_receive_listeners()
const message = {
type: "PLACE_SHIP",
player_number : 0,
value: [[0,0],[5,0]]
}
await event_callback(JSON.stringify(message))
expect(battleship_game.player_grid[0][0][5]).toBe(0)
})
it("player can hit point on board",async () => {
let send = jest.fn();
let event_callback;
// stores event listener callback in event_callback
const on = (type,callback) => {
event_callback = callback
}
const player_1 = {send, on}
const player_2 = {send, on}
battleship_game.add_player(player_1)
battleship_game.add_player(player_2)
await battleship_game.setup_receive_listeners()
const message = {
type: "SEND_HIT",
player_number : 0,
value: [3,5]
}
await event_callback(JSON.stringify(message))
expect(battleship_game.player_grid[1][5][3]).toBe(2)
})
it("player can win game with hit", async () => {
battleship_game.player_grid[1][0] = [0,0,0,0,1,0,0,0]
let send = jest.fn();
let event_callback;
// stores event listener callback in event_callback
const on = (type,callback) => {
event_callback = callback
}
const player_1 = {send, on}
const player_2 = {send, on}
battleship_game.add_player(player_1)
battleship_game.add_player(player_2)
await battleship_game.setup_receive_listeners()
const message = {
type: "SEND_HIT",
player_number : 0,
value: [4,0]
}
await event_callback(JSON.stringify(message))
expect(battleship_game.won).toBe(true)
})
})
describe("checkwin functionality", () => {
it("detects player 1 win", () => {
const send = jest.fn()
const on = jest.fn()
const player_1 = {send, on}
const player_2 = {send, on}
battleship_game.add_player(player_1)
battleship_game.add_player(player_2)
battleship_game.player_grid[0] = [[0,2,2,2,0,0,0,0],[0,2,2,2,0,0,0,0],[0,2,2,2,0,0,0,0],[0,2,2,2,0,0,0,0],[0,2,2,2,0,0,0,0],[0,2,2,2,0,0,0,0],[0,2,2,2,0,0,0,0],[0,2,2,2,0,0,0,0]]
expect(battleship_game.checkwin()).toBe(1)
})
})
})