Rework and test getPokemonPrefix

This commit is contained in:
Laeticia PIERRE 2024-05-08 22:41:49 +02:00
parent 29914de5f6
commit 1bf9755691
2 changed files with 86 additions and 7 deletions

81
src/messages.test.ts Normal file
View File

@ -0,0 +1,81 @@
import { expect, describe, it } from "vitest";
import Pokemon from "./field/pokemon";
import { getPokemonMessage, getPokemonPrefix } from "./messages";
import { BattleSpec } from "./enums/battle-spec";
describe("messages", () => {
describe("getPokemonPrefix", () => {
it("returns an empty prefix if the pokemon is a pokemon of the player", () => {
const pokemon = {
isPlayer: () => true,
hasTrainer: () => false,
scene: {
currentBattle: {
battleSpec: BattleSpec.DEFAULT
}
}
} as Pokemon;
expect(getPokemonPrefix(pokemon)).toBe('');
});
it("returns the wild prefix if the pokemon does not have a trainer", () => {
const pokemon = {
isPlayer: () => false,
hasTrainer: () => false,
scene: {
currentBattle: {
battleSpec: BattleSpec.DEFAULT
}
}
} as Pokemon;
expect(getPokemonPrefix(pokemon)).toBe('Wild ');
});
it("returns the foe prefix if the pokemon has a trainer which is not the player", () => {
const pokemon = {
isPlayer: () => false,
hasTrainer: () => true,
scene: {
currentBattle: {
battleSpec: BattleSpec.DEFAULT
}
}
} as Pokemon;
expect(getPokemonPrefix(pokemon)).toBe('Foe ');
});
it("returns the foe prefix if the pokemon is the final boss", () => {
const pokemon = {
isPlayer: () => false,
hasTrainer: () => false,
scene: {
currentBattle: {
battleSpec: BattleSpec.FINAL_BOSS
}
}
} as Pokemon;
expect(getPokemonPrefix(pokemon)).toBe('Foe ');
});
});
describe("getPokemonMessage", () => {
it("returns a message with pokemon prefix, pokemon name and content given", () => {
const pokemon = {
name: "Gengar",
isPlayer: () => false,
hasTrainer: () => true,
scene: {
currentBattle: {
battleSpec: BattleSpec.DEFAULT
}
}
} as Pokemon;
expect(getPokemonMessage(pokemon, " is hurt\nby poison!")).toBe('Foe Gengar is hurt\nby poison!');
});
});
});

View File

@ -6,14 +6,12 @@ export function getPokemonMessage(pokemon: Pokemon, content: string): string {
}
export function getPokemonPrefix(pokemon: Pokemon): string {
let prefix: string;
if (pokemon.isPlayer()) return '';
switch (pokemon.scene.currentBattle.battleSpec) {
case BattleSpec.DEFAULT:
prefix = !pokemon.isPlayer() ? pokemon.hasTrainer() ? 'Foe ' : 'Wild ' : '';
break;
return pokemon.hasTrainer() ? 'Foe ' : 'Wild ';
case BattleSpec.FINAL_BOSS:
prefix = !pokemon.isPlayer() ? 'Foe ' : '';
break;
return 'Foe ';
}
return prefix;
}
}