Skip to content

Commit

Permalink
fix setPosition on BoardApi
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusocb committed Feb 18, 2023
1 parent e18e62a commit 2137d1d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/classes/BoardApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ export class BoardApi {
}

/**
* Returns the piece on the square or null if there is no piece
* loads a fen into the board
*/
setPosition(fen: string) {
this.game.load(fen);
// update board state
this.updateGameState();
}

/**
Expand Down Expand Up @@ -392,6 +392,13 @@ export class BoardApi {
*/
loadPgn(pgn: string): void {
this.game.loadPgn(pgn);
this.updateGameState();
}

/**
* apply the fen game on the board
*/
updateGameState(): void {
this.board.set({ fen: this.game.fen() });
this.board.state.turnColor = shortToLongColor(this.game.turn());

Expand Down
17 changes: 17 additions & 0 deletions src/tests/BoardAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ describe.concurrent('Test the board API', () => {
boardApi.resetBoard();
expect(boardApi?.getCurrentTurnNumber()).toBe(0);
});

it('should update board with pgnt', () => {
const pgn = '1. e4 e5';

boardApi.loadPgn(pgn);

expect(boardApi?.getPgn()).toBe(pgn);
expect(boardApi?.board.getFen()).toBe(boardApi?.getFen().split(' ')[0]);
});

it('should udpate board with fen', () => {
const fen = '8/8/4k3/8/4K3/8/8/8 w - - 0 1';

boardApi.setPosition(fen);

expect(boardApi?.board.getFen()).toBe(fen.split(' ')[0]);
});
});

export {};

0 comments on commit 2137d1d

Please sign in to comment.