GameStore-test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. jest
  2. .dontMock('chess.js')
  3. .dontMock('../GameStore')
  4. .dontMock('../../constants/GameConstants');
  5. require('es6-shim');
  6. const GameConstants = require('../../constants/GameConstants');
  7. const AppDispatcher = require('../../dispatcher/AppDispatcher');
  8. const GameStore = require('../GameStore');
  9. describe('GameStore', () => {
  10. var actionMakeMove = {
  11. source: 'VIEW_ACTION',
  12. action: {
  13. actionType: GameConstants.MAKE_MOVE,
  14. from: 'e2',
  15. to: 'e4',
  16. capture: undefined,
  17. emitMove: true
  18. }
  19. };
  20. var actionChangePromotion = {
  21. source: 'VIEW_ACTION',
  22. action: {
  23. actionType: GameConstants.CHANGE_PROMOTION,
  24. promotion: 'b'
  25. }
  26. };
  27. var actionGameOver = {
  28. source: 'VIEW_ACTION',
  29. action: {
  30. actionType: GameConstants.GAME_OVER,
  31. options: {
  32. winner: 'White',
  33. type: 'timeout'
  34. }
  35. }
  36. };
  37. var actionRematch = {
  38. source: 'VIEW_ACTION',
  39. action: {
  40. actionType: GameConstants.REMATCH
  41. }
  42. };
  43. var state, chessboard, moves, cp;
  44. const callback = function(cb) {
  45. AppDispatcher.register.mock.calls[0][0](cb);
  46. state = GameStore.getState();
  47. chessboard = GameStore.getChessboardState();
  48. moves = GameStore.getMoves();
  49. cp = GameStore.getCapturedPieces();
  50. };
  51. it('should register a callback with the dispatcher', () => {
  52. expect(AppDispatcher.register.mock.calls.length).toBe(1);
  53. });
  54. it('should return initial store values', () => {
  55. state = GameStore.getState();
  56. chessboard = GameStore.getChessboardState();
  57. expect(state.gameOver.get('status')).toBeFalsy();
  58. expect(state.promotion).toBe('q');
  59. expect(state.turn).toBe('w');
  60. expect(state.check).toBeFalsy();
  61. expect(chessboard.lastMove.isEmpty()).toBeTruthy();
  62. expect(chessboard.fen)
  63. .toBe('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
  64. });
  65. it('should make move', () => {
  66. callback(actionMakeMove);
  67. expect(state.turn).toBe('b');
  68. expect(chessboard.lastMove.get('from')).toBe('e2');
  69. expect(chessboard.lastMove.get('to')).toBe('e4');
  70. expect(moves.getIn([0, 0])).toBe('e4');
  71. expect(state.check).toBeFalsy();
  72. });
  73. it('should make more moves', () => {
  74. actionMakeMove.action.from = 'e7';
  75. actionMakeMove.action.to = 'e5';
  76. actionMakeMove.action.emitMove = false;
  77. callback(actionMakeMove);
  78. actionMakeMove.action.from = 'f2';
  79. actionMakeMove.action.to = 'f4';
  80. actionMakeMove.action.emitMove = true;
  81. callback(actionMakeMove);
  82. expect(state.turn).toBe('b');
  83. expect(moves.getIn([1, 0])).toBe('f4');
  84. expect(chessboard.fen)
  85. .toBe('rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR b KQkq f3 0 2');
  86. expect(cp.get('w').isEmpty).toBeTruthy();
  87. expect(cp.get('b').isEmpty).toBeTruthy();
  88. });
  89. it('should not change any values if move is invalid', () => {
  90. actionMakeMove.action.from = 'a1';
  91. actionMakeMove.action.to = 'a3';
  92. callback(actionMakeMove);
  93. expect(state.turn).toBe('b');
  94. expect(moves.getIn([1, 0])).toBe('f4');
  95. expect(chessboard.fen)
  96. .toBe('rnbqkbnr/pppp1ppp/8/4p3/4PP2/8/PPPP2PP/RNBQKBNR b KQkq f3 0 2');
  97. expect(cp.get('w').isEmpty).toBeTruthy();
  98. expect(cp.get('b').isEmpty).toBeTruthy();
  99. expect(state.check).toBeFalsy();
  100. });
  101. it('should capture piece', () => {
  102. actionMakeMove.action.from = 'e5';
  103. actionMakeMove.action.to = 'f4';
  104. actionMakeMove.action.capture = 'P';
  105. actionMakeMove.action.emitMove = false;
  106. callback(actionMakeMove);
  107. expect(state.turn).toBe('w');
  108. expect(moves.getIn([1, 1])).toBe('exf4');
  109. expect(cp.get('w').first()).toBe('P');
  110. });
  111. it('should change promotion to bishop', () => {
  112. callback(actionChangePromotion);
  113. expect(state.promotion).toBe('b');
  114. });
  115. it('should finish the game', () => {
  116. callback(actionGameOver);
  117. expect(state.gameOver.get('status')).toBeTruthy();
  118. expect(state.gameOver.get('winner')).toBe('White');
  119. expect(state.gameOver.get('type')).toBe('timeout');
  120. });
  121. it('should put the store to initial state after rematch accepted', () => {
  122. callback(actionRematch);
  123. expect(state.gameOver.get('status')).toBeFalsy();
  124. expect(state.promotion).toBe('q');
  125. expect(state.turn).toBe('w');
  126. expect(state.check).toBeFalsy();
  127. expect(chessboard.lastMove.isEmpty()).toBeTruthy();
  128. expect(chessboard.fen)
  129. .toBe('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
  130. expect(moves.size).toBe(0);
  131. expect(cp.get('w').isEmpty()).toBeTruthy();
  132. expect(cp.get('b').isEmpty()).toBeTruthy();
  133. });
  134. });