Skip to content

Commit 8cd28fc

Browse files
committed
fix(frontend): broadcast moves in use play hook
1 parent b4033da commit 8cd28fc

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

frontend/src/components/Play/Play.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ export default function Play() {
2525
const [gameStarted, setGameStarted] = React.useState(true);
2626

2727
const chessBoardStateHandlers = useChessBoardState({ color, isEnabled: gameStarted });
28-
const { handleClientMakeMove } = chessBoardStateHandlers;
28+
const { handleClientMakeMove, setBroadcastMove } = chessBoardStateHandlers;
2929

3030
const { id: gameId } = useParams();
3131

3232
const playGameApi = usePlayApi({ gameId, clientUsername, setColor, setGameStarted, handleClientMakeMove });
3333
const { connectionState, players, highlightDrawButton, gameResult, broadcastMove, handleResign, handleOfferDraw } =
3434
playGameApi;
35+
setBroadcastMove(broadcastMove);
3536

3637
const chessProps = {
3738
color: color,
3839
players: players,
3940
gameStarted: gameStarted,
4041
gameResult: gameResult,
41-
broadcastMove: broadcastMove,
4242
actions: {
4343
playActions: {
4444
highlightDraw: highlightDrawButton,

frontend/src/components/shared/Chess/useChessBoardState/handleMovePiece.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type MoveOptions = Omit<SelectPieceOptions, "piece"> & {
1212

1313
export const handleMoveTo = (
1414
chessBoardState: ChessBoardState,
15-
options: MoveOptions & { moveTo: Position },
15+
options: MoveOptions & Required<Pick<MoveOptions, "broadcastMove">> & { moveTo: Position },
1616
): ChessBoardState => {
1717
const { moveTo, chess } = options;
1818
const { selectedPiece } = chessBoardState;

frontend/src/components/shared/Chess/useChessBoardState/useChessBoardState.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type ChessBoardStateHandlersProps = {
2222
chess: Chess;
2323
colorToPlay: Color;
2424
setColorToPlay: (color: Color) => void;
25+
setBroadcastMove: (broadcastMove: ((move: MoveInfo) => void) | null) => void;
2526
handleClientMakeMove: (move: Move, promotionPiece: PromotionPieceType | null) => void;
2627
handleClientUndoMove: () => void;
2728
handleHoveringOver: (position: Position) => void;
@@ -40,6 +41,7 @@ export const useChessBoardState = (props: Props): ChessBoardStateHandlersProps =
4041
const { color, isEnabled } = props;
4142

4243
const [colorToPlay, setColorToPlay] = React.useState<Color>(color);
44+
const broadcastMove = React.useRef<((move: MoveInfo) => void) | null>(null);
4345
const chess = React.useRef(new Chess()).current;
4446

4547
const [chessBoardState, setChessBoardState] = React.useState<ChessBoardState>({
@@ -56,7 +58,9 @@ export const useChessBoardState = (props: Props): ChessBoardStateHandlersProps =
5658
};
5759

5860
const handleMovedTo = (moveTo: Position) => {
59-
const options = { moveTo, chess, isEnabled, color, setColorToPlay };
61+
if (!broadcastMove.current) throw new Error("broadcastMove function is required for broadcasting moves");
62+
63+
const options = { moveTo, chess, isEnabled, color, setColorToPlay, broadcastMove: broadcastMove.current };
6064
setChessBoardState((chessBoardState) => handleMoveTo(chessBoardState, options));
6165
};
6266

@@ -95,11 +99,16 @@ export const useChessBoardState = (props: Props): ChessBoardStateHandlersProps =
9599
setColorToPlay(chess.board.colorToPlay);
96100
};
97101

102+
const setBroadcastMove = (newBroadcastMove: (move: MoveInfo) => void) => {
103+
broadcastMove.current = newBroadcastMove;
104+
};
105+
98106
return {
99107
chessBoardState,
100108
chess,
101109
colorToPlay,
102110
setColorToPlay,
111+
setBroadcastMove,
103112
handleClientMakeMove,
104113
handleClientUndoMove,
105114
handleMaybeGetPromotionPiece,

frontend/test/components/Play/Play.test.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { act, render, screen, waitFor } from "@testing-library/react";
1+
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
22
import Play from "components/Play/Play";
33
import { AppContext, AppContextType } from "hooks/appContext";
44
import { expect, test, vi } from "vitest";
@@ -64,3 +64,19 @@ test("Should display chessboard after game is loaded", async () => {
6464

6565
expect(screen.getByTestId("square-0-0", { exact: false })).toBeTruthy();
6666
});
67+
68+
test("Should move pieces and broadcast moves", async () => {
69+
await loadGame();
70+
71+
const squareE2 = screen.getByTestId("square-1-4", { exact: false });
72+
const squareE4 = screen.getByTestId("square-3-4", { exact: false });
73+
74+
fireEvent.mouseDown(squareE2);
75+
fireEvent.mouseMove(squareE4);
76+
fireEvent.mouseUp(squareE4);
77+
78+
expect(squareE2.getAttribute("data-testid")).includes(`piece-empty`);
79+
expect(squareE4.getAttribute("data-testid")).includes(`piece-Pawn`);
80+
81+
expect(MockWebSocket.messages).toContain(JSON.stringify({ type: "move", move: "e2e4" }));
82+
});

frontend/test/mockWebSocket.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export class MockWebSocket {
2727

2828
static readyState = MockWebSocket.CONNECTING;
2929

30+
static messages: string[] = [];
31+
3032
triggerOpen() {
3133
MockWebSocket.readyState = MockWebSocket.OPEN;
3234
MockWebSocket.singletonOnopen?.();
@@ -52,5 +54,7 @@ export class MockWebSocket {
5254
}, 10);
5355
}
5456

55-
send() {}
57+
send(message: string) {
58+
MockWebSocket.messages.push(message);
59+
}
5660
}

0 commit comments

Comments
 (0)