From 9c52f3dfa5bc2e1f2e8107d38f437107528ee9d9 Mon Sep 17 00:00:00 2001 From: Ido-Barnea Date: Mon, 22 Apr 2024 12:32:05 +0300 Subject: [PATCH] Wrote KillPieceByPieceAction tests --- .../KillPieceByEnvironmentAction.test.ts | 7 -- .../actions/KillPieceByPieceAction.test.ts | 87 +++++++++++++++++++ 2 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 core/development/logic/actions/KillPieceByPieceAction.test.ts diff --git a/core/development/logic/actions/KillPieceByEnvironmentAction.test.ts b/core/development/logic/actions/KillPieceByEnvironmentAction.test.ts index 3bae870..d001f9f 100644 --- a/core/development/logic/actions/KillPieceByEnvironmentAction.test.ts +++ b/core/development/logic/actions/KillPieceByEnvironmentAction.test.ts @@ -35,13 +35,6 @@ game.getPlayersTurnSwitcher = jest.fn().mockReturnValue({ }); game.endMove = jest.fn(); -jest.mock('./KillPieceAction', () => ({ - __esModule: true, - KillPieceAction: jest.fn().mockImplementation(() => ({ - execute: jest.fn().mockReturnValue(1), - })), -})); - describe('KillPieceByEnvironmentAction', () => { afterEach(() => { jest.clearAllMocks(); diff --git a/core/development/logic/actions/KillPieceByPieceAction.test.ts b/core/development/logic/actions/KillPieceByPieceAction.test.ts new file mode 100644 index 0000000..92be780 --- /dev/null +++ b/core/development/logic/actions/KillPieceByPieceAction.test.ts @@ -0,0 +1,87 @@ +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 { KillPieceByPieceAction } from './KillPieceByPieceAction'; +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.setKillerPiece = jest.fn(); + +describe('KillPieceByPieceAction', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('should return FAILURE if killedPiece.health > 0', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: OVERWORLD_BOARD_ID, + }; + const killedPiece = new Pawn(whitePlayer, initialPosition); + const initialPieceHealth = 6; + killedPiece.health = initialPieceHealth; + console.log(killedPiece.health); + const killPieceByPieceAction = new KillPieceByPieceAction( + killedPiece, + killedPiece, + ); + + // Act + const actionResult = killPieceByPieceAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.FAILURE); + expect(killedPiece.health).toEqual(initialPieceHealth - 1); + }); + + test('should return SUCCESS if killedPiece.health == 1', () => { + // Arrange + const initialPosition: Position = { + coordinates: [4, 0], + boardId: OVERWORLD_BOARD_ID, + }; + const killedPiece = new Pawn(whitePlayer, initialPosition); + const killPieceByPieceAction = new KillPieceByPieceAction( + killedPiece, + killedPiece, + ); + + // Act + const actionResult = killPieceByPieceAction.execute(); + + // Assert + expect(actionResult).toEqual(ActionResult.SUCCESS); + expect(game.setKillerPiece).toHaveBeenCalledTimes(1); + }); +});