-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #288 from Ido-Barnea/264-improve-test-coverage
Wrote more unit tests
- Loading branch information
Showing
12 changed files
with
565 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { OVERWORLD_BOARD_ID } from '../../Constants'; | ||
import { game } from '../../Game'; | ||
import { PlayerInventory } from '../inventory/PlayerInventory'; | ||
import { Queen } from '../pieces/Queen'; | ||
import { Position } from '../pieces/types/Position'; | ||
import { Player } from '../players/Player'; | ||
import { PlayerColor } from '../players/types/PlayerColor'; | ||
import { AttackPieceAction } from './AttackPieceAction'; | ||
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('AttackPieceAction', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('Invalid - Piece Undefined Position', () => { | ||
// Arrange | ||
const piece = new Queen(whitePlayer, undefined); | ||
|
||
const attackPieceAction = new AttackPieceAction(piece, piece); | ||
|
||
// Act | ||
const actionResult = attackPieceAction.execute(); | ||
|
||
// Assert | ||
expect(actionResult).toEqual(ActionResult.FAILURE); | ||
}); | ||
|
||
test('Valid - Normal Kill', () => { | ||
// Arrange | ||
const initialKillerPiecePosition: Position = { | ||
coordinates: [0, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
const killerPiece = new Queen(whitePlayer, initialKillerPiecePosition); | ||
const initialKilledPiecePosition: Position = { | ||
coordinates: [1, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
const killedPiece = new Queen(whitePlayer, initialKilledPiecePosition); | ||
|
||
const attackPieceAction = new AttackPieceAction(killerPiece, killedPiece); | ||
|
||
// Act | ||
const actionResult = attackPieceAction.execute(); | ||
|
||
// Assert | ||
expect(actionResult).toEqual(ActionResult.SUCCESS); | ||
}); | ||
|
||
test('Valid - Attack Self', () => { | ||
// Arrange | ||
const initialPiecePosition: Position = { | ||
coordinates: [3, 4], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
const piece = new Queen(whitePlayer, initialPiecePosition); | ||
|
||
const attackPieceAction = new AttackPieceAction(piece, piece); | ||
|
||
// Act | ||
const actionResult = attackPieceAction.execute(); | ||
|
||
// Assert | ||
expect(actionResult).toEqual(ActionResult.SUCCESS); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { OVERWORLD_BOARD_ID } from '../../Constants'; | ||
import { game } from '../../Game'; | ||
import { PlayerInventory } from '../inventory/PlayerInventory'; | ||
import { King } from '../pieces/King'; | ||
import { Position } from '../pieces/types/Position'; | ||
import { Player } from '../players/Player'; | ||
import { PlayerColor } from '../players/types/PlayerColor'; | ||
import { CastleAction } from './CastleAction'; | ||
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('CastleAction', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('Invalid - Piece Undefined Position', () => { | ||
// Arrange | ||
const kingPiece = new King(whitePlayer, undefined); | ||
const targetPosition: Position = { | ||
coordinates: [6, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
|
||
const castleAction = new CastleAction(kingPiece, targetPosition); | ||
|
||
// Act | ||
const actionResult = castleAction.execute(); | ||
|
||
// Assert | ||
expect(actionResult).toEqual(ActionResult.FAILURE); | ||
}); | ||
|
||
test('Invalid - Piece Invalid Position', () => { | ||
// Arrange | ||
const initialPosition: Position = { | ||
coordinates: [4, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
const kingPiece = new King(whitePlayer, initialPosition); | ||
const targetPosition: Position = { | ||
coordinates: [5, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
|
||
const castleAction = new CastleAction(kingPiece, targetPosition); | ||
|
||
// Act | ||
const actionResult = castleAction.execute(); | ||
|
||
// Assert | ||
expect(actionResult).toEqual(ActionResult.FAILURE); | ||
}); | ||
|
||
test('Valid - Kingside Castling', () => { | ||
// Arrange | ||
const initialPosition: Position = { | ||
coordinates: [4, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
const kingPiece = new King(whitePlayer, initialPosition); | ||
const targetPosition: Position = { | ||
coordinates: [6, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
|
||
const castleAction = new CastleAction(kingPiece, targetPosition); | ||
|
||
// Act | ||
const actionResult = castleAction.execute(); | ||
|
||
// Assert | ||
expect(actionResult).toEqual(ActionResult.SUCCESS); | ||
}); | ||
|
||
test('Valid - Queenside Castling', () => { | ||
// Arrange | ||
const initialPosition: Position = { | ||
coordinates: [4, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
const kingPiece = new King(whitePlayer, initialPosition); | ||
const targetPosition: Position = { | ||
coordinates: [2, 0], | ||
boardId: OVERWORLD_BOARD_ID, | ||
}; | ||
|
||
const castleAction = new CastleAction(kingPiece, targetPosition); | ||
|
||
// Act | ||
const actionResult = castleAction.execute(); | ||
|
||
// Assert | ||
expect(actionResult).toEqual(ActionResult.SUCCESS); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.