-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.test.tsx
256 lines (201 loc) · 8.82 KB
/
App.test.tsx
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { App, calculateWinner, SquareValue } from './App';
test('Renders the game initial status', () => {
render(<App />);
const nextPlayerLabel = screen.getByText('Next player: X');
expect(nextPlayerLabel).toBeInTheDocument();
const goToGameStartButton = screen.getByRole((role, element) =>
role === 'button' && element?.innerHTML === 'Go to game start');
expect(goToGameStartButton).toBeInTheDocument();
// Expect there to be 10 buttons: 9 board squares and 1 history button
expect(screen.getAllByRole('button').length).toEqual(10);
});
test('Renders "X" and "O" when squares are clicked in turn', async () => {
render(<App />);
const squares = screen.getAllByRole((role, element) =>
role === 'button' && element?.className === 'square');
expect(squares.map(s => s.innerHTML)).toEqual(Array(9).fill(''));
expect(screen.getByText('Next player: X')).toBeInTheDocument();
const sq0 = squares[0];
const sq1 = squares[1];
await userEvent.click(sq0);
expect(sq0.innerHTML).toEqual('X');
expect(sq1.innerHTML).toEqual('');
expect(screen.getByText('Next player: O')).toBeInTheDocument();
await userEvent.click(sq1);
expect(sq0.innerHTML).toEqual('X');
expect(sq1.innerHTML).toEqual('O');
expect(screen.getByText('Next player: X')).toBeInTheDocument();
});
test('Renders the winner and prevents further square clicks', async () => {
render(<App />);
const squares = screen.getAllByRole((role, element) =>
role === 'button' && element?.className === 'square');
expect(squares.map(s => s.innerHTML)).toEqual(Array(9).fill(''));
expect(screen.getByText('Next player: X')).toBeInTheDocument();
await userEvent.click(squares[0]);
expect(screen.getByText('Next player: O')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', '', '', '', '', '', '', '', '']);
await userEvent.click(squares[1]);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', 'O', '', '', '', '', '', '', '']);
await userEvent.click(squares[4]);
expect(screen.getByText('Next player: O')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', 'O', '', '', 'X', '', '', '', '']);
await userEvent.click(squares[2]);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', 'O', 'O', '', 'X', '', '', '', '']);
await userEvent.click(squares[8]);
expect(screen.getByText('Winner: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', 'O', 'O', '', 'X', '', '', '', 'X']);
await userEvent.click(squares[3]);
expect(screen.getByText('Winner: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', 'O', 'O', '', 'X', '', '', '', 'X']);
});
test('Renders history buttons', async () => {
render(<App />);
const squares = screen.getAllByRole((role, element) =>
role === 'button' && element?.className === 'square');
expect(squares.length).toEqual(9);
const sq0 = squares[0];
const sq1 = squares[1];
await userEvent.click(sq0);
await userEvent.click(sq1);
const historyButtons = screen.getAllByRole((role, element) =>
role === 'button' && element?.className !== 'square');
expect(historyButtons.map(b => b.innerHTML))
.toEqual(['Go to game start', 'Go to move #1', 'Go to move #2']);
});
test('Renders previous game states when history buttons are clicked', async () => {
render(<App />);
const squares = screen.getAllByRole((role, element) =>
role === 'button' && element?.className === 'square');
expect(squares.length).toEqual(9);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
const sq0 = squares[0];
const sq1 = squares[1];
await userEvent.click(sq0);
await userEvent.click(sq1);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', 'O', '', '', '', '', '', '', '']);
const gameStartButton = screen.getByText('Go to game start');
const move1Button = screen.getByText('Go to move #1');
const move2Button = screen.getByText('Go to move #2');
await userEvent.click(move1Button);
expect(screen.getByText('Next player: O')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', '', '', '', '', '', '', '', '']);
await userEvent.click(gameStartButton);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['', '', '', '', '', '', '', '', '']);
await userEvent.click(move2Button);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['X', 'O', '', '', '', '', '', '', '']);
});
test('Removes some history buttons when the history changes', async () => {
render(<App />);
const squares = screen.getAllByRole((role, element) =>
role === 'button' && element?.className === 'square');
expect(squares.length).toEqual(9);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
const sq0 = squares[0];
const sq1 = squares[1];
await userEvent.click(sq0);
await userEvent.click(sq1);
const getHistoryButtons = () => screen.getAllByRole((role, element) =>
role === 'button' && element?.className !== 'square');
let historyButtons = getHistoryButtons();
expect(historyButtons.length).toEqual(3);
const gameStartButton = historyButtons[0];
await userEvent.click(gameStartButton);
expect(screen.getByText('Next player: X')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['', '', '', '', '', '', '', '', '']);
await userEvent.click(sq1);
expect(screen.getByText('Next player: O')).toBeInTheDocument();
expect(squares.map(s => s.innerHTML))
.toEqual(['', 'X', '', '', '', '', '', '', '']);
expect(getHistoryButtons().length).toEqual(2);
});
test('Calculates the winner (calculateWinner() algorithm)', () => {
const testBoards = getTestBoards();
for (const board of testBoards) {
const winner: SquareValue = calculateWinner(board.board);
expect(winner).toEqual(board.winner);
}
function invertPlayer(player: SquareValue): SquareValue {
return player === 'X' ? 'O' : (player === 'O' ? 'X' : player);
}
// Repeat the tests swapping 'X' and 'O' players
for (const board of testBoards) {
const Oboard = {
board: board.board.map(v => invertPlayer(v)),
winner: invertPlayer(board.winner),
}
const winner: SquareValue = calculateWinner(Oboard.board);
expect(winner).toEqual(Oboard.winner);
}
});
function getTestBoards(): Array<{ board: SquareValue[], winner: SquareValue }> {
const winnerRowBoards = `
| X X X | X X X | X X X | O O | O O | O | O |
| O | O | O | X X X | X X X | O | O |
| O | O | O | | | X X X | X X X |
`;
const winnerColumnBoards = `
| X O O | X | X | O X | X O | O X | X |
| X | X O O | X | O X | X O | O X | O X |
| X | X | X O O | X | X | X | O X |
`;
const winnerDiagonalBoards = `
| X O O | X | X | X O | O O X | X | O X | X |
| X | X | O X | X O | X | X | O X | X O |
| X | O O X | O X | X | X | X O O | X | X O |
`;
const noWinnerBoards = `
| X O O | X O X | X | X O | |
| O X X | X X O | | | |
| X X O | O X O | | | |
`;
/** Parse tic-tac-toe boards in the ASCII-art format above */
function parseTicTacToeBoards(boardLines: string): SquareValue[][] {
// Sample input: '| O O X | O X |'
// Sample output: [ ['O', 'O', 'X'], ['O', null, 'X'] ]
function parseLine(line: string): SquareValue[][] {
return (line.trim().split('|').filter(chunk => chunk)
.map(chunk => [chunk[1], chunk[3], chunk[5]]
.map(v => v === ' ' ? null : v) as SquareValue[])
);
}
function mergeChunks(chunks: SquareValue[][], accumulator: SquareValue[][]) {
if (accumulator.length === 0) {
accumulator.push(...chunks);
} else {
chunks.forEach((chunk, i) => accumulator[i].push(...chunk));
}
}
const boards: SquareValue[][] = [];
boardLines.split('\n').forEach(line =>
mergeChunks(parseLine(line), boards));
return boards;
}
const testBoards: Array<{ board: SquareValue[], winner: SquareValue }> = [];
const xWinnerBoards = [winnerRowBoards, winnerColumnBoards, winnerDiagonalBoards];
xWinnerBoards.forEach(boards =>
parseTicTacToeBoards(boards)
.forEach(board => testBoards.push({ board, winner: 'X' }))
);
parseTicTacToeBoards(noWinnerBoards)
.forEach(board => testBoards.push({ board, winner: null }));
return testBoards;
}