-
Notifications
You must be signed in to change notification settings - Fork 0
/
academy.test.ts
233 lines (177 loc) · 7.18 KB
/
academy.test.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/******************************************
Exercise for the Reader :-)
Use your TDD skills from the JS Unit Testing session and Bowling Game Kata session
to make unit tests to drive out your Noughts & Crosses implementation.
The jest verbose flag has been set for you in "jest.config.js"
*********************************************/
import { Player, Cell, getBoard, setBoard } from './board'
import { checkWinner, crossText, getCurrentPlayer, getGameOver, noughtText, resetGame, setCurrentPlayer, switchPlayer, takeTurn } from './academy'
// Test: setBoard
describe ('This test ensures setBoard function works by establishing the contents of a 3x3 board', () => {
beforeEach(() => {
setBoard([[null, null, null], [null, null, null], [null, null, null]])
}
)
test ('it should set the board to an array of nulls', () => {
const result = getBoard()
expect (result).toEqual([[null, null, null], [null, null, null], [null, null, null]])
})
})
// Test: getBoard
describe ('This test ensures getBoard function works by returning the contents of a 3x3 board', () => {
beforeEach(() => {
setBoard([["cross", "cross", "cross"], [null, null, null], [null, null, null]])
}
)
test ('it should return the above array', () => {
const result = getBoard()
expect (result).toEqual([["cross", "cross", "cross"], [null, null, null], [null, null, null]])
})
})
// Test: reset game
describe ('This tests the functionality of the reset button', () => {
beforeEach(() => {
setBoard([["cross", "cross", "cross"], [null, null, null], [null, null, null]])
}
)
test ('reset should return a 3x3 array of nulls which is the original game state and board dimensions', () => {
//Arrange
const expectedResult = [[null, null, null], [null, null, null], [null, null, null]]
//Act
resetGame()
const result = getBoard()
//Assert
expect(result).toEqual(expectedResult)
})
})
// Test: take turn
describe ('This tests the takeTurn function', () => {
beforeEach(() => {
setBoard([[null, null, null], [null, null, null], [null, null, null]])
}
)
test ('if the game has finished: nothing happens and the board is retunred unchanged', () => {
//Arrange
setBoard ([["cross", "cross", "cross"], ["cross", "cross", "cross"], ["cross", "cross", "cross"]])
const expectedResult = getBoard()
//Act
const result = takeTurn(1,1,true,expectedResult,'nought')
console.log(result)
//Assert
expect(result).toEqual(expectedResult)
})
test ('if the game has not completed: if the cell is not empty, nothing happens and the board is not changed', () => {
//Arrange
setBoard ([[null, "cross", "cross"], ["cross", "cross", "cross"], ["cross", "cross", "cross"]])
const expectedResult = getBoard()
//Act
const result = takeTurn(0, 1, false, expectedResult, 'nought')
//Asset
expect(result).toEqual(expectedResult)
})
test ('if the game has not completed: if the cell is empty then the counter should be placed in that cell', () => {
//Arrange
setBoard ([[null, "cross", "cross"], ["cross", "cross", "cross"], ["cross", "cross", "cross"]])
const currentBoard = getBoard()
//Act
const result = takeTurn(0, 0, false, currentBoard, 'nought')
const expectedResult = getBoard()
//Asset
expect(result).toEqual(expectedResult)
})
})
// Test: switch player
describe ('This tests the functionality of switchPlayer', () => {
beforeEach(() => {
setBoard([[null, null, null], [null, null, null], [null, null, null]])
}
)
test ('it should switch the players only if the turn was completed', () => {
//Arrange
setCurrentPlayer ("cross")
const currentPlayer = getCurrentPlayer()
const expectedResult = "nought"
//Act
const result = switchPlayer(currentPlayer)
//Assert
expect(result).toEqual(expectedResult)
})
})
// Test: Check winner
describe ('This tests the functionality of checkWinner', () => {
beforeEach(() => {
setBoard([[null, null, null], [null, null, null], [null, null, null]])
}
)
test ('if there is a winner, return the name of the winner', () => {
//Arrange
setBoard ([["cross", "cross", "cross"], [null, null, null], [null, null, null]])
const currentBoard = getBoard()
const expectedResult = "cross"
//Act
const result = checkWinner(currentBoard)
//Assert
expect(result).toEqual(expectedResult)
})
test ('find if there is a winner in horizontal, row 1 or 2 or 3', () => {
//Arrange
setBoard ([["cross", null, null], ["cross", null, null], ["cross", null, null]])
const currentBoard = getBoard()
const expectedResult = "cross"
//Act
const result = checkWinner(currentBoard)
//Assert
expect(result).toEqual(expectedResult)
})
test ('find if there is a winner in verticle, row 1 or 2 or 3', () => {
//Arrange
setBoard ([["cross", null, null], ["cross", null, null], ["cross", null, null]])
const currentBoard = getBoard()
const expectedResult = "cross"
//Act
const result = checkWinner(currentBoard)
//Assert
expect(result).toEqual(expectedResult)
})
test ('find if there is a winner in diag, top left to bottom right', () => {
//Arrange
setBoard ([["cross", null, null], [null, "cross", null], [null, null, "cross"]])
const currentBoard = getBoard()
const expectedResult = "cross"
//Act
const result = checkWinner(currentBoard)
//Assert
expect(result).toEqual(expectedResult)
})
test ('find if there is a winner in anti-diag, top right to bottom left', () => {
//Arrange
setBoard ([[null, null, "cross"], [null, "cross", null], ["cross", null, null]])
const currentBoard = getBoard()
const expectedResult = "cross"
//Act
const result = checkWinner(currentBoard)
//Assert
expect(result).toEqual(expectedResult)
})
test ("return 'nobody' if the game is completed and there is no winner", () => {
//Arrange
setBoard ([["nought", "cross", "nought"], ["cross", "nought", "cross"], ["cross", "nought", "cross"]])
const currentBoard = getBoard()
const expectedResult = "nobody"
//Act
const result = checkWinner(currentBoard)
//Assert
expect(result).toEqual(expectedResult)
})
test ("return 'nobody' if the game is completed and there is no winner", () => {
//Arrange
setBoard ([[null, "cross", "nought"], ["cross", "nought", "cross"], ["cross", "nought", "cross"]])
const currentBoard = getBoard()
const expectedResult = undefined
//Act
const result = checkWinner(currentBoard)
//Assert
expect(result).toEqual(expectedResult)
})
})
//eof