I’m building a simple battleship game in javascript and I’m trying to test this module with Jest:
class Ship{
constructor(length,coordinates){
this.length=length;
this.hits=0;
this.coordinates=coordinates;
this.sunk=false;
}
hit(){
return this.hits+=1
}
isSunk(){
if(this.hits==this.length){
this.sunk=true
return true
}else{
return false
}
}
}
class Gameboard{
constructor(player,ships){
this.player=player;
this.shipsNumber=ships.length;
this.ships=ships;//ships is an array of objects
this.missingCoordinates=[]
}
receiveAttack(coordinates){
let found=false
//inserire la logica di controllo se lo shot è stato gia fatto
//se non è stato fatto controlla se ha preso la nave
//Se ha preso la nave nopn cambiare turno, inserisci le coordinate nell' array shots e continua a giocare
//looks in each ship if the coordinate of the shot matches
this.ships.forEach(object=>{
if(object.coordinates.includes(coordinates)){
//if matches hit the ship and check if it s sunk
found=true;
object.hit();
if(object.isSunk()){
this.shipsNumber-=1
}
}
})
//if doesn t match put the coordinates in missing coordinates
// se invece non ha colpito la nave cambia turno e insirisci le coordinate negli shot
//later we can change the color to the cell and make it impossible to click again
if(found==false){
this.missingCoordinates.push(coordinates);
}
this.player.active=true;
}
}
class Player{
constructor(name,active){
this.name=name
this.active=active
}
attackOpponent(coordinates,opponentGameboard){
opponentGameboard.receiveAttack(coordinates);
this.active=false;
}
}
module.exports={Gameboard,Ship,Player};
Using this tests sequence:
let {Gameboard,Ship, Player} = require("./App");
let user=new Player("Eligio",true);
let computer=new Player("Computer",false);
let userShips=[new Ship(4,["00","01","02","03"]),new Ship(4,["20","21","22","23"])];
let computerShips=[new Ship(4,["70","71","72","73"]),new Ship(4,["80","81","82","83"])];
test('hitted boat', () => {
let gameboard1= new Gameboard(user, userShips)
gameboard1.receiveAttack("00");
expect(gameboard1.ships[0].hits).toBe(1);
});
test('Sunk boat', () =>{
let gameboard1= new Gameboard(user, userShips)
gameboard1.receiveAttack("00");
gameboard1.receiveAttack("01");
gameboard1.receiveAttack("02");
gameboard1.receiveAttack("03");
expect(gameboard1.ships[0].sunk).toBeTruthy()
})
test('missing shot', ()=>{
let gameboard1= new Gameboard(user, userShips)
gameboard1.receiveAttack("25")
expect(gameboard1.missingCoordinates[0]).toBe("25")
})
test('all ships are sunk',()=>{
let gameboard1= new Gameboard(user, userShips)
gameboard1.receiveAttack("00");
gameboard1.receiveAttack("01");
gameboard1.receiveAttack("02");
gameboard1.receiveAttack("03");
gameboard1.receiveAttack("20");
gameboard1.receiveAttack("21");
gameboard1.receiveAttack("22");
gameboard1.receiveAttack("23");
expect( gameboard1.shipsNumber).toEqual(0);
})
test('switching players', ()=>{
let playerGameboard= new Gameboard(user, userShips);
let computerGameboard=new Gameboard(computer,computerShips)
user.attackOpponent("00",computerGameboard);
expect(user.active).toBeFalsy();
expect(computer.active).toBeTruthy()
computer.attackOpponent("00",playerGameboard);
expect(computer.active).toBeFalsy()
expect(user.active).toBeTruthy();
})
I manage to pass all tests apart for the “all ships are sunk”.
Using different console logs the App works as expected so I can’t really understand why the test doesn’t pass.
I’ve also tried different Jest Matchers like .toBe to see if there were any differences but nothing
Can anyone help me please?