-
Notifications
You must be signed in to change notification settings - Fork 2
/
BoardStore-test.js
203 lines (176 loc) · 7.13 KB
/
BoardStore-test.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
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
jest.dontMock('../../constants/AppConstants');
jest.dontMock('../BoardStore');
jest.dontMock('object-assign');
jest.dontMock('react/lib/keyMirror');
jest.dontMock('lodash');
describe('BoardStore', function() {
let AppConstants = require('../../constants/AppConstants');
let _ = require('lodash');
let AppDispatcher;
let BoardStore;
let callback;
let actionPlayPosition = {
action : {
type : AppConstants.ActionTypes.PLAY_POSITION,
pos : void 0
}
};
let playPosition = function(x, y) {
actionPlayPosition.action.pos = {x: x, y: y}; callback(actionPlayPosition);
}
beforeEach(function() {
AppDispatcher = require('../../dispatchers/AppDispatcher');
BoardStore = require('../BoardStore');
callback = AppDispatcher.register.mock.calls[0][0];
});
it('registers a callback with the dispatcher', function() {
expect(AppDispatcher.register.mock.calls.length).toBe(1);
});
it('should initialize with empty board', function() {
let tiles = BoardStore.getTiles();
let boardSize = BoardStore.getSize();
for(let x = 0; x < boardSize; x++) {
for(let y = 0; y < boardSize; y++) {
expect(tiles[x][y]).toBe(0);
}
}
});
it('should initialize without a winner', function() {
expect(BoardStore.getWinner()).toBe(void 0);
});
it('should initialize without a game ended', function() {
expect(BoardStore.gameEnded()).toBe(false);
});
it('should have player 1 as first player', function() {
expect(BoardStore.getCurrentPlayer()).toBe(1);
playPosition(1, 2);
let tiles = BoardStore.getTiles();
expect(tiles[1][2]).toBe(1);
});
it('should have player 2 as second player', function() {
playPosition(0, 0);
expect(BoardStore.getCurrentPlayer()).toBe(2);
playPosition(1, 0);
let tiles = BoardStore.getTiles();
expect(tiles[1][0]).toBe(2);
});
describe('invalid play', function() {
it('should not switch player when play on the same tile', function() {
playPosition(0, 0);
playPosition(0, 0);
expect(BoardStore.getCurrentPlayer()).toBe(2);
});
it('should not switch player when play out of bounds (too big)', function() {
playPosition(5, 0);
expect(BoardStore.getCurrentPlayer()).toBe(1);
});
it('should not switch player when play out of bounds (too small)', function() {
playPosition(-1, 0);
expect(BoardStore.getCurrentPlayer()).toBe(1);
});
it('should not play after game ended', function() {
playPosition(0, 0); // player 1
playPosition(1, 1); // player 2
playPosition(0, 1); // player 1
playPosition(1, 2); // player 2
playPosition(0, 2); // player 1 wins!
playPosition(2, 2); // Invalid move, should not be counted.
let tiles = BoardStore.getTiles();
expect(tiles[2][2]).toBe(0);
});
})
it('should not override when play on the same tile', function() {
playPosition(0, 0);
playPosition(0, 0);
let tiles = BoardStore.getTiles();
expect(tiles[0][0]).toBe(1);
});
describe('win', function() {
it('should win when player 1 makes 3 tiles in vertical', function() {
playPosition(0, 0); // player 1
playPosition(1, 1); // player 2
playPosition(0, 1); // player 1
playPosition(1, 2); // player 2
playPosition(0, 2); // player 1
expect(BoardStore.getWinner()).toBe(1);
expect(BoardStore.gameEnded()).toBe(true);
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 0})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 1})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 2})).toBeTruthy();
expect(BoardStore.getWinnerTiles().length).toBe(3);
});
it('should win when player 1 makes 3 tiles in horizontal', function() {
playPosition(0, 0); // player 1
playPosition(1, 1); // player 2
playPosition(1, 0); // player 1
playPosition(1, 2); // player 2
playPosition(2, 0); // player 1
expect(BoardStore.getWinner()).toBe(1);
expect(BoardStore.gameEnded()).toBe(true);
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 0})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 1, y: 0})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 2, y: 0})).toBeTruthy();
expect(BoardStore.getWinnerTiles().length).toBe(3);
});
it('should win when player 1 makes the diagonal top-left to bottom-right', function() {
playPosition(0, 0); // player 1
playPosition(1, 0); // player 2
playPosition(1, 1); // player 1
playPosition(1, 2); // player 2
playPosition(2, 2); // player 1
expect(BoardStore.getWinner()).toBe(1);
expect(BoardStore.gameEnded()).toBe(true);
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 0})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 1, y: 1})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 2, y: 2})).toBeTruthy();
expect(BoardStore.getWinnerTiles().length).toBe(3);
});
it('should win when player 1 makes the diagonal top-right to bottom-left', function() {
playPosition(2, 0); // player 1
playPosition(1, 0); // player 2
playPosition(1, 1); // player 1
playPosition(1, 2); // player 2
playPosition(0, 2); // player 1
expect(BoardStore.getWinner()).toBe(1);
expect(BoardStore.gameEnded()).toBe(true);
expect(_.find(BoardStore.getWinnerTiles(), {x: 2, y: 0})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 1, y: 1})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 2})).toBeTruthy();
expect(BoardStore.getWinnerTiles().length).toBe(3);
});
it('should set all 5 tiles as winning tiles on T bone winning scheme', function() {
playPosition(0, 0); // player 1
playPosition(1, 0); // player 2
playPosition(1, 1); // player 1
playPosition(2, 0); // player 2
playPosition(2, 1); // player 1
playPosition(1, 2); // player 2
playPosition(0, 2); // player 1
playPosition(2, 2); // player 2
playPosition(0, 1); // player 1
expect(BoardStore.getWinner()).toBe(1);
expect(BoardStore.gameEnded()).toBe(true);
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 0})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 1, y: 1})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 2, y: 1})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 2})).toBeTruthy();
expect(_.find(BoardStore.getWinnerTiles(), {x: 0, y: 1})).toBeTruthy();
expect(BoardStore.getWinnerTiles().length).toBe(5);
});
// @todo: test for T where more than 3 tiles are winning tiles at the same time
})
it('should end the game when no more tiles are available', function() {
playPosition(0, 0);
playPosition(0, 1);
playPosition(0, 2);
playPosition(1, 0);
playPosition(1, 2);
playPosition(1, 1);
playPosition(2, 0);
playPosition(2, 2);
playPosition(2, 1);
expect(BoardStore.getWinner()).toBe(void 0);
expect(BoardStore.gameEnded()).toBe(true);
expect(BoardStore.isDrawGame()).toBe(true);
});
});