-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.test.js
More file actions
82 lines (72 loc) · 2.42 KB
/
tictactoe.test.js
File metadata and controls
82 lines (72 loc) · 2.42 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
const { expect } = require('chai');
// tictactoe.test.js
describe('TicTacToe', () => {
let TicTacToe;
before(() => {
TicTacToe = require('./tictactoe.js').TicTacToe || require('./tictactoe.js');
});
it('should initialize with empty board and X as current player', () => {
const game = new TicTacToe();
expect(game.board).to.deep.equal(Array(9).fill(null));
expect(game.currentPlayer).to.equal('X');
expect(game.winner).to.be.null;
});
it('should allow players to make moves and alternate turns', () => {
const game = new TicTacToe();
expect(game.makeMove(0)).to.be.true;
expect(game.board[0]).to.equal('X');
expect(game.currentPlayer).to.equal('O');
expect(game.makeMove(1)).to.be.true;
expect(game.board[1]).to.equal('O');
expect(game.currentPlayer).to.equal('X');
});
it('should not allow moves on occupied positions', () => {
const game = new TicTacToe();
game.makeMove(0);
expect(game.makeMove(0)).to.be.false;
});
it('should detect a win for X', () => {
const game = new TicTacToe();
game.makeMove(0); // X
game.makeMove(3); // O
game.makeMove(1); // X
game.makeMove(4); // O
game.makeMove(2); // X wins
expect(game.winner).to.equal('X');
});
it('should detect a win for O', () => {
const game = new TicTacToe();
game.makeMove(3); // X
game.makeMove(0); // O
game.makeMove(4); // X
game.makeMove(1); // O
game.makeMove(8); // X
game.makeMove(2); // O wins
expect(game.winner).to.equal('O');
});
it('should detect a draw', () => {
const game = new TicTacToe();
// X O X
// X X O
// O X O
game.makeMove(0); // X
game.makeMove(1); // O
game.makeMove(2); // X
game.makeMove(5); // O
game.makeMove(3); // X
game.makeMove(6); // O
game.makeMove(4); // X
game.makeMove(8); // O
game.makeMove(7); // X
expect(game.winner).to.equal('Draw');
});
it('should not allow moves after game is won', () => {
const game = new TicTacToe();
game.makeMove(0); // X
game.makeMove(3); // O
game.makeMove(1); // X
game.makeMove(4); // O
game.makeMove(2); // X wins
expect(game.makeMove(5)).to.be.false;
});
});