Skip to content

Commit

Permalink
Merge pull request #290 from Ido-Barnea/264-improve-test-coverage
Browse files Browse the repository at this point in the history
Wrote more unit tests
  • Loading branch information
Ido-Barnea authored Apr 22, 2024
2 parents c410b20 + 0a1286d commit 53ac5fd
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 7 deletions.
4 changes: 0 additions & 4 deletions core/development/LogicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,6 @@ export function changePieceToAnotherPlayer(piece: BasePiece) {
.filter((_player) => _player !== piece.player)[0];
}

export function endGame() {
game.end();
}

export function switchInventory(player: Player) {
if (switchShownInventory(player.color)) {
player.inventory.getItems().forEach((item) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('KillPieceByPieceAction', () => {
const killedPiece = new Pawn(whitePlayer, initialPosition);
const initialPieceHealth = 6;
killedPiece.health = initialPieceHealth;
console.log(killedPiece.health);
const killPieceByPieceAction = new KillPieceByPieceAction(
killedPiece,
killedPiece,
Expand Down
85 changes: 85 additions & 0 deletions core/development/logic/actions/PermanentlyKillPieceAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { King } from '../pieces/King';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { PermanentlyKillPieceAction } from './PermanentlyKillPieceAction';
import { ActionResult } from './types/ActionResult';

const whitePlayer = new Player(PlayerColor.WHITE, new PlayerInventory());

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderGameInformation: jest.fn(),
hideUnicornAttackButton: jest.fn(),
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initializeInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));
jest.mock('../../ui/ShopUI.ts');

game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({
getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer),
getTurnsCount: jest.fn().mockReturnValue(1),
});
game.setPieces = jest.fn();
game.increaseDeathCounter = jest.fn();
game.end = jest.fn();

describe('PermanentlyKillPieceAction', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('should return SUCCESS', () => {
// Arrange
const initialPosition: Position = {
coordinates: [4, 0],
boardId: OVERWORLD_BOARD_ID,
};
const killedPiece = new Pawn(whitePlayer, initialPosition);
const permanentlyKillPieceAction = new PermanentlyKillPieceAction(
killedPiece,
);

// Act
const actionResult = permanentlyKillPieceAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.SUCCESS);
expect(game.setPieces).toHaveBeenCalledTimes(1);
expect(game.increaseDeathCounter).toHaveBeenCalledTimes(1);
});

test('should end game if killedPiece is King', () => {
// Arrange
const initialPosition: Position = {
coordinates: [4, 0],
boardId: OVERWORLD_BOARD_ID,
};
const killedPiece = new King(whitePlayer, initialPosition);
const permanentlyKillPieceAction = new PermanentlyKillPieceAction(
killedPiece,
);

// Act
permanentlyKillPieceAction.execute();

// Assert
expect(game.end).toHaveBeenCalledTimes(1);
});
});
6 changes: 4 additions & 2 deletions core/development/logic/actions/PermanentlyKillPieceAction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { game } from '../../Game';
import { destroyPieceOnBoard, endGame } from '../../LogicAdapter';
import { destroyPieceOnBoard } from '../../LogicAdapter';
import { King } from '../pieces/King';
import { BasePiece } from '../pieces/abstract/BasePiece';
import { GameAction } from './abstract/GameAction';
Expand All @@ -19,7 +19,9 @@ export class PermanentlyKillPieceAction implements GameAction {
.filter((piece) => piece !== this.killedPiece);
game.setPieces(filteredPieces);

if (this.killedPiece instanceof King) endGame();
if (this.killedPiece instanceof King) {
game.end();
}

game.increaseDeathCounter();
destroyPieceOnBoard(this.killedPiece);
Expand Down
71 changes: 71 additions & 0 deletions core/development/logic/actions/RevertPieceMovementAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { OVERWORLD_BOARD_ID } from '../../Constants';
import { game } from '../../Game';
import { PlayerInventory } from '../inventory/PlayerInventory';
import { Pawn } from '../pieces/Pawn';
import { Position } from '../pieces/types/Position';
import { Player } from '../players/Player';
import { PlayerColor } from '../players/types/PlayerColor';
import { RevertPieceMovementAction } from './RevertPieceMovementAction';
import { ActionResult } from './types/ActionResult';

const whitePlayer = new Player(PlayerColor.WHITE, new PlayerInventory());

jest.mock('../../ui/BoardManager.ts', () => ({
destroyElementOnBoard: jest.fn(),
moveElementOnBoard: jest.fn(),
getSquareElementById: jest.fn(),
spawnPieceElementOnBoard: jest.fn(),
getAllSquareElements: jest.fn(),
highlightLastMove: jest.fn(),
}));
jest.mock('../../ui/Screen.ts', () => ({
renderGameInformation: jest.fn(),
hideUnicornAttackButton: jest.fn(),
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initializeInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));
jest.mock('../../ui/ShopUI.ts');

game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({
getCurrentPlayer: jest.fn().mockReturnValue(whitePlayer),
getTurnsCount: jest.fn().mockReturnValue(1),
});

describe('RevertPieceMovementAction', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('should return FAILURE if piece.position is undefined', () => {
// Arrange
const piece = new Pawn(whitePlayer, undefined);
const revertPieceMovementAction = new RevertPieceMovementAction(piece);

// Act
const actionResult = revertPieceMovementAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.FAILURE);
});

test('should return SUCCESS if piece.position is defined', () => {
// Arrange
const initialPosition: Position = {
coordinates: [4, 0],
boardId: OVERWORLD_BOARD_ID,
};
const piece = new Pawn(whitePlayer, initialPosition);
const revertPieceMovementAction = new RevertPieceMovementAction(piece);

// Act
const actionResult = revertPieceMovementAction.execute();

// Assert
expect(actionResult).toEqual(ActionResult.SUCCESS);
});
});

0 comments on commit 53ac5fd

Please sign in to comment.