From 22a8776d3565d1bf4619e6667025d3833c04fcb4 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 13:19:09 +0300 Subject: [PATCH 1/2] Wrote PermanentlyKillPieceAction tests --- core/development/LogicAdapter.ts | 4 - .../actions/KillPieceByPieceAction.test.ts | 1 - .../PermanentlyKillPieceAction.test.ts | 85 +++++++++++++++++++ .../actions/PermanentlyKillPieceAction.ts | 6 +- 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 core/development/logic/actions/PermanentlyKillPieceAction.test.ts diff --git a/core/development/LogicAdapter.ts b/core/development/LogicAdapter.ts index cd01f9c..424707c 100644 --- a/core/development/LogicAdapter.ts +++ b/core/development/LogicAdapter.ts @@ -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) => { diff --git a/core/development/logic/actions/KillPieceByPieceAction.test.ts b/core/development/logic/actions/KillPieceByPieceAction.test.ts index 92be780..fc989be 100644 --- a/core/development/logic/actions/KillPieceByPieceAction.test.ts +++ b/core/development/logic/actions/KillPieceByPieceAction.test.ts @@ -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, diff --git a/core/development/logic/actions/PermanentlyKillPieceAction.test.ts b/core/development/logic/actions/PermanentlyKillPieceAction.test.ts new file mode 100644 index 0000000..817ae4a --- /dev/null +++ b/core/development/logic/actions/PermanentlyKillPieceAction.test.ts @@ -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('KillPieceByPieceAction', () => { + 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); + }); +}); diff --git a/core/development/logic/actions/PermanentlyKillPieceAction.ts b/core/development/logic/actions/PermanentlyKillPieceAction.ts index a3d8e4c..8d83e58 100644 --- a/core/development/logic/actions/PermanentlyKillPieceAction.ts +++ b/core/development/logic/actions/PermanentlyKillPieceAction.ts @@ -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'; @@ -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); From 0a1286dd929b8f50eb0c5c757ffb5903c05c936e Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 13:22:00 +0300 Subject: [PATCH 2/2] Wrote RevertPieceMovementAction tests --- .../PermanentlyKillPieceAction.test.ts | 2 +- .../actions/RevertPieceMovementAction.test.ts | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 core/development/logic/actions/RevertPieceMovementAction.test.ts diff --git a/core/development/logic/actions/PermanentlyKillPieceAction.test.ts b/core/development/logic/actions/PermanentlyKillPieceAction.test.ts index 817ae4a..6a70d6e 100644 --- a/core/development/logic/actions/PermanentlyKillPieceAction.test.ts +++ b/core/development/logic/actions/PermanentlyKillPieceAction.test.ts @@ -40,7 +40,7 @@ game.setPieces = jest.fn(); game.increaseDeathCounter = jest.fn(); game.end = jest.fn(); -describe('KillPieceByPieceAction', () => { +describe('PermanentlyKillPieceAction', () => { afterEach(() => { jest.clearAllMocks(); }); diff --git a/core/development/logic/actions/RevertPieceMovementAction.test.ts b/core/development/logic/actions/RevertPieceMovementAction.test.ts new file mode 100644 index 0000000..f1ee1d3 --- /dev/null +++ b/core/development/logic/actions/RevertPieceMovementAction.test.ts @@ -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); + }); +});