Browse Source

add game store tests

romanmatiasko 9 years ago
parent
commit
ac82a78a16
2 changed files with 149 additions and 0 deletions
  1. 1 0
      package.json
  2. 148 0
      src/js/stores/__tests__/GameStore-test.js

+ 1 - 0
package.json

@@ -38,6 +38,7 @@
     "gulp-streamify": "0.0.5",
     "gulp-uglify": "^1.1.0",
     "gulp-util": "^3.0.4",
+    "jest-cli": "^0.4.0",
     "lodash.omit": "^3.0.0",
     "react": "^0.13.0",
     "socket.io-client": "^1.3.5",

+ 148 - 0
src/js/stores/__tests__/GameStore-test.js

@@ -0,0 +1,148 @@
+jest
+  .dontMock('chess.js')
+  .dontMock('../GameStore')
+  .dontMock('../../constants/GameConstants');
+
+require('es6-shim');
+const GameConstants = require('../../constants/GameConstants');
+const AppDispatcher = require('../../dispatcher/AppDispatcher');
+const GameStore = require('../GameStore');
+
+describe('GameStore', () => {
+  
+  var actionMakeMove = {
+    source: 'VIEW_ACTION',
+    action: {
+      actionType: GameConstants.MAKE_MOVE,
+      from: 'e2',
+      to: 'e4',
+      capture: undefined,
+      emitMove: true
+    }
+  };
+  var actionChangePromotion = {
+    source: 'VIEW_ACTION',
+    action: {
+      actionType: GameConstants.CHANGE_PROMOTION,
+      promotion: 'b'
+    }
+  };
+  var actionGameOver = {
+    source: 'VIEW_ACTION',
+    action: {
+      actionType: GameConstants.GAME_OVER,
+      options: {
+        winner: 'White',
+        type: 'timeout'
+      }
+    }
+  };
+  var actionRematch = {
+    source: 'VIEW_ACTION',
+    action: {
+      actionType: GameConstants.REMATCH
+    }
+  };
+
+  var state, chessboard, moves, cp;
+
+  const callback = function(cb) {
+    AppDispatcher.register.mock.calls[0][0](cb);
+    state = GameStore.getState();
+    chessboard = GameStore.getChessboardState();
+    moves = GameStore.getMoves();
+    cp = GameStore.getCapturedPieces();
+  };
+
+  it('should register a callback with the dispatcher', () => {
+    expect(AppDispatcher.register.mock.calls.length).toBe(1);
+  });
+
+  it('should return initial store values', () => {
+    state = GameStore.getState();
+    chessboard = GameStore.getChessboardState();
+    expect(state.gameOver.get('status')).toBeFalsy();
+    expect(state.promotion).toBe('q');
+    expect(state.turn).toBe('w');
+    expect(state.check).toBeFalsy();
+    expect(chessboard.lastMove.isEmpty()).toBeTruthy();
+    expect(chessboard.fen)
+      .toBe('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
+  });
+
+  it('should make move', () => {
+    callback(actionMakeMove);
+    expect(state.turn).toBe('b');
+    expect(chessboard.lastMove.get('from')).toBe('e2');
+    expect(chessboard.lastMove.get('to')).toBe('e4');
+    expect(moves.getIn([0, 0])).toBe('e4');
+    expect(state.check).toBeFalsy();
+  });
+
+  it('should make more moves', () => {
+    actionMakeMove.action.from = 'e7';
+    actionMakeMove.action.to = 'e5';
+    actionMakeMove.action.emitMove = false;
+    callback(actionMakeMove);
+    actionMakeMove.action.from = 'f2';
+    actionMakeMove.action.to = 'f4';
+    actionMakeMove.action.emitMove = true;
+    callback(actionMakeMove);
+    expect(state.turn).toBe('b');
+    expect(moves.getIn([1, 0])).toBe('f4');
+    expect(chessboard.fen)
+      .toBe('rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR b KQkq f3 0 2');
+    expect(cp.get('w').isEmpty).toBeTruthy();
+    expect(cp.get('b').isEmpty).toBeTruthy();
+  });
+
+  it('should not change any values if move is invalid', () => {
+    actionMakeMove.action.from = 'a1';
+    actionMakeMove.action.to = 'a3';
+    callback(actionMakeMove);
+    expect(state.turn).toBe('b');
+    expect(moves.getIn([1, 0])).toBe('f4');
+    expect(chessboard.fen)
+      .toBe('rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR b KQkq f3 0 2');
+    expect(cp.get('w').isEmpty).toBeTruthy();
+    expect(cp.get('b').isEmpty).toBeTruthy();
+    expect(state.check).toBeFalsy();
+  });
+
+  it('should capture piece', () => {
+    actionMakeMove.action.from = 'e5';
+    actionMakeMove.action.to = 'f4';
+    actionMakeMove.action.capture = 'P';
+    actionMakeMove.action.emitMove = false;
+    callback(actionMakeMove);
+    expect(state.turn).toBe('w');
+    expect(moves.getIn([1, 1])).toBe('exf4');
+    expect(cp.get('w').first()).toBe('P');
+  });
+
+  it('should change promotion to bishop', () => {
+    callback(actionChangePromotion);
+    expect(state.promotion).toBe('b');
+  });
+
+  it('should finish the game', () => {
+    callback(actionGameOver);
+    expect(state.gameOver.get('status')).toBeTruthy();
+    expect(state.gameOver.get('winner')).toBe('White');
+    expect(state.gameOver.get('type')).toBe('timeout');
+  });
+
+  it('should put the store to initial state after rematch accepted', () => {
+    callback(actionRematch);
+    expect(state.gameOver.get('status')).toBeFalsy();
+    expect(state.promotion).toBe('q');
+    expect(state.turn).toBe('w');
+    expect(state.check).toBeFalsy();
+    expect(chessboard.lastMove.isEmpty()).toBeTruthy();
+    expect(chessboard.fen)
+      .toBe('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
+    expect(moves.size).toBe(0);
+    expect(cp.get('w').isEmpty()).toBeTruthy();
+    expect(cp.get('b').isEmpty()).toBeTruthy();
+  });
+});