the-game-domain
Version:
Complete domain layer for 'The Game' card game with rich business entities, automatic persistence, action tracking, and advanced game mechanics. Features Game, Player, Card, Deck, Pile entities with repository pattern, turn management, victory/defeat dete
144 lines • 8.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const GameAction_1 = require("./GameAction");
describe('GameAction', () => {
const mockGameId = 'game-123';
const mockPlayerId = 'player-456';
const mockPlayerName = 'Player 1';
describe('constructor', () => {
it('should create a game action with required fields', () => {
const action = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Jogo criado com 2 jogadores');
expect(action.id).toBeDefined();
expect(action.gameId).toBe(mockGameId);
expect(action.type).toBe('GAME_CREATED');
expect(action.description).toBe('Jogo criado com 2 jogadores');
expect(action.timestamp).toBeInstanceOf(Date);
});
it('should create a game action with optional fields', () => {
const action = new GameAction_1.GameAction(mockGameId, 'CARD_PLAYED', 'Player 1 jogou uma carta na pilha 1', mockPlayerId, mockPlayerName, 5, 1);
expect(action.playerId).toBe(mockPlayerId);
expect(action.playerName).toBe(mockPlayerName);
expect(action.cardValue).toBe(5);
expect(action.pileNumber).toBe(1);
});
it('should use provided id if given', () => {
const customId = 'custom-action-id';
const action = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Test', undefined, undefined, undefined, undefined, customId);
expect(action.id).toBe(customId);
});
});
describe('getters', () => {
it('should return immutable timestamp', () => {
const action = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Test');
const timestamp1 = action.timestamp;
const timestamp2 = action.timestamp;
expect(timestamp1).not.toBe(timestamp2);
expect(timestamp1.getTime()).toBe(timestamp2.getTime());
});
});
describe('equals', () => {
it('should return true for same action', () => {
const action1 = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Test', undefined, undefined, undefined, undefined, 'same-id');
const action2 = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Test', undefined, undefined, undefined, undefined, 'same-id');
expect(action1.equals(action2)).toBe(true);
});
it('should return false for different actions', () => {
const action1 = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Test', undefined, undefined, undefined, undefined, 'id-1');
const action2 = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Test', undefined, undefined, undefined, undefined, 'id-2');
expect(action1.equals(action2)).toBe(false);
});
});
describe('toString', () => {
it('should return string representation', () => {
const action = new GameAction_1.GameAction(mockGameId, 'GAME_CREATED', 'Test');
const result = action.toString();
expect(result).toContain('GameAction');
expect(result).toContain('GAME_CREATED');
expect(result).toContain('Test');
});
});
describe('toJSON', () => {
it('should serialize action to JSON', () => {
const action = new GameAction_1.GameAction(mockGameId, 'CARD_PLAYED', 'Player 1 jogou uma carta na pilha 1', mockPlayerId, mockPlayerName, 5, 1);
const json = action.toJSON();
expect(json.id).toBe(action.id);
expect(json.gameId).toBe(mockGameId);
expect(json.type).toBe('CARD_PLAYED');
expect(json.description).toBe('Player 1 jogou uma carta na pilha 1');
expect(json.playerId).toBe(mockPlayerId);
expect(json.playerName).toBe(mockPlayerName);
expect(json.cardValue).toBe(5);
expect(json.pileNumber).toBe(1);
expect(json.timestamp).toBeInstanceOf(Date);
});
});
describe('fromJSON', () => {
it('should deserialize action from JSON', () => {
const originalAction = new GameAction_1.GameAction(mockGameId, 'CARD_PLAYED', 'Player 1 jogou uma carta na pilha 1', mockPlayerId, mockPlayerName, 5, 1);
const json = originalAction.toJSON();
const deserializedAction = GameAction_1.GameAction.fromJSON(json);
expect(deserializedAction.id).toBe(originalAction.id);
expect(deserializedAction.gameId).toBe(originalAction.gameId);
expect(deserializedAction.type).toBe(originalAction.type);
expect(deserializedAction.description).toBe(originalAction.description);
expect(deserializedAction.playerId).toBe(originalAction.playerId);
expect(deserializedAction.playerName).toBe(originalAction.playerName);
expect(deserializedAction.cardValue).toBe(originalAction.cardValue);
expect(deserializedAction.pileNumber).toBe(originalAction.pileNumber);
expect(deserializedAction.timestamp.getTime()).toBe(originalAction.timestamp.getTime());
});
});
describe('factory methods', () => {
describe('gameCreated', () => {
it('should create game created action with singular player', () => {
const action = GameAction_1.GameAction.gameCreated(mockGameId, 1);
expect(action.type).toBe('GAME_CREATED');
expect(action.description).toBe(`Jogo ${mockGameId} criado com 1 jogador`);
expect(action.gameId).toBe(mockGameId);
});
it('should create game created action with plural players', () => {
const action = GameAction_1.GameAction.gameCreated(mockGameId, 3);
expect(action.type).toBe('GAME_CREATED');
expect(action.description).toBe(`Jogo ${mockGameId} criado com 3 jogadores`);
expect(action.gameId).toBe(mockGameId);
});
});
describe('cardPlayed', () => {
it('should create card played action', () => {
const action = GameAction_1.GameAction.cardPlayed(mockGameId, mockPlayerId, mockPlayerName, 10, 2);
expect(action.type).toBe('CARD_PLAYED');
expect(action.description).toBe(`${mockPlayerName} jogou a carta 10 na pilha 2`);
expect(action.gameId).toBe(mockGameId);
expect(action.playerId).toBe(mockPlayerId);
expect(action.playerName).toBe(mockPlayerName);
expect(action.cardValue).toBe(10);
expect(action.pileNumber).toBe(2);
});
});
describe('turnChanged', () => {
it('should create turn changed action', () => {
const action = GameAction_1.GameAction.turnChanged(mockGameId, mockPlayerId, mockPlayerName);
expect(action.type).toBe('TURN_CHANGED');
expect(action.description).toBe(`Vez de ${mockPlayerName}`);
expect(action.gameId).toBe(mockGameId);
expect(action.playerId).toBe(mockPlayerId);
expect(action.playerName).toBe(mockPlayerName);
});
});
describe('gameFinished', () => {
it('should create game finished action without winner', () => {
const action = GameAction_1.GameAction.gameFinished(mockGameId);
expect(action.type).toBe('GAME_FINISHED');
expect(action.description).toBe('Jogo finalizado!');
expect(action.gameId).toBe(mockGameId);
});
it('should create game finished action with winner', () => {
const action = GameAction_1.GameAction.gameFinished(mockGameId, 'Player 1');
expect(action.type).toBe('GAME_FINISHED');
expect(action.description).toBe('Jogo finalizado! Player 1 venceu!');
expect(action.gameId).toBe(mockGameId);
});
});
});
});
//# sourceMappingURL=GameAction.test.js.map